| 1 | #include <allegro.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <math.h> |
| 4 | #include <iostream.h> |
| 5 | |
| 6 | #define player_speed 1 |
| 7 | #define bullet_speed 2 |
| 8 | |
| 9 | |
| 10 | float player1_x = 320; |
| 11 | float player1_y = 240; |
| 12 | float player2_x; |
| 13 | float player2_y; |
| 14 | float player1_rotate; |
| 15 | float player2_rotate; |
| 16 | float bullet_x; |
| 17 | float bullet_y; |
| 18 | int mickeyx; |
| 19 | int mickeyy; |
| 20 | int RED = makecol(255,0,0); |
| 21 | |
| 22 | BITMAP *player1; |
| 23 | BITMAP *player2; |
| 24 | BITMAP *buffer; |
| 25 | |
| 26 | void bullet_move() |
| 27 | { |
| 28 | bullet_x += bullet_speed * fixtof(fcos(ftofix(player1_rotate))); |
| 29 | bullet_y += bullet_speed * fixtof(fsin(ftofix(player1_rotate))); |
| 30 | if(key[KEY_SPACE]) |
| 31 | { |
| 32 | bullet_x = player1_x; |
| 33 | bullet_y = player1_y; |
| 34 | putpixel(buffer,bullet_x, bullet_y,RED); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void player1_movement() |
| 39 | { |
| 40 | if(key[KEY_A] && player1_x > 0) |
| 41 | { |
| 42 | player1_x -= player_speed; |
| 43 | } |
| 44 | if(key[KEY_D] && player1_x < 640 - player1->w) |
| 45 | { |
| 46 | player1_x += player_speed; |
| 47 | } |
| 48 | if(key[KEY_W] && player1_x > 0 && player1_x < 640 - player1->w && player1_y > 0 && player1_y < 480 - player1->h) |
| 49 | { |
| 50 | player1_y -= player_speed * fixtof(fcos(ftofix(player1_rotate))); |
| 51 | player1_x += player_speed * fixtof(fsin(ftofix(player1_rotate))); |
| 52 | } |
| 53 | if(key[KEY_S] && player1_y < 480 - player1->h) |
| 54 | { |
| 55 | player1_y += player_speed; |
| 56 | } |
| 57 | if(player1_y + player1->h >= 480) |
| 58 | { |
| 59 | player1_y = 479 - player1->h; |
| 60 | } |
| 61 | if(player1_y <= 0) |
| 62 | { |
| 63 | player1_y = 1; |
| 64 | } |
| 65 | if(player1_x <= 0) |
| 66 | { |
| 67 | player1_x = 1; |
| 68 | } |
| 69 | if(player1_x + player1->w >= 640) |
| 70 | { |
| 71 | player1_x = 639 - player1->w; |
| 72 | } |
| 73 | get_mouse_mickeys(&mickeyx, &mickeyy); |
| 74 | player1_rotate += mickeyx; |
| 75 | |
| 76 | |
| 77 | } |
| 78 | |
| 79 | int main(int argc, char *argv[]) |
| 80 | { |
| 81 | allegro_init(); |
| 82 | install_keyboard(); |
| 83 | install_mouse(); |
| 84 | set_mouse_speed(1,1); |
| 85 | set_color_depth(16); |
| 86 | set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0); |
| 87 | cout << bullet_x << " " << bullet_y << endl; |
| 88 | |
| 89 | buffer = create_bitmap(640,480); |
| 90 | |
| 91 | player1 = load_bitmap("player.bmp", NULL); |
| 92 | |
| 93 | player2 = load_bitmap("player.bmp", NULL); |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | while(!key[KEY_ESC]) |
| 99 | { |
| 100 | player1_movement(); |
| 101 | bullet_move(); |
| 102 | rotate_sprite(buffer, player1, player1_x, player1_y, ftofix(player1_rotate)); |
| 103 | blit(buffer, screen,0,0,0,0,640,480); |
| 104 | clear_bitmap(buffer); |
| 105 | } |
| 106 | |
| 107 | destroy_bitmap(player1); |
| 108 | destroy_bitmap(player2); |
| 109 | destroy_bitmap(buffer); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | END_OF_MAIN() |