Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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 needed
int ball_y = 200; // Change this to where ever the ypos is needed
int ball_vx = -4; // you can mess with these values
int 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 wall
if(ball_x <= 0)
ball_xv = -ball_xv;

// bounces ball of top wall
if(ball_y <= 0)
ball_yv = -ball_yv;

// Bounces ball of the right wall
if(ball_x+ball_width >= screen_width) // substitute screen_width & ball_width valuesball_xv = -ball_xv; // with there real values

// bounces ball of bottom wall
if(ball_y+ball_height >= screen_height)//substitute screen_height & ball_height
ball_yv = -ball_yv; // with there real values

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

Share this post

Link to post
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.

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!