Public Group

4-way pong

This topic is 4512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

well I have the paddles for my pong game all I need is the algorithm for the pong ball, not sure where to look, I will probably need a little AI too.

Share on other sites
Well you could try something like this.

First you will need a few variables
int ball_x = 200; // Change this to where ever the xpos is neededint ball_y = 200; // Change this to where ever the ypos is neededint ball_vx = -4; // you can mess with these valuesint ball_vy = -4;

Now in your main loop
ball_x += ball_vx;ball_y += ball_yx;// Now Draw Ball// checking for collision with walls// Bounces ball of the left wallif(ball_x <= 0)ball_xv = -ball_xv;// bounces ball of top wallif(ball_y <= 0)ball_yv = -ball_yv;// Bounces ball of the right wallif(ball_x+ball_width >= screen_width) // substitute screen_width & ball_width valuesball_xv = -ball_xv;             // with there real values// bounces ball of bottom wallif(ball_y+ball_height >= screen_height)//substitute screen_height & ball_heightball_yv = -ball_yv;                    // with there real values

This should Draw Your ball and bounce of each side of your window.

Share on other sites
I am not to advanced as yet in programming(not up to graphical lvl yet), but for AI I believe you could have the paddles follow the ball with a 'rand() % 50) + 1;' to give it the chance of missing. The '50' being pixels, if the paddle was, say twenty pixels long, giving it a fifteen pixel overspill on each side.
Someone (I forgot who) once suggested to give the ball one speed (say ten pixels a second) and the paddles a slightly slower speed (say eight pixels a second) so that the computer wouldn't always have enough time to make it to the ball. I suggest you grab the top two ideas and alter them until they seem to work for four paddles. And that should do it for AI.

 I wouldn't know how to implement this though; so sorry I can't be of more help.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
15
5. 5

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631776
• Total Posts
3002300
×