4 / Building Pong Details


As a class let's build the paddle. 

We will make use of motion, the "if" block and keyboard input.

A Moving Ball

Let's build a ball that moves across the screen. For now let's just have it start when we hit the green flag and not worry about stopping at the edge or bouncing.

We will look at forever loops, motion and ways to achieve timing.

Out of Bounds

When a player misses the ball and it goes past there paddle it is out of bounds and their opponent gains a point. Let's tackle this next. This will also require us to think about how to keep score.

We will be looking at using variables and how to sense the position of an object or two objects colliding.

Bouncing Ball

Okay, hang on to your seats folks. Here comes some math and geometry. You didn't think we could program a cutting edge 2D masterpiece like Pong without some serious number crunching did you?

We'll start with a ball moving towards the right of the screen at an angle a bit off from perpendicular. Then when the ball reaches the right side of the screen and if it hits our paddle we will make bounce. 

Let's start by looking at Scratch's "if on edge, bounce" block. Will this work for us?

Remember how reflection works? http://scienceworld.wolfram.com/physics/AngleofReflection.html

We will look at calculations with operators and variables.

How can we achieve this in Pong? Let's make use of a variable to figure out these angles. Then we can calculate the amount to turn.

Remember the three angles in a triangle add up to 180. So:

  180 = a + 90 + (direction - 90)

   a = 180 - 90 - direction + 90

   a = 180 - direction

Our new angle b then is the same as a (because angle of incidence = angle of reflection). And b in terms of Scratch directions is 180 + b.

  b = 180 + a

  b = 180 + (180 - direction)

  b = 360 - direction


The four formulas you need are:


Ball Bounces Off Sprite


Right Paddle


Left Paddle


Top Bar


Bottom Bar


Adding A Top and Bottom Wall

Having the ball bounce off the top and bottom of the screen.

Scoring System

Using variables.


Tying It All Together - Game Main Loop

Using a main loop to manage all other components. Having the game start properly. Initialization. Game play and scoring. End of game.

Finished Early? Bonus Features

Done early? How about adding another feature? One player style play against the computer? Or an "attract mode"? Other ideas?