Creating a multiplayer quick-reaction game

Lead Image © kudryashka, 123RF.com

Beat the Buzzer

The quick-reaction game provides an introduction to building simple circuits with the Raspberry Pi and controlling those circuits with ScratchGPIO, an advanced version of Scratch.

In this physical computing example, I'll walk through the steps for creating a two-player game to see who has the fastest reaction time to a randomly displayed light. The object of the game will be to see which player can react faster by being the first person to click on the buzzer. I'll create some controls to minimize cheating, and the game will keep score, so there will be a winner and bragging rights.

Besides providing a friendly game among two friends, this project will show you how to build simple circuits that can be detected and controlled with Scratch. You'll need a light-emitting diode (LED), two push-button switches, three 330-ohm resistors, a breadboard, and wire leads to connect the breadboard to the GPIO of the Pi.

You'll need to complete three main steps: Install ScratchGPIO5 to access the general-purpose input/output (GPIO) pins on the Raspberry Pi, use Scratch to send output to the GPIO pins to turn on a light, and detect input on the pins when a player presses the buzzer.

For this project, I recommend that you prototype the game on a breadboard to make the circuit prototyping easier and to avoid connecting circuits directly to the pins on the Pi. Incorrectly wiring something on the Pi can cause damage.

Installing ScratchGPIO5

The native installation of Scratch does not have the capability to control lights, switches, or motors via the Raspberry Pi. However, ScratchGPIO5, an extended version of Scratch by Simon Walters [1], does let you use the Raspberry Pi to control circuits.

When you install ScratchGPIO5, it will install a separate copy of Scratch to /home/pi/scratchgpio5 along with a unique desktop icon. Your original version of Scratch will remain installed and unmodified.

To install ScratchGPIO5, open a terminal window and run the following commands:

sudo wget http://goo.gl/Pthh62 -O isgh5.sh
sudo bash isgh5.sh

The first command (the switch is an "oh" not a zero) grabs the installation files, and the second command runs the installer. At the conclusion of the installation, two new icons, ScratchGPIO5 and ScratchGPIO5Plus, will be created on the desktop. The project in this exercise will use the ScratchGPIO5 version. The "plus" version has support for additional add-on boards.

Double-click the ScratchGPIO5 icon from the desktop to open the new version of Scratch. You should see a message indicating that the remote sensors are enabled. Confirm the message by clicking OK.

Wiring an LED Output

The LED will be the visual cue to the players to click their buzzers. This step will create the first circuit for the game and will also verify that everything is set up correctly.

As a test, connect pin 6 on the Pi's GPIO header to the ground of the breadboard. Then, connect pin 1 to the LED. To protect the Pi and the LED, put a resistor between the GPIO pin and the LED, as shown for pin 11 in Figure  1. The LED should remain lit. Pin 1 is a 3.3V power pin, which makes the LED always on. If the LED does not light, try reversing the LED leads on the breadboard. If problems persist, verify that your circuit is wired correctly on the breadboard and to the Rasp Pi. Figure  1 shows all the circuits in the project. Note that I use a resistor in all the circuits.

Figure 1: Circuits for the project.

To find the pins on the Pi, you should know that ScratchGPIO uses the physical pin numbering, so they go in the order they appear on the board. Pin 1 is marked and is closest to the SD card. All the odd pins are found in the low row of pins on the inside of the board. Pin 2 is adjacent to pin 1, and the even numbered pins continue down the long row of pins on the outside of the board.

For one more test, connect the LED to pin  11. Then, from Scratch, select File | Open. Browse to My Projects and select blink11 to load a one-script test project that came bundled with ScratchGPIO. When you click the green flag, the LED should blink on and off in one-second intervals.

At this point, everything should be working. If you see errors, and you're certain the circuit is wired correctly, then open a terminal window and run sudo python blink11.py. This step removes Scratch from the equation and should provide an error message in the terminal to help you troubleshoot further, if needed.

After you use the bundled troubleshooting scripts to verify you have a working ScratchGPIO environment, it's time to create a new Scratch project. The quick-reaction game will be a one-sprite project with multiple scripts. For now, I will continue to work with the default Scratch Cat sprite, but you can use any sprite you want.

Figure 2 shows an initialization script that runs when the green flag is clicked. The script assumes a two-player game and uses the ask() and wait block to prompt for each player's name, which is stored in variables named player 1 and player 2, respectively.

Figure 2: Initialization script.

The broadcast() block, although not a new block for Scratch programs, is being used in a unique way by ScratchGPIO. The broadcast pin11low message ensures that the LED is turned off at the start of the game.

When you use the broadcast() block in typical Scratch programming, you would also need to use a when I receive() block to instruct the sprites to listen for the broadcasts. However, in ScratchGPIO, these blocks are not necessary to control the output pins on the Pi.

ScratchGPIO expects broadcast messages to be in a specific syntax and automatically listens for the formatted broadcasts. If you look at the pin11low message, you can break it down into two parts. The first part is the pin number (i.e., pin11). The second part of the syntax is either low or high. This is one of the rare times in Scratch programming that you need to be aware of spelling, spacing, and syntax, so if the script doesn't work, double-check everything.

Setting the pin value to low will turn off the power, creating an open circuit, and setting the value to high will turn it on, creating a closed circuit. You can think of the low/high syntax in terms of the power output to the pin. Low sets the power to 0V (off), and high sets the power to 3.3V (on). ScratchGPIO uses pins 11, 12, 13, 15, 16, and 18 as output.

At this point, clicking the green flag should cause the light to go off, and the sprite should prompt for the players' names and assign them to the respective variables. The next step in the project will be to turn on the LED after a random number of seconds, so players can test their reaction times.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content