Creating a multiplayer quick-reaction game

Creating a Buzzer

To see which player has the faster reaction, you need to wire two buzzers to the breadboard. The Scratch program will control when the players are allowed to press the button, detect the first buzzer press, and keep score.

For the buzzer, I'll use two simple push-button switches, one for each of the two players. Player one's buzzer will connect to pin 7, and player two's will connect to pin 8. (See Figure 1 for the buzzer circuits.) To detect the buzzer clicks, the Scratch script needs to monitor the input on the Raspberry Pi's pins. ScratchGPIO treats pins 3, 5, 7, 8, 10, 19, 21, 22, 23, 24, and 26 as input.

The two scripts in Figure 3 show the main logic for the game. The script that begins with the when host clicked block starts the game. I added a new variable called enable buzzers? and assigned it the value no, to prevent players from buzzing in too early.

Figure 3: Main logic scripts.

The repeat() loop uses a variable called match length to control how many matches or sets each game will have. This approach allows the players to achieve the best two out of three, for example. This setup can be controlled by a slider control on the stage.

After waiting a random amount of time from 1 to 5 seconds, the script enables the buzzers and then turns on the LED by broadcasting a pin11high message. When the LED lights up, that's the signal for the players to buzz in. The broadcast(show light) and wait block coordinates the programming needed to detect the players' buzzers.

The when I receive(show light) script in Figure 3, detects the pin input. The forever loop ensures that the script will continue running until either player one or player two clicks a buzzer. The ()sensor value block has drop-down options for the ScratchGPIO inputs.

Additionally, the first if() block checks whether pin 7 (player 1) is on and whether the enable buzzer? variable is equal to yes. If both conditions are true, the script increments player one's score, disables the buzzer, and stops the current script. The second if() block performs the same check for pin 8 (player 2).

The input pins have a default value of 1 in the "off" position. When input is detected, such as clicking the buzzer, the value of the pin becomes 0. You can see this by enabling the pin7 sensor value in the stage monitor.

Disabling the buzzer prevents the other player from buzzing in and getting a point. Stopping the script breaks the forever loop and signals the when host clicked script to pick up where it left off, which will be to run the broadcast(pin11low) block to turn off the LED. The advantage of using the broadcast(show light) and wait block is that the script will pause until all the when I receive(show light) scripts complete.

Then, the script continues to randomly light the LED for the specified number of matches. At the end of the game, the program declares a winner with the broadcast(declare winner) block at the end of the when host clicked script.

The when I receive(declare winner) script in Figure 4 determines which player has the highest score and then announces a winner.

Figure 4: At the end of the game, the program declares a winner.

Extending the Game

For now, the sprite announces the player with the quicker reaction time, but you could add additional LEDs for each player and use the LED as a physical signal for the first button click.

The Author

Michael Badger wrote the Scratch 1.4 and 2.0 Beginner's Guide series from Packt Publishing. Learn more at

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content