# Pong AI

## Recommended Posts

Before I start, I've actually Googled this topic. It really didn't return any useful results. Surprisingly, despite the fact that Pong seems to be a good starting place, there is very little information cover its AI. Well, I have written Pong to use shared and multiple input devices for 2 players (I'm considering updating it for up to 4 players when I get back from vacation). Input devices are currently keyboard and XBox 360 controller, with mouse support next. Now that a little history is out of the way... I'm kind of stumped on AI. I've had a few thoughts. I've planned out what needs to happen. 1. The AI paddle(s) should not move unless the ball is within a certain field of play, such as the 1/3 of the screen closest to the AI paddle. 2. If the ball hits the bottom/top boundary within, say, 50 pixels of the paddle, the AI paddle should not be able to hit the ball. I have detailed many other requirements of the AI and introduced scaling in order to incorporate different levels of difficulty, but I'm having a great deal of trouble figuring out how to make the AI predict where the ball will go. Note that this means the AI cannot know where the ball will go. I was thinking that the prediction should be based on the ball's velocity and where the ball hits a boundary, but I'm clueless about how to implement this. Any ideas? Pseudo-code would be fine...I'm not looking for the solution to be written for me, just some pointers in the right direction... Oh, and if you could offer any language-specific help, I'm using C# / XNA. One more thing...what's the tag to use bullets?

#### Share this post

##### Share on other sites
mmelson    232
It sounds like you're over thinking the problem. The simplest AI for Pong could be something like this:

if ball is above the center of the paddle, move the paddle up.
else if ball is below the center of the paddle, move the paddle down.

To make it easier or harder, just make the paddle move slower or faster (respectively).

To implement some of your other features, you could just add a check for the ball's x being within some distance of the paddle before you move (though, I don't think I've ever seen a person do that). To make sure there's a way they can miss (also, probably unnecessary), you could simple put a small gap at the top and bottom where the paddle can't reach, and you've got your guaranteed score area.

(Edit: Mentioned some ways to add the restrictions he talked about.)
(Edit: Edit: Stupid missing ')'.)

#### Share this post

##### Share on other sites
I probably am overthinking the topic.

So if I do something like

if (ball is above paddle){     //move paddle up}

would I just then add another check to stop the paddle if it is equal to the ball's Y position? Because it would look awfully silly to have the paddle continue moving up after it passes the ball. ^_^

I would normally test the solution first instead of asking, but I'm on vacation until Wednesday and don't have access to my source (or XNA for that matter).

#### Share this post

##### Share on other sites
Geometrian    1810
If you add another function so your code looks like:
if (ball is above paddle){     //move paddle up}else if (ball is below paddle){     //move paddle down}
(That's pseudo-code as I don't program C langs) That way, if the paddle rises over the ball, the second function is activated and the paddle moves down. This code will set the paddle's y-coord on a level with the ball, unless the ball's y-speed is greater than the paddle can move...

BTW, use "source" and "/source" (in brackets) for your code instead of "code" and "/code" (in brackets) :-)

#### Share this post

##### Share on other sites
Thanks! I guess I was really trying to make it more complicated than it was. I feel silly now. :(

Thanks again for the help. :)

#### Share this post

##### Share on other sites
You may want to check one of these:

Non-XNA
Tiny Tennis

XNA Version
Tiny Tennis w/ XNA

#### Share this post

##### Share on other sites
One way I've made a pong AI that has a higher miss rate, is by only moving the paddle if it crosses a certain top, or bottom border within(or outside even) the bat. I've only just started in C# so this is probably just pseudo code...
if (ball.y< bat.y-5){  //move the bat up}else if (ball.y> bat.y+5{  //move down}

Not sure if that would actually work in C#, but hopefully you get what I'm saying.

~TDOT>

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now