Introduction: How to Create a Scratch Game With Makey Makey Controller on a Raspberry Pi

About: Software/hardware enthusiast, hacker, maker, father, husband, vim devotee, git lover, Node.js afficionado, SA. Currently working on: @timecounts, @MakeSoton

In this instructable you'll learn how to make a simple game in Scratch on a Raspberry Pi; to make the game more fun you will be controlling it using real world objects (fruit and vegetables) via a Makey Makey.

You will need:

  • A Raspberry Pi mini computer
  • An SD card or MicroSD card (depending on which Raspberry Pi you have) formatted with the latest version of Raspbian
  • A USB hub (only if you're using a Raspberry Pi with fewer than 3 USB ports)
  • A 1A+ 5V MicroUSB power supply for the Raspberry Pi (a MicroUSB cable plugged into a laptop or computer may suffice)
  • A HDMI cable
  • A HDMI display (for example television or monitor)
  • A Makey Makey kit
  • 4 items of fruit or veg
  • A USB keyboard
  • A USB mouse
  • Optional: a USB wifi dongle
  • About 30 minutes (less if you're fast!)

Step 1: Connect Everything Up

Insert the SD/MicroSD card into the Raspberry Pi. Plug all the USB devices into the Raspberry Pi. Plug the HDMI cable into the Raspberry Pi and the TV/monitor. Finally connect the power cable to the Raspberry Pi - it should boot to raspi-config - see next step.

Connect crocodile clips to the left/right/up/down pads and the earth pad of the Makey Makey. Connect the other ends of the up/down/left/right crocodile cables to the fruit/veg - this can be done by simply stabbing the connectors into the fruit/veg but I find it's more reliable if you open the crocodile jaws before doing so - the connection seems stronger and the connection is more permanent. Leave the other end of the earth cable loose.

You should find that if you hold the metal of the earth connector with one hand and touch one of the fruit/veg with the other hand that the relevant directional light of the Makey Makey should light up, as shown.

Step 2: Load Scratch

You can tell the Raspberry Pi to boot into Scratch directly every time you turn it on using the raspi-config menu that pops up on first boot (as shown). Once you've done so select finish and then accept the request to reboot. If you boot into scratch the screen may be blank to start with - just press any key to wake it up.

Alternatively you can go with the graphical option and then load Scratch from the Applications menu.

Step 3: Create Global Variables/lists

The bottom right area of Scratch is called the "Stage." From the stage, select the first sprite (the cat) and rename it to Game using the input field at the top of the screen.

From the variables tab on the left create 4 variables "For all sprites" - expected, selected, right and wrong. Then from the same area create a list called "Objects", also "for all sprites."

Untick all the variables except right and wrong - they're the only ones we want to see on the screen.

Step 4: Configure the Cat and Save the Game

Configure the cat as shown by dragging blocks from the left hand side into the "Scripts" section. The colour of the block tells you which section it comes from, you may have to scroll to find the relevant block.

I've made three chains from these blocks; some of this may not make sense yet as it requires the next step to work:

  • the first one resets the "right" and "wrong" score counters when you click the start flag and then initiates the first turn
  • the second one says what happens on each turn - we first delete everything from the "Objects" list (NOTE: this isn't in the first picture!), then we broadcast "store", wait, then pick a random item from "Objects" and store it to "expected"; then we announce the object we're expecting (also not in the first picture) then finally we broadcast "go"
  • the final chain says what happens when the user touches one of our objects (fruit/veg) - if it was the first touch of the game then the timer is reset (for competitions! Tick the timer so that it is visible); then if you selected the expected object we increase your "right" counter by one, otherwise we increase your "wrong" counter by one; finally we trigger the next turn to start

Of course it's a good idea to save your work, to do so click the save button, set your author name and pick a filename for your game.

Step 5: Create Object Sprites

We now want to create a new sprite for each of our objects (fruit/veg); to do so press the left hand "New Sprite:" icon in the "Stage" and draw your object in the dialog that appears. Once you're happy, press "Okay" and then rename it like we did for the Game sprite. As you can see, I'm clearly a very skilled artist.

Drag the various items around in the canvas at the top right so that they're laid out nicely - I lay them out to match the up/down/left/right keypad layout.

Step 6: Configure the First Object

Select the first object (lemon) from the Stage. Create two variables for it, both "for this sprite only", call them "index" and "object". Drag the blocks into the script area as shown to configure the object.

  • When "store" is received, add myself to the "Objects" list
  • When "go" is received, pick a random number between 1 and the number of items in Objects and call it "index"; store the object at index "index" in the Objects list to the variable "object"; and then remove that object from the list; finally say which object I picked
  • When the left arrow key is pressed, set the global variable "selected" to the object that I picked above; then broadcast that an object was touched

Step 7: Configure the Other Objects

With the first object (lemon) selected, drag each of the chains in turn onto each of the other objects in the stage. This will copy over the configuration. Once this has been done (dragging 3 blocks for each of the 3 objects -> 9 drags) select the second object and tweak what's added to the Objects list (e.g. change "lemon" to "tomato") and which key triggers this object to be selected (e.g. change "left arrow" to "down arrow"); then repeat for the other two objects.

Step 8: Play!

Don't forget to save your progress!

Press the full screen button at the top right; then press the green flag. You should now be able to play the game!

To play the game, you must find and touch the fruit that claims to be the fruit that Scratch the cat is saying. For example here Scratch says "banana" and the tomato is claiming to be a banana, and thus you must touch the tomato!

Scoring: up to you, I play timed games where every correct answer is worth a point and every wrong answer is worth negative two points; participants have 60 seconds (from the touch of the first fruit) to score as many points as they can - whomever scores the most is the winner!