Build a Raspberry Pi Scratch Game of Chance
2022-12-22 | By Maker.io Staff
License: See Original Project
You’ve probably flipped a coin to make some sort of trivial decision with your friends, but what if we told you that you can make that coin flip a bit more interesting? Or that flipping a coin could be turned into its own game?
In this project, we’ll show you how to get away from the manual approach to flipping the coin and instead use an embedded computer-based game that can be realized with the Raspberry Pi and a Sense HAT. Think of this as flipping the Coin 2.0: you’ll be guessing when a cat sprite lands on its feet. Even if you don’t use it to replace your trusty favorite coin, it is still a fun project to work on!
Read below to learn more!
Reinventing the Coin Flip with the Gyro-Cat Scratch Game
Building the Gyro-Cat Scratch Game requires two main electronic components: a Raspberry Pi and a Raspberry Pi (RPi) Sense HAT. The Sense HAT uses an 8x8 matrix of addressable LEDs that can display various characters, scrolling text, and numbers. With an 8x8 matrix, the Sense HAT provides 64 pixels for displaying visual information.
The RPI Sense HAT.
Interacting with the Gyro Cat will require the player to start the flip using a joystick. The RPi Sense HAT has a small joystick at the bottom right corner of the printed circuit board (PCB). By pushing the RPI Sense HAT joystick in the upward direction, the Raspberry Pi will move the Scratch cat sprite in an upward spiral motion on the screen of the attached monitor.
The RPI Sense HAT joystick.
The Raspberry Pi single-board computer (SBC) will provide the visual and audible effects of the game using an attached monitor and speakers. The joystick position data will be transferred to the Raspberry Pi from the RPi Sense HAT through the 40-pin connector. The Scratch cat rotational movement will be displayed on the attached monitor using the Scratch blockly code programming language. There is a cat meow sound produced using the Scratch sound code block.
The assembly of the Raspberry Pi and the RPi Sense HAT is illustrated below, and you can see the parts list for this project in the following Scheme-it link: https://www.digikey.com/schemeit/project/gyro-cat-scratch-game-fde79a6d1217495aa79c45d5c5f76002
The red transparency is added to make the scrolling messages easier to see.
The assembled Raspberry Pi Sense HAT game console
Using the Scratch Blockly Code
This Gyro Cat game software is built using Scratch, which is included with the Raspberry Pi operating system. The game was developed in three sections: the Start-player, the Timer, and the Reset player. The Start-player code allows the game to be initiated by clicking the on-screen green flag in the stage window.
With the green flag clicked, the title of the game scrolls across the LED matrix, as shown below.
The Gyro Cat scrolling title feature
Within the code structure, there is a conditional statement that looks for the joystick being pushed upward by the player. When the joystick is pushed upward, the cat will move and turn in a 10-step, - 90˚, and 30-step sequence. The code used for checking the joystick position and sequencing the Scratch cat movements is captured within a loop. The loop will run continuously until the player clicks the on-screen red stop sign.
Below are the code blocks for the Start-player game section. The sequence can be modified to provide new sprite movements for the game by changing the move and turn code blocks.
The Start-player code blocks
The Timer section consists of code blocks that allow a 10-second window of play within the game. The timer is initiated by the player tapping the spacebar on the keyboard. The word Go is displayed on the Scratch stage, as shown below.
Using the joystick, the player will move the Scratch sprite in a spiral matter. An up-release motion with the joystick will allow the spiral motion to occur. Upon the 10-second timer elapsing, the Game-Over message will scroll across the RPi Sense HAT LED matrix, and a cat meow sound will then be played. The Timer code blocks are shown here:
The final section for the Gyro Cat Scratch game is the Reset-player mode. This section of code will allow the player to start a new game session by placing the Scratch sprite back on its feet when the joystick is pressed down. The sprite will be oriented 90˚ and placed at XY coordinates of (3,29). The Reset-player code is shown here:
The final console and operation of the Gyro-Cat Scratch game may be viewed by watching the video here:
The Rules for your New Game
1. Click the green flag
a. Gyro-Cat name will scroll on the RPi Sense HAT LED matrix
b. The RPI Sense HAT LED matrix will clear
2. Click the spacebar on the keyboard
a. The sprite will say (display) “GO”
3. Move the joystick on the RPi Sense HAT to spin the Scratch sprite
a. Periodically push up-release the joystick
b. See if the Scratch sprite is standing on his feet
4. Count (record) the number of times the Scratch sprite lands on its feet within the time allotted
5. When the time is up, the “Game-Over” message will scroll across the RPi Sense HAT LED matrix
a. A cat meow sound will be heard
b. The cat meow sound signifies the game is over
6. The player with the highest count wins the game
So, what do you think? How do you like the new approach to flipping a coin?