Jump to content
  • Advertisement
Sign in to follow this  
stein102

Pong AI help

This topic is 2398 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

Right now I'm working on a pong clone game and the AI I want is pretty simple, if the ball is above the paddle, the paddle moves up and reverse. What I have is this:


if(ball.getY() < y - height/2){
moveUp();
}

if(ball.getY() > y + height/2){
moveDown();
}


What's happening is the paddle looks like it's spazzing out, it alternates between going up and down like every .5 seconds, but still travels in the general direction it's supposed to go.

Share this post


Link to post
Share on other sites
Advertisement
moveUp and moveDown affect the y variable right? In that case you may want to make it so that only one of those functions is called each time (possibly by turning the moveDown if statement into an else-if statement)

Share this post


Link to post
Share on other sites
#1 question before anyone should EVER bother helping you: Have you traced the execution of your code? If so, what happens? If you find it, you will know what your problem is and likely not need anyone else to do it for you.

Share this post


Link to post
Share on other sites
The ai paddle is probably spazzin out because each frame, when the paddle catches up to the ball, it goes too far up/down and in the next frame has to go back down/up. So instead of moving the opponent at a constand speed/frame, it should move by the difference between the position of the ball and center of paddle. Example: move by ballY - paddleY, if difference is greater than the max speed the paddle can go, put it as max speed.

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!