i am new to game problem with allegro and i cannot get my object to move without seeing the objects remain as a move. Here is my code
| 1 | #include <allegro.h> |
| 2 | |
| 3 | volatile long speed_counter = 0; |
| 4 | |
| 5 | void increment_speed_counter() |
| 6 | { |
| 7 | speed_counter++; |
| 8 | } |
| 9 | END_OF_FUNCTION(increment_speed_counter); |
| 10 | |
| 11 | int start() |
| 12 | { |
| 13 | allegro_init(); |
| 14 | install_keyboard(); |
| 15 | if (set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0) < 0) |
| 16 | { |
| 17 | textout(screen, font, "Sorry your comuter does not support 320 x 240.", 0, 0, 0); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | void game() |
| 22 | { |
| 23 | int x, y; |
| 24 | x = 0; |
| 25 | y = 0; |
| 26 | BITMAP *smallship; |
| 27 | BITMAP *buffer; |
| 28 | buffer = create_bitmap(SCREEN_W, SCREEN_H); |
| 29 | smallship = load_bitmap("airplane.bmp",0); |
| 30 | install_timer(); |
| 31 | LOCK_VARIABLE(speed_counter); |
| 32 | LOCK_FUNCTION(increment_speed_counter); |
| 33 | install_int_ex(increment_speed_counter, BPS_TO_TIMER(60)); |
| 34 | while (!key[KEY_ESC]) |
| 35 | { |
| 36 | while (speed_counter > 0) |
| 37 | { |
| 38 | if (key[KEY_UP]) |
| 39 | y = y - 1; |
| 40 | if (key[KEY_DOWN]) |
| 41 | y = y + 1; |
| 42 | if (key[KEY_LEFT]) |
| 43 | x = x - 1; |
| 44 | if (key[KEY_RIGHT]) |
| 45 | x = x + 1; |
| 46 | speed_counter--; |
| 47 | } |
| 48 | acquire_screen(); |
| 49 | draw_sprite(buffer, smallship, x, y); |
| 50 | blit(buffer, screen, 0,0,0,0,320,240); |
| 51 | release_screen(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void exit() |
| 56 | { |
| 57 | allegro_exit(); |
| 58 | } |
| 59 | |
| 60 | int main(void) |
| 61 | { |
| 62 | start(); |
| 63 | game(); |
| 64 | return 0; |
| 65 | } |
| 66 | END_OF_MAIN() |
Can anyone tell me how i can fix this code. Thank you in advance.
Callclear_bitmap(buffer);after blitting it to the screen. Also, you shouldn't acquire the screen while you're not drawing to it. Since you're only blitting to the screen, Allegro will take care of it fine for you.
thx i got it to work.
just a quick note: you do know that you can use the simple math operations like y+=1; right? or if you're doing increments by 1, use y++; or y--; It works exactly the same, just saves ya a little time in coding. Good luck with the game!