Introduction: Pong in Scratch

This Project will give you the steps to recreate Pong, one of the very first video games, in the Program Scratch.

Supplies

The Supplies are very simple, all you need is internet access, a device that can access the internet, and an account for Scratch, the site used in this set of instructions.

Step 1: Introduction to Scratch

The first thing you have to do is access the website found at scratch.mit.edu. once on the site, use the Join Scratch option to create an account. Once this is finished, use the create option at the top left side of the window to create a project. This is where the fun begins. As a side note, Scratch can be a very finicky program, and may become unresponsive, so save often.

Step 2: Some Extra Information

When creating new sprites, always find the center point for of the creation area, marked by a center point. this will be the basis of the sprite creation process, and all X and Y values will assume the sprite to be create at this point. if this center point is not used, they values given will not work, and you will have to figure out the proper location.

Now, on to the creation.

Step 3: Creating the Center Line

To create your first sprite hover over the cat icon in the bottom right corner of the screen, this will give you four options. Click “paint”.

Using the rectangle tool draw a small vertical black rectangle. This will be the building block for the center line.

Set the sprite1’s x value to 0 and y value to 0. The rectangle should be at the top center of the play area.

Right click on the “Sprite1” icon and click “duplicate” Set the new sprite’s x value to 0 and y to 30 less (-30).

Repeat steps 5 and 6 until you have a dotted center line separating the play area into 2 zones.

Step 4: The Goal Posts

In this simple step, you will create the two goal posts.

Create a new sprite of a tall rectangle that spans the entire height of the play area. Rename this sprite to “P1-Goal”. Set the x value to -240 and y value to 0.

Duplicate “P1-Goal”, rename it “P2-Goal”, set its x value to 240 and y value to 0.

Step 5: Coding

From here on, you will need to use the coding tab at the top left of the page, just below the Scratch icon. there are a total of 9 types of code, but only use 7 of them in this project.

Step 6: Creating the First Player

Begin this step by copying one of the center lines and setting the X value to 210, and the Y to 0. Rename the sprite to Player1

Once this is done, coding begins. Ensure you are in the coding tab of the sprite in the top left.

Click the “Player1” sprite icon, in the top left corner click the “Code” tab

Under “Events” drag the “when (green flag) clicked” block into the integrated development environment (IDE).

Also under “Events” drag “when key pressed” blocks into the IDE. Under “Motion” grab the “go to x: position y: position” block and attack it under the “when (green flag) clicked” block Set the go to x position to -215 and y position to 0

Under “Motion” attach a “change y by” to the when key pressed and give it a value of 4

Under “Motion” “if on edge, bounce” attach to when key pressed

Under “Motion” “point in direction” block to “when key pressed” button.

Duplicate the enitre “when key pressed” and change the "change y" to -4

Set the desired keystrokes for the movement to “when key pressed block”

Step 7: The Code, and Player 2

Above is what the code should look like.

In the bottom right, where all the sprites are, right click the player and copy it.

Rename it to Player2. Once this is done, go into Player2's code and change the movement buttons, and the "Greenflag" movement to X:-210 Y:0

Step 8: Score Keeping

Create a new sprite in the bottom left, and name it "P1-Score"

In the costumes tab, use the text box feature to create a 0 at the center point. Copy this costume 5 times.

Change the other costumes so they are numbed 0-5 in order.

Click on the “P1-Score” sprite.

Select “Code” in the top left

Grab a “when (green flag) clicked” block and a “when I receive (message)” block, set the message to “P1-Score”.

Under “when (green flag) clicked” attach a “switch costume to (value)” block, set the value to 0.

Under “when I receive (“P1-Score”)” attach a “next costume” block.

Copy the "P1-Score" sprite and rename it to "P2-score"

Change the message received to "P2-score"

Set the X value of "P1-Score" to -130 and "P2-Score" to 130, and the Y to 150 for both.

In variables, create 2 variables "P1-score" and "P2-score"

Under the "when (green flag) clicked" block for each player, add "set (variable) to ()" and set the value to 0.

Under the "when I Receive ()" for each, add "Change (variable) by ()" and set the value to 1 and the variable to the corresponding player.

Step 9: Win Condition

Create a new sprite called “WinCondition”

Use the text tool a write “Player 1 Wins”, name the costume “P1-Win”

Add a second costume

Use the text tool a write “Player 2 Wins”, name the costume “P2-Win”

In code

Start with the “when (green flag) clicked” block, and attach a “hide” block (in looks)

Next grab a “when I receive (message)” block, set the message to “P1-Win”, to this attach a “show” block

Attach a “switch costume to (option)” block, set the option to “P1-Win” Now attach a “stop (option)” block, set option to all.

Right click on the “when I receive (“P1-Win”)” block and duplicate it. Change anything relating to player 1 to player 2.

Step 10: Ball Part 1: Start

Create a circular sprite, and name it "ball".

Start with the “when (green flag) clicked” block

To that attach the motion block “go to x: (position) y: (position)”, set both values to 0

Next attach an “if (operation) then …. else” block.

In the open “if” value attach a “pick random (value) to (value) = (value)” block and set the values to 1, 0, and 0 respectively.

Under the “if” section of the block attach a “broadcast (message)” block

Click the drop down menu, click “new message”, name this “P2-Hit”

Under the else portion of the “if (operation) then …. else” block attach a “broadcast (message)” block.

Create a new message “P1-Hit”

Step 11: Ball Part 2: Movement

Start a new block of code with a “when I receive (message)” block, set the message to “P2-Hit”

Next attach a “forever” block unless otherwise stated everything from here will be within the “forever” block.

Attach a “wait (value) seconds” block, set the value to 0.01 Attach

“move (value) steps” block setting the value to 8

Attach “if on edge, bounce” block

Step 12: Ball Part 3: Player Contact

Next grab an “if (operation) then” block, place a “touching (variable)?” block in the operation section of the if statement, setting the variable to “Player 1”.

Within the if statement attach “broadcast (message)”, “point in direction (value)”, and “stop (option)” blocks.

Set the message to “P1-Hit” Place a “pick random (value) to (value)" block under operators and place into the “direction (value)” and set the random value range from 45 to 135.

Set the option in the “stop (option)” block to “this script”.

Step 13: Ball Part 4: Score

Start a new “if (operation) then” block, populate the operation with a “touching (variable)?” block, set the variable to “P1-Goal”

Attach a “broadcast (message)”, set the message to “P2-Score”

Next attach a “go to x: (position) y: (position)”, set both positions to 0. Attach a “point in direction (value)”, set the value to -90.

Attach a “wait (value) seconds” block, set value to 1.

Next attach a “broadcast (message)” block, set message to “P2-Hit”.

Directly under “broadcast (message)” attach a “if (operation) then”

Set the operation to a “(variable) = (value)” block, set the variable to “P2-score” and the value to 5.

Within this if statement attach a “broadcast (message)” and a “stop (option)” block

Set the message to “P2- Win”, and the stop option to “all”

Under this if statement attach a “stop (option)” block, set the option to “this script”

Step 14: Ball Part 5: Copying

Right click on the “when I receive (“P2- Hit”)” block and select duplicate, click anywhere within the IDE to drop this new block of code.

In the new block of code change anything relating to player 1 to player 2 and vice versa, also change any “point in direction values” from positive to negative, everything else can remain the same.

Step 15: The Final Code for the Ball

Step 16: CONGRATULATIONS

if all things went according to design, you should have a working Pong game, made entirely in scratch.