Tuesday, 18 March 2014

Spud Game

My School was lucky enough to win 5 Raspberry Pis in last Autumn's Hour of Code promotion and I thought it'd be a good idea to introduce them to my CodeClub posse.

I'm a big fan of Raspberry Pis - I've got several doing various jobs at home - but I've always wondered just how well they'd fit into a Primary School environment. The biggest draw for many youngsters is the ability to interact with the Minecraft Pi Edition through code. But my Codeclubbers are still using Scratch and so for their first taste of Raspberry Pi I wanted to stick with something familiar.

So there is the dilemma: running Scratch on the Pi by itself doesn't really offer any advantages - in fact if they connect one up to the school's ICT suite PCs, the only difference they'll probably notice is that Scratch runs more ponderously. Will that get them excited about Raspberry Pis?

I needed a project to really demonstrate what (for me) is the big plus point of Pis: the ease with which you can connect and interact with external devices. A simple idea would be to connect the GPIO to some breadboarded LEDs and make them flash with Scratch code, but that seemed a little tame. So in the end I decided to make use of an Adafruit capacitive touch sensor which I'd got from Pimoroni and hadn't really put to good use yet.

Fortunately ARGHBOX have already produced a great tutorial for using the sensor to interact with a set of potatoes! So using that as a starting point I created a simple "Simon says" type game for the Pi using Scratch.

What you'll need

A Raspberry Pi (obviously)

An Adafruit touch sensor

A breadboard

5 x 10K resistors

Some wires

Some potatoes

A Pi Cobbler

Software

The latest and updated version of Raspbian (this will probably work on other distros but I did not test any others).

ScratchGPIO5 from cymplecy (the default version of Scratch that comes with Raspbian doesn't know how to talk to the GPIO pins).

Assembly

The setup is almost identical to ARGHBOX's but I used different GPIO pins so that it would work more easily with the sensor inputs made available by default in ScratchGPIO5.

The cymplecy site has some great documentation for using Scratch with GPIO inputs and outputs, however the key elements of my Scratch game are quite straightforward.

First of all you need to set the relevant GPIO pins to act as inputs. This is achieved by sending a broadcast message of the format shown below.

Then the main code that interacts with the spuds simply checks for a change in the input value and then acts accordingly.

The game

The player presses the S key and the potatoes on screen flash in a random sequence.

The player then presses the space bar and gently taps the real spuds in the same order as they flashed on the screen. The spuds say "Oi!" when they're tapped and the entered sequence is displayed in the 'answer' variable on the screen.

If the player remembers correctly then they see the WIN screen, if not, the big fat LOSE screen awaits them!