I created my own Pong, but the ball is not moving. I don`t have any error in my error list. In this game I used time.h to move the ball. Why the ball is not moving? What is wrong with my code?
Thanks in adbvance
#include <allegro.h>
#include <time.h>
int speed = 100;
double ball_x = 320;
double ball_y = 240;
BITMAP *buffer; //This will be our temporary bitmap for double buffering
clock_t final, initial;
double elapsed;
void moveBall(){
circlefill ( buffer, ball_x, ball_y, 5, makecol( 0, 0, 0)); //Erase the old ball
if(key[KEY_LEFT] && ball_x > 0 + 5)
ball_x = ball_x - speed * elapsed;
else if(key[KEY_RIGHT] && ball_x < 640 -5)
ball_x = ball_x + speed*elapsed;
if(key[KEY_UP] && ball_y > 0 + 5)
ball_y = ball_y - speed * elapsed;
else if(key[KEY_DOWN] && ball_y < 480 - 5)
ball_y = ball_y + speed * elapsed;
textprintf(buffer,font,10,10,makecol(255,255,255),"ball x = %d, ball y = %d",(int)ball_x,(int)ball_y);
circlefill ( buffer, ball_x, ball_y, 5, makecol( 255, 255, 255));
acquire_screen();
draw_sprite( screen, buffer, 0, 0);
release_screen();
}
int main(){
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
buffer = create_bitmap( 640, 480);
initial = clock();
final = clock();
while( !key[KEY_ESC]){
elapsed = (double)(clock() - initial)/(double) CLOCKS_PER_SEC;
initial = clock();
moveBall();
rest(20);
}
return 0;
}
END_OF_MAIN();