Ping Pong

It's time to go back in time, and recreate a game that helped to launch the video game industry. It has gone by many names such as Pong, Ping Pong, and Table Tennis, but the basic gameplay has stayed the same. Two paddles, one ball, and an epic battle to get the ball past the other player.

So, let's break down this game. We will require:

  • One paddle for the left player
  • One paddle for the right player
  • One bouncing ball
  • Two variables to keep score for each player.

Let's get started!

Making the paddles

After creating a new project, you will be presented with the white stage and a Scratchy sprite.

  1. Right-click on Scratchy and click on delete. Now, click on Paint new sprite.
    Making the paddles
  2. Draw a long black rectangle and click on OK. Name this sprite Left.

    We are going to create three simple scripts for this paddle, as follows:

    Making the paddles
  3. The first script ensures that the left paddle points in the proper direction and then sets its size to 60 percent. You should experiment with the size to determine what feels right for your game.
  4. The next two scripts control the vertical movement of the left paddle. Again, experiment with the amount of movement that each button press does to change the difficulty of your game.
  5. When you have set these up, right-click on your left paddle sprite and click on Duplicate.
  6. Rename the new sprite to Right. We now have the beginning for making our right paddle. You will be making a few adjustments, so that a second player will control the paddle.
    Making the paddles

We change the direction in which the paddle is pointing to ensure that it faces the right direction. You then need to change the keys that you will be using for the second player to move the paddle up and down. In this example, we use the up and down arrow keys.

On the stage itself, drag and drop your paddles into the proper positions. The left paddle should be on the left-hand side of the stage, but not touching the wall, while the right paddle should be on the right-hand side, while also leaving a gap between the paddle and the wall.

When you have made your choices, click on the green flag button and test your game. The paddles should be able to move up and down.

Following the bouncing ball

Now, it's time to create the heart of the program: the ball!

Following the bouncing ball

To get started, let's create a new sprite:

  1. Start by importing a sprite from the library. There are numerous round ball-like costumes to choose from. When you are happy with your costume, double-click on it. Rename your new sprite to Ball.
  2. Now, you will need to make two variables. Name them Left Score and Right Score and set them to be used by all sprites.
  3. When the green flag is clicked, set the size of the ball to 20 percent, and set both left score and right score to 0.
  4. Move the ball to the center of the screen, and use a new block to point the ball in a random direction. Random numbers are like picking a number out of a hat. You don't know which one you will get ahead of time. Using a random block, we can give Scratch two numbers, a small number and a large number, and it will pick a random number in between these. By making the ball point in a random direction, we add to the challenge and skill level of the game.
  5. Now, onto the forever block. Each time it loops, we want to move the ball 5 steps. We now have another new block, If on edge, bounce. This block tells the ball that if it has touched the outer edges of the screen, it should bounce away from it.
  6. We now have two if statements. These come into effect if the ball touches either the left or the right paddle. If it touches either, we have the ball point in the opposite direction, and then pick a random direction to move in.
  7. The last two if statements are the most important ones. These check whether the ball has gone behind either of the paddles. If it has, it awards a point to the other paddle, and moves the ball back into the middle of the stage. The game then continues!

You now have the beginning of your Ping Pong game! Where can you go from here? Here are a few ideas:

  • Add a background that represents a tennis match.
  • Have the game end when one side scores enough points.
  • Have the computer play as one of the paddles.

The rest is up to you! Have fun and experiment!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.43.126