|
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. -- Ryan Patterson - <http://cgamesplay.com/> |
Danex!
Member #7,759
September 2006
|
cool thx i will try that and post back results |
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 -- Ryan Patterson - <http://cgamesplay.com/> |
Danex!
Member #7,759
September 2006
|
1) void move_ball(){ if (ball_x == 630) { |
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; -- Ryan Patterson - <http://cgamesplay.com/> |
Danex!
Member #7,759
September 2006
|
ok so first off i need to change ---> and second i need to scrap the whole move ball function and restart with 4 variables of x,y,x_speed,y_speed |
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_speed 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 -- Ryan Patterson - <http://cgamesplay.com/> |
Danex!
Member #7,759
September 2006
|
mk i set up the boundaries for the paddle like you said and if worked perfectly |
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 Use whatever is easiest, because both are fine. -- Ryan Patterson - <http://cgamesplay.com/> |
Danex!
Member #7,759
September 2006
|
OK made a new move_ball() if (ball_y < 0){ what should i add so that if (ball_y < 0){ |
CGamesPlay
Member #2,559
July 2002
|
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 -- Ryan Patterson - <http://cgamesplay.com/> |
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!!!!111 Stop 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! -- Ryan Patterson - <http://cgamesplay.com/> |
Danex!
Member #7,759
September 2006
|
Whats wrong with globals? half my coding is globals ha ha, well not half but alot
basically when i run it the ball move down and right then hits the wall i set up prints "Player 1 Wins!" then exits |
CGamesPlay
Member #2,559
July 2002
|
In order for the ball to bounce off the top of the screen, you need to see when the ball's y is less than 0 Do the opposite for downward movement. [edit] -- Ryan Patterson - <http://cgamesplay.com/> |
Danex!
Member #7,759
September 2006
|
sweet it works, and you can reflect the ball and all but now it randomly just changes direction in mid screen, whats goin on there? |
CGamesPlay
Member #2,559
July 2002
|
It shouldn't randomly bounce mid-screen if your code is what you have up there in addition to what I said. About the ball going through paddles: you compare that the ball's y is >= the paddle's y, and that the ball's y is <= the paddle's y. This means you have to hit the very top of the paddle in order to bounce. You should be checking that the ball's y is >= the paddle's y, and the ball's y is <= the paddle's y + height. That is, the ball needs to be in between the top of the paddle and the bottom of it. -- Ryan Patterson - <http://cgamesplay.com/> |
Danex!
Member #7,759
September 2006
|
cool that works now the paddles bounce and all and i found the problem which was the ball is not bounceing randomly, sum how paddle 2 affects the ball as far as 3/4 of the map away from itself, (its like their is a virtual paddle at mid screen that does the same thing as paddle 2) i am goin thru my code to see how this is happening, also now i have the problem of the ball always moveing in the same direction when it hits a paddle or a wall so i need to add a angle of attack sum how, any ideas or suggestions? |
|