Artificial inteligence used in a Pong game

Many games need two players but mostly you have to play on your own, because noone is there to play with you. For these situations you want an opponent that is not too strong but is strong enough so that the game is not boring at all. If you are programing such a game you have to write a AI (artificial inteligence), that "plays" against the human player. In this chapter I want to explain how to program a really simple AI one could use for example in a Pong game like Streethockey as I did. I will only explain how to program the AI, please take a look at the other classes and methods in the sourcecode by yourself (download at the end of the chapter).

The situation

First of all we should decide what we want our AI to do: In a Pong game the computer has to make goals against the player and has to save his own goal as a goalkeeper. A human player, who is playing this game, will always try to be at the same y - position as the ball, so that the ball will bounce when it hits the player paddle. The computer should do the same and now we want to "tell" the computer how to follow the ball's y - position.

The idea

So the computer has to "watch" the ball and tries to be at the same position (in the game Streethockey it's the y - position) as the ball. Imagine the ball object would have methods, that could tell us (the computer) it's velocity, direction and position, which could be the "eyes" of the computer. Then the AI method is really simple, as you see:

The AI - method

Explaining the method

This method is really simple, there are two different cases:
If the ball is not moving towards the paddle (it's x_speed is positive) then the paddle is moved back to the middle position of it's goal. To do this one has to know if the position is greater or smaller than the middle. If it's smaller then paddle is over the middle and you have to move the paddle down and if it's greater then move the paddle up.
Now we get to the interesting case, the "AI" case: The ball is moving towards the computer and the computer has to decide where to move. If the computers position is greater than the balls position, then the ball is over the paddle, the paddle has to be moved up. I think you know what happens if the ball's position is smaller then the position of the paddle... right, move the paddle down. That's it and it works as you can see if you watch the applet!

At least one important thing

It would be no problem to write a AI (in this case) that is not to beat. The computer doesn't get tired and is always "watching" the ball. You as a programer have to think of the human player. A game makes only fun, if you can win. Winning mustn't be too easy but it has to be possible. So the computer has to be weak too. For example it has to be slower than the ball, so that the computer can't get fast balls or something like that. You have to find the right ballance between a strong and a weak AI, which might take much more time than writing the AI itself.

Sourcecode download (*.zip - archive)
Take a look at the applet

Next chapter

Generation of a 2D landscape