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();

If you don't have an error I would definatly be suspicious of the time variables. Have you proven them to be providing the correct data? Also I have heard even in c++ global variables are not highly looked upon and you can clearly pass elasped to moveball() so you probably should.

Try increasing the speed to like 2000, is the ball position printing in different coordinates? If the if statements getting executed?

I discovered the value of elapse. elapsed = 0.032000000000000001
I changed the speed of the ball but the ball refuses to move.
Why does the ball is not moving, if speed = 1000?
1000 * 0.032 = 32 pixels.
Why does elapse is less than zero? How can you fix the timer?

Are you pressing the arrow keys? Your logic for moving the ball requires that a key be pressed to move the ball in that direction.

I suppose the clock() will eventually wrap around, so you need to understand what the maximum value of clock() could be and handle it:

new_clock = clock();
    if (new_clock < initial)
        // (new_clock + MAX_CLOCKS) - initial, but re-ordered to avoid overflow
        delta = (MAX_CLOCKS - initial) + new_clock;
    else
        delta = new_clock - initial;
    elapsed = double(delta) / double(CLOCKS_PER_SEC);

The ball refuses to move. What is wrong with my code?

#include <allegro.h>
#include <time.h>
#define MAX_CLOCKS 1000 // one second

int speed = 1000;


double ball_x = 320;
double ball_y = 240;


BITMAP *buffer; //This will be our temporary bitmap for double buffering
clock_t new_clock, initial;
double elapsed;
double delta;

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();
	new_clock = clock();

	if (new_clock < initial)
// (new_clock + MAX_CLOCKS) - initial, but re-ordered to avoid overflow
	delta = (MAX_CLOCKS - initial) + new_clock;
	else
		 delta = new_clock - initial;
		elapsed = double(delta) / double(CLOCKS_PER_SEC);
	while( !key[KEY_ESC]){

		moveBall();
		
		rest(20);
	}

	return 0;


}

I don`t use allegro timers because I don´t know how to use them.
Can you teach me how to use them?

I don`t use allegro timers because I don´t know how to use them.
Can you teach me how to use them?

Oh, absolutely! Here you go. Anything else? We're here to serve.

Well now your update of initial and new_clock (and computation of elapsed ) are outside your moveBall() loop. Since the first two values are assigned on successive lines, so little time elapses between successive calls to clock() that they probably have the same value, making elapsed 0.0. This would certainly keep the ball from moving, according to the formulas you use in moveBall().

This article has been dead for over six months. Start a new discussion instead.