Original Post
I am trying to write my first game (pong) using allegro. I have written the code for the playing area and the bats, but I am having a couple of problems which I cant seem to solve. In the folowing code there are two things that are not working; First is the bat for player 2 does not move up when the up key is pressed; Secondly, I am using the function stopbat() to top the bats at the boundries. However the bats only seem to stop at certain values of y namely 50 and 420. These happen to be the first values that I entered to see if the function was working. When I change the values the bats dont stop at all. This is the header file (pong.h); //********************// // Pong header file // //********************// #ifndef _PONG_H #define _PONG_H #include "allegro.h" //define some game constants int MAXSPEED = 10; //define bat structure struct tagbat { int x,y; int dir, speed; int colour; int score; } bat[2]; //Define game variables int gameover =0; //function prototypes void setupscreen(); void setupbat(int num); void drawbat(int num); void getinput(int num); void batup(int num); void movebat(int num); void batdown(int num); void stopbat(int num); #endif This is the main code #include "pong.h" //********************************// // Stop the bat at the boudries // //********************************// void stopbat(int num) { int speed = bat[num].speed; if(speed > 0) { if(bat[num].y == 50) bat[num].speed = 0; } if(speed < 0) { if(bat[num].y == 420) bat[num].speed = 0; } } //*************// // Move bats // //*************// void movebat(int num) { int speed = bat[num].speed; bat[num].y -= speed; } //***************// // Up function // //***************// void batup(int num) { bat[num].speed++; if(bat[num].speed < MAXSPEED); bat[num].speed = MAXSPEED; } void batdown(int num) { bat[num].speed--; if(bat[num].speed > -MAXSPEED); bat[num].speed = -MAXSPEED; } //******************// // Erase the bats // //******************// void erasebat(int num) { //calculate box to encompas bat int left = bat[num].x - 2; int top = bat[num].y - 20; int right = bat[num].x + 2; int bottom = bat[num].y + 20; //erase the bat rectfill(screen, left, top, right, bottom, 0); } //****************************************// // Construct bat using drawing function // //****************************************// void drawbat(int num) { int x = bat[num].x; int y = bat[num].y; //draw bat rectfill(screen, x-2, y-20, x+2, y+20, bat[num].colour); } //************************************// // check for player input (2 player) // //************************************// void getinput(int num) { if(key[KEY_ESC]) gameover = 1; //Player 1 if(key[KEY_W]) batup(0); if(!key[KEY_W]) bat[num].speed = 0; if(key[KEY_S]) batdown(0); if(!key[KEY_S]) bat[num].speed = 0; //Player 2 if(key[KEY_UP]) batup(1); if(!key[KEY_UP]) bat[num].speed = 0; if(key[KEY_DOWN]) batdown(1); if(!key[KEY_DOWN]) bat[num].speed = 0; } //*********************************************// // Set up the starting condition of each bat // //*********************************************// void setupbat() { //player 1 bat[0].x = 30; bat[0].y = 240; bat[0].speed = 0; bat[0].colour = 15; bat[0].score = 0; bat[0].dir = 0; //player 2 bat[1].x = (SCREEN_W-30); bat[1].y = 240; bat[1].speed = 0; bat[1].colour = 15; bat[1].score = 0; bat[1].dir = 0; } //*********************************************// // Set up the graphics mode and game screen // //*********************************************// void setupscreen(void) { int ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); if(ret != 0) { allegro_message(allegro_error); return; } textprintf(screen, font, 1, 1, 15, "Pong! - %dx%d P1: %d P2: %d", SCREEN_W, SCREEN_H, bat[0].score, bat[1].score); int x1 = 20; int y1 = 20; int x2 = 620; int y2 = 460; hline(screen, x1, y1, x2, 15); hline(screen, x1, y1-1, x2, 15); hline(screen, x1, y2, x2, 15); hline(screen, x1, y2+1, x2, 15); } //*****************// // Main function // //*****************// void main(void) { allegro_init(); install_keyboard(); install_timer(); srand(time(NULL)); setupscreen(); setupbat(); while(!gameover) { erasebat(0); erasebat(1); stopbat(0); stopbat(1); movebat(0); movebat(1); drawbat(0); drawbat(1); if (keypressed()) { getinput(0); getinput(1); } rest(30); } allegro_exit(); } END_OF_MAIN(); If someone could help me out I would be extremely greatfull. Thanks.