Pong with ShaKey

Difficulty
Moderate
Steps
10

Have you ever played Pong? What about a game of Pong created by yourself!

In this guide, we will show you how to create a game of pong using the Scratch programming language. Then we'll use the ShaKey's inputs and shake feature to create a controller for the game.

Complete this guide to learn about basic programming concepts such as variables and loops. Take your coding skills to the next level.

Parts Used in This Guide

ShaKey Standard Kit
LB-SHAKEY-STD

Buy

Step 1  Create a sprite for the ball

  • First, open up the Scratch editor and start a new project. 
  • Remove the cat sprite by clicking on the little cross icon on 'Sprite1'
  • Hover your mouse over the cat icon and click on 'Paint'
  • Create a sprite for the ping pong ball.
  • Rename it as 'pong-ball'

Step 2  Create paddle sprite

  • Let's create the sprite for the paddle now. 
  • Hover your mouse over the cat icon and click on 'Paint'. 
  • Then create a sprite for the paddle. We've used a simple black rectangle here.
  • Rename it as 'paddle'

Step 3  Create enemy sprite

  • Next, we'll create the enemy sprite. 
  • Remember to rename it to be 'enemy'.

Step 4  In-game Backdrop

Choose a backdrop for the game!

  • Hover over the 'Choose a Backdrop' icon. 
  • Here, we've chosen 'Neon tunnel' but feel free to pick whichever you want.

Step 5  Game over backdrop

A second backdrop is needed. 

  • Create the 'Game Over' backdrop. 
  • Here, we have created one using the same backdrop 'Neon tunnel' but with the words 'Game Over' over it.
  • Rename this backdrop 'Game Over'

Step 6  Program the sprite with Scratch

  • Now for the fun part: We'll program the pong sprite. 
  • So, click on the pong-ball sprite and then 'Code' tab. 
  • Copy the code blocks as shown in the three images.

We created a variable, 'score' here. A variable can be thought of as container that can hold information and pass it to other blocks in your Scratch program.

We also used control blocks, such as 'forever', 'if ... then ... ' and 'stop ... '. These blocks control the flow of the program, and set conditions. So, we have set up a loop that will repeatedly change the score by 1 if the condition 'touching paddle' is met.

Another condition we have set is to switch the backdrop to 'Game Over' and stop the program, if the condition, 'touching enemy' sprite is met.

Step 7  Program the paddle

Next, we'll program the paddle so that when the left arrow key is pressed, the paddle will move by x -10, else if the right arrow is pressed, its position along the x-axis will change by 10.

When the game starts, the paddle sprite should be at x: -1 y: -1.  

We also want it to be hidden in the 'Game Over' backdrop.

Step 8  Program the enemy

Finally, we'll program the enemy sprite! Copy the code blocks as shown. 

On start, the enemy sprite will be at position x:30 y:120. 

It will randomly appear at different points of the game. Once the pong ball touches it, it's game over.

Step 9  Remap ShaKey using Arduino sketch

Let's make the game a little more interesting! We'll program the ShaKey's shake feature. Before we can do so, we'll need the Arduino IDE set up for ShaKey.  Follow these steps:

  • If you haven't done so yet, check out Set up Arduino IDE for ShaKey
  • Then to get the ShaKey's 'shake' feature working with Scratch, download this sketch: ShaKey-remap.zip
  • Verify and upload the 'ShaKey-modified.ino' file to the ShaKey.


Step 10  Program the ShaKey's shake feature

We'll now program the ShaKey in Scratch. 

We want to make it so that when you shake the ShaKey, the pong ball will be momentarily invincible. If it's moving toward the enemy sprite, shake it so that It will teleport to a different location!

Add the following code blocks:

  • 'if key s pressed? then' 
  • 'go to x: pick random -150 to 150 y: pick random 150 to -150' 
  • a 'wait 1 seconds' block.

Download the complete Scratch code here: Pong-with-ShaKey.sb3