Controlling the computer paddle

Now we need to control the computer paddle. It will be moving up and down the y-axis and trying to follow the y-coordinate of the ball.

Watch the first video to see a good technique of how to do this.


Now that we can move the computer paddle at a constant speed, we need to find a way to vary the speed of the ball. We know that:

  • if the ball is the same speed as the computer paddle, the computer will always win.
  • If the ball is too fast, the computer will lose quickly.

If we can vary the speed of the ball during game time, the game will be challenging for the player and the computer.

Challenge!

Create the following game now that we have a moving Computer Paddle and a player-controlled Player Paddle:

  1. At the start of the game the ball has a width and a height of 10 pixels
  2. In the game loop, every time the ball hits the player paddle, it increases in size by 5 pixels
  3. Also, every time the ball hits the computer paddle, it increases in size by 5 pixels
  4. If the ball height becomes greater than 50, the ball height and ball width reset to 10 pixels.

Here is an example fo what you will create: