Artificial inteligence used in a Pong game

Basic structure of an applet
Move a ball
Double buffering
Ball bouncing and change the direction
Using sound in applets
Using pictures in applets
Mouse events
Keyboard events
The first complete game
Artificial intelligence for a pong like game
Generation of random 2D landscapes

Many games need two players but mostly you have to play on your own, because no one 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 an 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

          y_pos += y_speed;
          y_pos -= y_speed;
      • // if the paddle's position is over the middle y - position
        if (real_y_pos < 148)
        // Paddle is under the middle y - position
        else if (real_y_pos > 152)
            y_pos -= y_speed;
            y_pos += y_speed;
        • // If ball's position smaller than paddle's, move up
          if (ball.getYPos() < real_y_pos)
          // If ball's position greater than padle's, move down
          else if (ball.getYPos() > real_y_pos)
      • // As long as ball's y - position and paddle's y - position are different
        if ( real_y_pos != ball.getYPos())
    • // calculate the middle of the paddle
      real_y_pos = y_pos + (size_y / 2);

      /* If the ball is moving in opposite direction to the paddle and is no danger for computer's goal move paddle back to the middle y - position*/
      if (ball.getXSpeed() > 0)
      // ball is moving towards paddle
      else if (ball.getXSpeed() < 0)
  • /** this method moves the computer */
    public void move (Ball ball)

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 an AI (in this case) that can not be beaten. 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 is 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 balance 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