Skip to main content
gamedev.net
🔒 Locked

Frustrated!

Started by disknoir 1 reply 1.2k views
Original Post
disknoir
disknoir
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.
GeoMX
GeoMX
You're incorrectly reading input, you enter into input reading when a key is pressed, however, inside this code you only update the first bat, not both of them. To see what's happening, try changing the order of your getinput() calls, instead of:
getinput(0);getinput(1);

try this:
getinput(1);getinput(0);

Since you're checking for all keypresses in your getinput function, you could use only one call to this function and update both bats instead of calling it for every bat. Or just don't use the keypressed() condition.

Your x and y coordinates are relative to the bat's center, so, you're drawing 20 pixels above your y coordinate and 20 pixels below, so your stopping functions should use value 40 when going up and 440 when going down, not 50 nor 420 :P.

Another comment, you should try using some global/constant values, something like:
#define BAT_W 4#define BAT_H 40#define UPPER_BAR 20#define LOWER_BAR 460// Draw bat, x and y are bat center's coordinatesint x = bat[ num ].x;int y = bat[ num ].y;rectfill(screen, x - BAT_W / 2, y - BAT_H / 2, x + BAT_W / 2, y + BAT_H / 2, bat[num].colour );// Another option, x and y are bat's top left corner coordinatesrectfill(screen, x, y, x + BAT_W, y + BAT_H, bat[num].colour );


Regards,
JJ Enríquez (GeoMX).

[Edited by - GeoMX on August 11, 2006 11:53:18 AM]
Imagination's the limit.
disknoir
disknoir
Thanks man. I got the input sorted.
With the bats though, its not the fact that I didnt know the distance from the centre of the bat. 420 and 50 are the only numbers that it worked with.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.