Allegro.cc Forums » Game Design & Concepts » Pong problems

 Pong problems
 Danex! Member #7,759 September 2006 Hi all, i am new to allegro, just started around 3 or 4 days ago, i went through tutorials that i could find and learned sum stuff and looked at their code for pong and other games and decided to try and make my own pong clone useing what i had learned, so i set about it and got so far as to make 2 paddles and a ball. Both paddles move fine but i cannot figure out how to make them reflect the ball. i also cannot figure out how to stop the paddles from going off the map or make the ball reflect off the top and bottom walls, can anyone tell me how to set up boundaries for the walls and the paddle to reflect the ball?
 CGamesPlay Member #2,559 July 2002 Well, you have 2 problems: making the paddles stay on screen, and making the ball bounce.First, the paddles are on the screen if their Y value is 0 or greater, right? The paddle starts at Y, and goes down to Y + height. So, make sure that both paddles always have a Y greater than or equal to 0. Also, if the bottom of the paddle, Y + height, every goes off the bottom of the screen, SCREEN_H, then you need to stop it. That means that, if you detect that Y + height of either paddle is greater than SCREEN_H, you should set that paddle's Y value to SCREEN_H - height (it's an equation: Y + height = SCREEN_H, solve for Y).The ball should bounce off the top and bottom of the screen if its Y - radius goes under 0 (the top of the screen), or if its Y + radius goes greater than the SCREEN_H (the bottom of the screen). When you detect that that has happened, you should switch the directrion of the ball to moving the opposite direction up or down.You can tell if the ball is hitting a paddle because the X value is equal to the paddles X value, and the ball's Y is both larger than the paddle's Y value (the ball is lower than the top of the paddle), and less than the paddle's Y value + height (the ball is higher than the bottom of the paddle). When this happens, just switch the direction that the ball is going, left or right. -- Tomasu: Every time you read this: hugging!Ryan Patterson -
 Danex! Member #7,759 September 2006 cool thx i will try that and post back resultsDanex!
 CGamesPlay Member #2,559 July 2002 If that's the case then you shouldn't respond to this post until after you have the results, because you can't post twice in a row -- Tomasu: Every time you read this: hugging!Ryan Patterson -
 Danex! Member #7,759 September 2006 1)ok, i get that part about the paddle logic, i set the paddles to not go below 0 or above 480 with if (paddle_1_y < 0){ paddle_1_y = 0;} if (paddle_2_y < 0){ paddle_2_y = 0;} if (paddle_1_y > 480){ paddle_1_y = 480;} if (paddle_2_y > 480){ paddle_2_y = 480;}part of it works becuase the paddle will not go below 0, but the paddle goes alittle below my screen where i can see part but part is off the screen, i think it may be a problem with my screen becuase the paddle diffinently stops and won't move any more2) i cannot get the ball to move right???, thats my move ball function below, it also has my logic for bounceing the ball off the paddlevoid move_ball(){ if (ball_x == 630) { if (ball_y > paddle_2_y, paddle_2_y + 79 > ball_y) { ball_x--; } else if(ball_x==635) { ball_x--; textout_ex( buffer, font, "Player 1 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0)); } } if (ball_x == 5){ if (ball_y > paddle_2_y, paddle_2_y + 79 > ball_y) { ball_x--; } else if (ball_x==0){ball_x++; textout_ex( buffer, font, "Player 2 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0)); } } circlefill( buffer, ball_x, ball_y , 5, makecol( 0,255, 0)); }
 CGamesPlay Member #2,559 July 2002 If the paddle's Y value can become 480, that means that thetop of the paddle will be the very bottom row on the screen, with the rest of the paddle off of the screen. You need to make sure that the paddle's Y value never goes below the screen height - the paddle height.The move_ball function will need to be written again. In order to properly handle this, you need to have 4 variables. x, y, x_speed, and y_speed. Every time the function is called, add x_speed to x and y_speed to y. To make the ball move up, set y_speed to a negative number. To make the ball move down, set y_speed to a positive number. Same applies to x_speed and moving left/right.After you have that working properly, when the ball's y is less than 0, set y_speed = -y_speed, and when the ball's y is greater than the screen height, sey y_speed = -y_speed; -- Tomasu: Every time you read this: hugging!Ryan Patterson -
 Danex! Member #7,759 September 2006 ok so first off i need to change --->if (paddle_1_y < 0){paddle_1_y = 0;}if (paddle_2_y < 0){paddle_2_y = 0;}if (paddle_1_y > 480){paddle_1_y = 480;}if (paddle_2_y > 480){paddle_2_y = 480;} to something like --->if (paddle_1_y + paddle_1_hight > 480){paddle_1_y = 480;}and second i need to scrap the whole move ball function and restart with 4 variables of x,y,x_speed,y_speedin the second part i do not understand why that makes the ball move, am i supposed to update x_speed to update x?Danex!P.S. thx for all ur help and understanding that i am ignorant of allegro logic
 CGamesPlay Member #2,559 July 2002 ```if (paddle_1_y < 0) { paddle_1_y = 0; } if (paddle_1_y + paddle_1_height > 480) { paddle_1_y = 480 - paddle_1_height; } ```That is how it should look for 1 paddle Quote: i need to scrap the whole move ball function and restart with 4 variables of x,y,x_speed,y_speedin the second part i do not understand why that makes the ball move, am i supposed to update x_speed to update x? Well, if you have x_speed = 1; and every move_ball you set x = x + x_speed; then the ball will move to the right 1 pixel every time move_ball is called. If x_speed is -1, then the ball will move left 1 pixel every time move_ball is called. You can make it move faster, too. x_speed = 1 means it moves 2 pixels right each call, etc. All of this also applies to the y_speed.Quote: P.S. thx for all ur help and understanding that i am ignorant of allegro logic Well, to be technical, this is just math -- Tomasu: Every time you read this: hugging!Ryan Patterson -
 Danex! Member #7,759 September 2006 mk i set up the boundaries for the paddle like you said and if worked perfectly thanks alot, can you give kudos or karma or something on this forum?also is it a good idea to mix primitives like"circlefill( buffer, ball_x, ball_y , 5, makecol( 0,255, 0));"with bitmaps, or should i just make another bitmap of a ball?P.S. working on the moving ball and i am currently using a primative for the ball and don't want that to mess my program
 CGamesPlay Member #2,559 July 2002 Quote: thanks alot, can you give kudos or karma or something on this forum? If there isn't a check box that says "this question has been answered to my satisfaction", then no, there isn't. You have to specify the kind of thread as one "with a specific answer" when you make the thread.Quote: also is it a good idea to mix primitives like"circlefill( buffer, ball_x, ball_y , 5, makecol( 0,255, 0));"with bitmaps, or should i just make another bitmap of a ball? Use whatever is easiest, because both are fine. -- Tomasu: Every time you read this: hugging!Ryan Patterson -
 Danex! Member #7,759 September 2006 OK made a new move_ball()void move_ball(){ int x_speed=1;int y_speed=1;ball_x += x_speed;ball_y += y_speed; if (ball_y < 0){ball_y = 0;}if (ball_y > 475){ball_y = 475;}if (ball_x == 5 && ball_y >= paddle_1_y && ball_y <= paddle_1_y){ x_speed = -1; y_speed = -1; }if (ball_x == 635 && ball_y >= paddle_2_y && ball_y <= paddle_2_y){ x_speed = -1; y_speed = -1; }i am gettin somewhere at least cause the ball moves down and to the rightand i tried the greator than or equal to math but i am gettin no reflectionOn, the plus side i applied the same logic used in the paddles to make the ball not able to leave the mapHowever, the ball merely sticks to the bottom and straight lines to the endi believe this is due to me not makeing the ball bounce with this --->if (ball_y < 0){ball_y = 0;}if (ball_y > 475){ball_y = 475;}what should i add so thatif (ball_y < 0){ball_y = 0;---->switch direction of incoming ball(bounce)}if (ball_y > 475){ball_y = 475;---->switch direction of incoming ball(bounce)}
 CGamesPlay Member #2,559 July 2002 Please read thisYour speed variables are local vairables. Even if you set them to -1, the will get set to 1 when you call the function again. Make them globals -- Tomasu: Every time you read this: hugging!Ryan Patterson -
 nonnus29 Member #2,606 August 2002 Quote: Your speed variables are local vairables. Even if you set them to -1, the will get set to 1 when you call the function again. Make them globals OMFG!!!!111Stop the madness!! CGames is telling a newb to use globals!!! He's out of control, someone stop him!!!!Danex!, when you use global variables, it makes kittens cry.
 Kikaru Member #7,616 August 2006 [in scary robot voice] "Use global variables or I make you cry!" [/in scary robot voice]
 CGamesPlay Member #2,559 July 2002 In Soviet Russia, global variables use you! -- Tomasu: Every time you read this: hugging!Ryan Patterson -
Danex!
Member #7,759
September 2006

Whats wrong with globals? half my coding is globals ha ha, well not half but alot
Danex!
also l o l makes I'm Dumb!...thats pretty funny
anyway i made them globals but the ball still doesn't bounce
the following is my entire program as u can see lots of globals