Pong ~Part 4~ The CPU’s Paddle

Welcome to part 4 of the Pong tutorial series. Please make sure you have read Part 1Part 2, and Part 3.

Step Four: Program the Computer-controlled Paddle

OK, so the last tutorial was pretty short and sweet. So lets waste no time getting into the next step. This step is pretty important because we begin to look at a very important step in game programming: AI (which stands for Artificial Intelligence).

Artificial Intelligence

Any time when the computer itself needs to control something (like an enemy in a game) and perform certain actions depending on certain conditions, we call this AI. Now, our AI is fairly basic. We need the computer’s paddle to move up and down to try to block the ball from getting past. It would be easy to make the cpuPaddle ALWAYS block the ball — all we would do is constantly set the cpuPaddle.y to the ball.y position. But how do we give the computer the intelligence to act like a normal player, sometimes succeeding and other times failing?

The best way to figure this out is to simply look at how a normal player would react in a situation, and try to replicate that with code.

This is actually pretty easy in our case — just think of how you play pong. You constantly watch the ball. If it is too far below your paddle, you move your paddle down. If it is too far above, you move it up. However, to stop the computer from being an invincible pong player, we will give the cpuPaddle a max movement speed, so that it can’t always catch the ball in time. That will be the basis for our AI. It’s really simple, but that’s all we need right now.

At the top of your file create a new variable which will control how fast the cpuPaddle can move up and down:


var cpuPaddleSpeed:int = 3;

And then in your game loop function, add this code (anywhere is fine, I just put it at the top).

if(cpuPaddle.y < ball.y - 10){
	cpuPaddle.y += cpuPaddleSpeed;
} else if(cpuPaddle.y > ball.y + 10){
	cpuPaddle.y -= cpuPaddleSpeed;
}

This code moves the cpuPaddle. If the y position of the paddle is more than 10 pixels above or below the ball, it will move in whatever direction it needs to, with a speed equal to the cpuPaddleSpeed.

Wait a minute… now that we spent all this time giving the cpuPaddle some basic AI to make it imperfect, it still never misses the ball! Don’t worry, this is only for the moment. Right now, the ball is traveling at a constant y-speed: 2 (or -2). But once we program the collisions between the paddles and the ball we are going to add a few lines of code that will change the ball’s y speed (and therefore, its direction) so that it is possible for the ball to move faster than the cpuPaddle can keep up with.

Get the source code while supplies last! Click here to download.

Again, this was only a short-and-sweet tutorial designed to get the cpuPaddle moving with some AI. Next tutorial we are going to get into the juicy part you’ve all been waiting for: programming the collisions between the paddles and the ball — at which point you will actually be able to play the game!

And then finally, after that, we will add scoring and some other extra-cool features. If there’s a specific game feature that could be added to pong which you wish me to include a tutorial about, just leave me a comment below and I promise I’ll get back to you.

13 comments on “Pong ~Part 4~ The CPU’s Paddle

  1. David says:

    These Tutorials are amazing thank you its a very nice and fantastic work i love making and playing games this inspires me more for video game designer :D

  2. Helmi Shonib says:

    both my paddles doesn;t move :(
    Please help!
    my paddle is stuck at bottom of screen while cpu paddle is in the middle of screen

    • David says:

      you might want to check the code and the instance name of your paddles maybe that might be the problem

    • Check to ensure that the name you gave to the instances are exactly the same as to those given during the tutorials. The names -are- CASE sensitive.
      For example:
      Instance Name: Playerpaddle – Code name: playerPaddle | This code will not work.
      Instance Name: PlayerPaddle – Code name: playerPaddle | This code will work.
      Remember to keep this in mind.
      Hope this helped! :)
      Jake.

      • Final-Z says:

        It is possible that you didn’t put the paddle movement in the loop section. Doing so would cause the paddles to freeze at frame 1, and then, no movement.

  3. mike says:

    How can you stop the cpu paddle from going past the edge of the board? Thanks.

    • Edvin Arge says:

      I’m no expert, but I don’t see why this wouldn’t work.

      //Check if top of the paddle is hitting the top of the screen
      if(cpuPaddle.y – cpuPaddle.height/2 stage.stageHeight){
      cpuPaddle.y = stage.stageHeight – cpuPaddle.height/2;
      }

      I just changed playerPaddle from earlier to cpuPaddle. Make sure your cpu paddle instance is named cpuPaddle

  4. Erwinson says:

    Your tutorials are very good.
    It really helps a lot especially to those who wanted to program.
    Keep it up!
    You’re so cool. :)
    Thanks a lot again.

  5. jeff says:

    Can you do a tutorial for people who want to make a 1v1 pong game(player vs player)?

  6. jinse says:

    So the cpu paddle can move out of the box or am I missing a piece of code?

    • LarsOlsen says:

      I’m no expert but i believe the cpuPaddle won’t move out of the box because it follows the ball constantly and therefor it will have no need to travel outside the box :P

  7. bibop says:

    very helpful! im glad u posted this :)

  8. Bumpf says:

    for the problem with the cpuPaddle moving out of the box, i added an additional condition for the cpuPaddle to be moving:

    if(cpuPaddle.y < ball.y – 10 && ball.y ball.y + 10 && ball.y >= cpuPaddle.height/2 – 10)
    {
    cpuPaddle.y = cpuPaddle.y – cpuPaddleSpeed;
    }

    //the +-10 comes from the difference in the first condition

    so far great tutorial

Leave a comment