Mission 6 Reaction Game

micro:bit
micro:bit
Raspberry Pi
raspberry pi

micro:bit

Here I challenged you to create the reaction game with separate buttons. I love these arcade buttons that you can buy online. By adding them to a cardboard box and hiding everything inside it makes the whole game look really cool!

The arcade buttons in action

Build

You will need

  • 2 arcade buttons of different colours
  • 4 crocodile clips
Four crocodile clips and two buttons

These crocodile clips are really small, smaller than the typical crocodile clip. I’m using them to fit everything into the picture! Longer crocodile clips will work too. Different coloured ones are fine too. The colours of crocodile clips don’t mean anything.

  1. Clip a crocodile clip onto each end of the arcade button.
Croc clips on button

2. Add the other ends to Pin 0 and 3V on the micro:bit

3. Do the same for the second button. You have to clip the 3V crocodile clip to the other crocodile clip – as it won’t fit on 3V.

The suspect can hold the button in their hand and press it or you could place them inside a box like this:

4. The micro:bit clips onto the top of the box so they can see the countdown. Don’t forget the battery! I stuck it on with double sided tape.

Code

Test the buttons

Before we start rebuilding our code, let’s just test the buttons work!

  1. Create a new project to test the buttons
  2. Try this code on the micro:bit with the buttons attached:

You will find digital read pin P0 under the Pins menu which is under the Advanced menu at the bottom of the menu list.

  1. Press A on the micro:bit then watch the screen. It should show 0 constantly until you press the arcade button attached to Pin 0, then it will show 1.
  2. Reset the micro:bit and Press B. It should show 1.

If all that works, then your buttons are attached correctly. If not, look at your setup and check you’ve got all the wires connected to the right place.

Code the buttons

For the arcade buttons to work the code is slightly different. We’re not checking button A and button B. We’re checking the pins the arcade buttons are attached to.

A digital pin has two states: 1 and 0. The pin is in state 1 when the button has been pressed.

For the code we just need to replace button A with digital read pin 0 and button B with digital read pin 1.

  1. On the MakeCode website, https://makecode.microbit.org/ you should see your past projects. Open the project for the reaction game that you’ve just done.
  2. Remove all button A is pressed and button B is pressed blocks
  3. Also remove the not blocks from the while loop

4. Drag 0 = 0 out of the Logic menu and drop it In the first diamond of the while loop

5. Do the same for the second diamond

6. Drop it onto the first 0 in the first diamond

7. Drop it onto the first 0 in the first diamond

8. From Advanced select Pins, drag out digital read pin P0

9. Drop it onto the first 0 in the second diamond

10. Change P0 to P1

Now in the If statements, instead of on button A press we have if digital read pin P0 =

11. From Logic drag out the block 0 = 0 and place it on top of true of the first if true then block

12. From Advanced > Pins drop digital read pin P0 onto the first 0

13. Change the second 0 to 1

14. And the same for button B press, we have if digital read pin P1 = 1

There we have it! Arcade buttons to hit instead of the micro:bit buttons. Really helpful for zombies with poor coordination.

Haha, just kidding – I left in the two bugs from earlier: putting a pause in to celebrate and player B cheating by holding down their button. Fix those bugs and you’ll have an awesome arcade button zombie reaction game 🙂

raspberry pi

raspberry pi

Build

For this expert level build you’ll need

  • 4 x male to female jumper wires
  • 4 x crocodile clips
  • 2 x arcade buttons
equipment needed

Each button needs to be connected to a data pin and a ground pin. On the Raspberry Pi the pins are called GPIO. I’m going to connect one button to GPIO 17 and GND. The other button will be connected to GPIO 18 and GND.

The Pibow case I have on my Raspberry Pi has labels for the GPIO pins. To find out which pin is which look up this GPIO diagram on the Raspberry Pi website: https://www.raspberrypi.org/documentation/usage/gpio/

  1. Shutdown your Raspberry Pi. Wait a few moments before unplugging the power cable.
  2. Add the female end of the male to female jumper wires  to GPIO 17 and any GND pin.
  3. Attach the crocodile clips to the male end of the jumper wires
  4. Attach the other end of the crocodile clips to the arcade button.

5. Attach the second crocodile clip to GPIO 18 and GND in the same way

In my photo I’ve attached the second button using spade connectors. These are more sturdy way of connecting arcade buttons. You can buy the spade connectors and crimp them onto a jumper wire yourself.

The suspect can hold the button in their hand and press it or you could place them inside a box like this:

Code

In the python code, we need a new library: gpiozero and instead of userEntry we’re looking at is_pressed on the buttons

  1. First, use this Test code to check if your buttons are working:
#import the libraries
import time
from gpiozero import Button

#setup the buttons
player1 = Button(17)
player2 = Button(18)

#check for button presses, always
while True:
    if player1.is_pressed:
        print ("Player1")
		time.sleep(0.1)
    if player2.is_pressed:
        print ("Player2")
		time.sleep(0.1)		
  1. Run the code and press the arcade buttons. The shell should print out Player1 when you press one button and Player2 when you press the other. If it’s not working check your wires are attached to the right pins. Remember not to remove or add wires when the Raspberry Pi is powered on.
  2. Now, let’s put in our countdown and don’t go back to the countdown until a button was pressed. We’ve got two loops here
  • While True which is a forever loop. This keeps running the game over and over again
  • While buttonPressed == False. We don’t want to go back to the countdown until a button has been pressed. In the previous game, the user has to press Enter to continue. In this game, there is no enter. So we need something to wait for a button press before going back to start.
#import the libraries
import time
from gpiozero import Button

#setup the buttons
player1 = Button(17)
player2 = Button(18)

#check for button presses, always
while True:
	#set button press to be false
	buttonPressed = False
	
	#countdown 
	print ("Ready")
	time.sleep(1)
	print ("Steady")
	time.sleep(1)
	print ("Go!")    
	
	#keep checking for button presses, until someone presses a button
	while buttonPressed == False:
		if player1.is_pressed:
			print ("Player1")			
			buttonPressed = True
		if player2.is_pressed:
			print ("Player2")				
			buttonPressed = True

There we have it! Arcade buttons to hit instead of the keyboard buttons. Really helpful for zombies with poor coordination