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
For more detail: How to create a Scratch game with Makey Makey controller on a Raspberry Pi