help- need decent AI for Pong

Started by
9 comments, last by Greig Hamilton 18 years, 1 month ago
hi, i've been trying to figure out how to make decent AI for a Pong clone I'm making for about a week now. ive set the y-axis value of the computers paddle to equal the y value of the ball, but that is just too good for anybody to win against. Then I offset the y value of the computers paddle to be a few pixels off the ball's predicted y value, and that doesn't seem good enough to pass as good AI. I don't know what I'm asking for. I guess I want something that's a little more human. Also I'm programming this in C++ with the Allegro game library if that helps any. Thanks for any help
Advertisement
Have it track the ball, but give it a maximum velocity.
Quote:Original post by Sneftel
Have it track the ball, but give it a maximum velocity.


Yes, pseudo: (make the paddle max speed slower than that of the ball)
// make it centeredif (paddle.y + paddle.height / 2 < ball.y + ball.height / 2)    paddle.y += maximum_paddle_speed;// make it centeredif (paddle.y + paddle.height / 2 > ball.y + ball.height / 2)    paddle.y -= maximum_paddle_speed;

This way if the ball speed is 5 and the paddle speed is 3, then the paddle might now and then miss the ball because it won't be thinking ahead of time, unlike us... You could make it do that, by calculating future ball positions and stuff, but there is no need :). This is how I made my pong AI before.
Another idea is to give the computer a reaction time. Don't have it adjust its velocity until after some amount of time after the player hits the ball. This way it will seem less mechanical.
I see 4 options:
- You could do what the first two suggested.
- You could calculate where the ball will hit and go to that place+random (so it may miss the ball). This random number can be based on difficulty.
- Make a neural network, or ID3 tree, design some high level properties and let the computer learn alone how to play.
- Make some evolution style genetic code that will breed AI and make them fight eachother until the best one wins.

(of course Im just kidding with the last two [smile])

Iftah.
Give your computer paddle momentum, and reaction time, since a real world person has both of those, when your ball ends up moving too fast, or the computer tries to cover a large distance to quickly it might overshoot, and miss like a real person.
Here's what I do: give the enemy paddle the same AI as before, but only bring it into effect when the ball is a certain distance away from the x-value of the paddle. Like so:

// assume the following://// ball - structure containing x and y values of ball// enemy - structure containing y value of the enemy paddle// AI_RESPOND_BOUNDARY - constant that defines how close the ball must be before the paddle responds to it// ENEMY_PADDLE_SIZE - the size of the enemy paddle, in pixels or other world units// ENEMY_PADDLE_SPEED - speed, in units or pixels, of the enemy paddle per frame/second/whatever//if (ball.x > AI_RESPOND_BOUNDARY)              // check for the bounary{    if (ball.y > enemy.y + ENEMY_PADDLE_SIZE) // if the ball is lower than the paddle        enemy.y+=ENEMY_PADDLE_SPEED;          // increment the paddle's y value    if (ball.y < enemy.y)                     // if the ball is higher        enemy.y-=ENEMY_PADDLE_SPEED;          // deincrement the paddle's y value}


Of course, you could always add what agi_shi said about putting the ball into the center of the paddle, but whether it's necessary depends on the way you're doing collision detection. In any case, it's probably fine. That's what I'd do, anyway, so go ahead and make your own modifications.
Have a look at this old post, it contains info about AI for pong. Also the last post has a link to an article about Pong AI.
http://www.gamedev.net/community/forums/topic.asp?topic_id=368480&whichpage=1?
Hopefully you will find something useful there.

Greig

Edit: Added the URL and fixed my signature link. (Must have been tired to forget the URL.)

[Edited by - Greig Hamilton on March 6, 2006 6:09:01 AM]
Quote:Original post by Greig Hamilton
Have a look at this old post, it contains info about AI for pong. Also the last post has a link to an article about Pong AI.

Hopefully you will find something useful there.

Greig


Where's the link? [grin]

And your sig link is broken.
The AI paddle in my pong game followed the ball and it worked fine. To change the difficulty there was a target area on the paddle that the ball had to be out of to move the ball. The smaller the target area is the harder it is to score. Every level just made the area smaller.
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!

This topic is closed to new replies.

Advertisement