2. Other Projects‎ > ‎Raspberry Pi‎ > ‎Pi Blog‎ > ‎

Scratch Monkey Bonk Game for Raspberry Pi

posted 9 Dec 2016, 13:40 by Andy Lakin-Hall   [ updated 9 Dec 2016, 13:41 ]
With my first Raspberry Jam as an exhibitor coming up I decided to develop this Scratch game for the Raspberry Pi. Now I can access the GPIO for Outputs and Inputs, I wanted to try these out for real.

The game already has multiple options for input. You can use the mouse to click on the monkeys, or hit the cursor keys to bash them down. The cursor keys mean the game can be controlled by a MakeyMakey - which is fun. I'm working on a conductive tray that can hold playdough or fruit which you can tap to make the monkeys go down. I'll post about that later if I can get it to work.

I'm building this circuit on my FUZE with the handy IO ports and breadboard. The first change I wanted to try was to have four LEDs that would light when a monkey was rising.

Start with a broadcast to make sure the gpio server is on: broadcast gpioserveron

Next the LEDs connect to GPIO pins 0 to 3, and I know to get these to work as outputs I have to configure them first.

config17out
config18out
config27out
config22out

I added in commands to turn each pin on and off as required, including a full set of off commands at the end of the game to make sure everything is actually off at the end of the game.

gpio17off
gpio18off
gpio27off
gpio22off

With that part working, the next step was to include switches beside each LED that you can press to make the corresponding monkey go back down.

This was slightly trickier, as I had to first identify the right codes for GPIO lines 4 to 7, but I figured these out.

config23in
config24in
config25in
config4in

I also had to work out the right circuit to connect the switch. This turned out to be easier than I thought it would be; just a lead from the GPIO pin to the switch, and then another to GND. The pin floats high, and then goes low when the buttons is pushed.

Here's the components arranged on the board.

Within each monkey, I added the command to turn on the appropriate GPIO pin. This is Monkey 1, so its code is gpio17on

The code for detecting the event that makes the monkey descent has got rather complex now, as I want it to work for a push button as well as cursor keys and mouse click.

The most important bit is the gpio23 sensor value. Once you've configured a pin as an input, it appears in the Sensors block.

If the button is pressed, gpio23 goes low. This triggers the Squeak2 sound and the bonked flag variable is set to TRUE.

Next comes the command to turn off the corresponding LED gpio17off.

Finally, when the GAME OVER broadcast is received, the monkey descends and the LED off command is sent once again. broadcast gpio17off

The full game can be downloaded below.

https://www.dropbox.com/s/1k4eb22n0s7rro7/Monkey%20Bonk%20RPi.sb?dl=0


Comments