Back to: Scratch Programming
Pong
Whilst not the first computer game ever written, Pong was one of the earliest commercial games that appeared on consoles and in arcades. Released in 1972 by Atari, Pong became wildly popular and helped launch the video game industry. The original Pong consisted of two paddles that players used to volley a small ball back and forth across a screen.

Getting started
First we’ll need our sprites, a ball and two paddles, we’ll also make it look nice by adding a backdrop. From the sprite library, you can choose a ball and paddles. You can edit the paddle colours by clicking the paddle, then the “Costumes” tab. Here’s the sprites I have chosen, one of the paddles has been edited.

Coordinates
To implement any game, we must be very familiar with the idea of coordinates. Coordinates allow us to specify any location on the screen. Click your ball sprite and set the x and y co-ordinates for each to zero. The ball will move to the centre of the stage. A negative x value moves the ball left, a positive x value moves it to the right. A negative y value moves the ball down, a positive moves it up.

Moving the Paddles
We are going to link the paddles to keys to allow them to move left (left arrow <) and right (> right arrow) when each key is depressed. Select the bottom sprite by clicking it (which makes it active) and let’s start coding. Remember that code runs fast, so we need to make the code wait for us to make our move. We need the program to constantly check if a key has been pressed – we use the “Forever” code block to achieve this. Here’s the code blocks to move the bottom paddle:

Since the top paddle is moved in an almost identical manner, we simply repeat this code using A to move left and D to move right. Pick up this entire block of code and drag it over the top paddle sprite, and it will duplicate the code. You’ll need to edit the movement keys to be a & e.
Click the RED stop button the the GREEN start button – BOTH paddles should now be controllable.
Moving the ball
Select the “ball” sprite so we can start to code the movement of the ball. Move in a “When flag clicked” block to start then a “Forever” block. The code should look as follows:

We pretty much have a functioning game at this point, but the ball bouncing off the paddles at 180 degrees breaks the laws of physics. We need the ball to bounce in a reflective way. Mathematically this is 180 degrees minus the angle of incidence.

Let’s replace the “Turn 180 degrees” bounce instruction to:
for both paddles.
Scoring a point, variables and finishing the game
If the ball passes a paddle, then we need to add a point to the other player and reset the ball into play randomly. My top paddle is at y = 140 so if the ball moves higher than this, then the user loses a point and the ball resets to the centre. Similarly, the bottom paddle is at y = -140, so if the ball goes lower than this, it is a miss.
The following code will send the ball back to the centre and randomly serve it in the event of a miss.

After putting it all together, your code should look like this:

Variables and scoring
A variable in computer programming is a named container in a computer’s memory that stores a value that can change. Variables are essential to software programs, and are used to store and reference values many times in a program.
To make a new custom variable in Scratch, select the Variables option then “Make a Variable“:

We need to create a variable called “Red Score” and an second variable called “Blue Score“.


These variables will increment (go up in value by 1) each time an opponent misses the ball. After creating the variables they will be displayed on the stage, but can be hidden or changed in appearance.
Now we need to code the variables so that “Red Score” increases by 1 every time the ball goes above y = 140 and “Blue Score” increases by 1 when the ball goes below y = -140.
We can use existing conditional logic to code this but the first thing we need to do is set both scores to zero (0) at the start of the game. In programming terms we call this “initialising the variables“. These will go at the very top of the program.

We can use the existing lines of logic that checks:
“If y-position > 140 Then…”

“If y-position < -140 Then…”

The code so far:

Play with a friend and suggest improvements. This is your “alpha” release and some refinements are likely to be needed.
One small issue I immediately find annoying is that after losing a point, the next ball is served very quickly. We can fix this by placing a “Wait 2 seconds” control after the score incrementing code in each of the conditionals.
After you have made this change, you have a decent working game of Pong.
Challenge
The line of code “move 10 steps” determines the speed of the ball. Try changing this number in your code to see what happens.
To make the game more challenging, there are a few changes we can implement:
- Ask the players at the start of the game, how fast they want the ball to move (between 1 and 20)
- Make the ball move slightly faster each time it bounces off a paddle.
- Add a sound effect each time the ball hits the paddle.
Hint: create a new variable called “ball speed” (for this sprite only) and initialise it to 10, then place it in the “move ??? steps” code.
