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!)
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