# 4-way pong

## 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;

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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628320
• Total Posts
2982057

• 12
• 9
• 9
• 13
• 11