dark mode light mode Search Menu
Search

Build a Raspberry Pi Game

I was asked by element14 to use the Raspberry Pi to reinvent the whole concept of traditional board games. Here is what I came up with, a fun board game alternative to digital only games.

The Raspberry Pi Model B+ has been with us for a few months now and there are a growing number of projects that make use of the extra GPIO pins. The GPIO (General Purpose Input Output) are a series of pins that enable common electronic components such as LED (Light Emitting Diodes), button and switches to be connected to your Raspberry Pi. Using programming languages such as Python and ScratchGPIO we can use these pins to control real world components.

Raspberry Pi B+ Board
RaspberryPi B+ Board

About This Project

For this project we will look back to our childhood and bring a much loved game from our past into the future. The humble board game.

Board games have been a traditional family pastime for many generations but with the rise of computer games their novelty has started to dwindle. These card and paper based games have little to offer the children of today who have been brought up on a diet of downloadable content packs and gamer scores.

But what if we could take a game from yesteryear and adapt it using the Raspberry Pi?

Meet the latest interactive board game…Pythons and Resistors:

Finished Raspberry Pi Board Game
Finished Raspberry Pi Board Game: Python and Resistors

This project is split into three parts

  1. Assembling the board game
  2. Assembling and testing the electronic components
  3. Writing and testing the Python code

Project Components

To build this project you will need the following equipment.

  • Raspberry Pi B+
  • Breadboard
  • 10 x Reed switches
  • 6 x 82 ohm resistors
  • 6 x LED 1.8v and 3.3v
  • 30x Female to male jumper cables
  • 20x Male to male jumper cables
  • 1 x Button
  • Snakes and Ladders board game file (provided below, as an image)
  • Thick card or cardboard
  • Sugru for player pieces
  • Magnets to be inserted in to the player pieces. Neodymium are the strongest

Game Rules

The board game is based on a simple snakes and ladders setup, with 100 squares in total via a grid of 10 x 10 squares. The object of the game is for 2 or more players to roll a dice and move their game piece to match the number given on the dice. If the player lands on a Python’s head, then they will slither down the game board to the tail of the Python. If the player lands on the bottom of a Resistor then they will climb up the game board. The winner is the first player to reach square 100, which is at the top left of the board.

1. Assemble the Board Game

The board game components consists of

  • The game board, printed on glossy photo paper
  • Sugru rubber to create game pieces
  • Neodymium magnets to trigger the reed switches
  • Spare components to decorate the game pieces.
  • Cello tape
Python and Resistors Board
Python and Resistors Board

Start by printing at least two copies of the game board as it is easy to make a mistake when assembling the game. While the game board printouts are printing, open a pack or two of Sugru and create game pieces.

Sugru is a form of air curing rubber that starts life as a sticky ball with a similar consistency to blutack. Form the Sugru into the shape that you wish. When you are happy with the shape add about three neodymium magnets to the base of the piece ensuring that they are gripped firmly by the sugru. Also you may wish decorate the game pieces with spare components such as LED or resistors.

Use Sugru to Create Game Pieces
Use Sugru to Create Game Pieces

2. Assemble and Test the Electronic Components

With the game board and player pieces completed our focus turn to the wiring and components that are under the board.

Reed Switch

At the head of every Python and the base of every Resistor we are going to place a reed switch on the underside of the game board. These reed switches will be triggered by the magnets inside of our game pieces. A reed switch is a tiny switch contained inside of a glass vial.

The contacts that form the switch mechanism come normally open so a current does not flow through the switch. By placing a magnet near to the switch we can trigger the contacts to quickly make contact, with a small audible click / pop being heard when this is the case. When contact is made the current will flow through the switch and in this case the current will flow to a GPIO pin on the Raspberry Pi which has been configured to be an input and react when this happens.

The best place to start is by fixing the reed switches into place on the underside of the board. To easily locate the correct spot I used a magnet on the printed side to trigger the reed switch on the underside. Once found I simply used cellotape over the glass vial of the switch to fix it into place. Repeat this action for all of the reed switches for all of the pythons and resistors.

Use Cellotape on Switches
Use Cellotape on Switches

Jumper Leads

With the switches in place we must now attach jumper cables to each end of the reed switch component. In one end of the switch we will supply 3v3 power from the Raspberry Pi 3v3 pin and the other will be connected to the corresponding pin. It doesn’t matter which side that you choose, just make a note of which side you will be using as the 3v3 supply as we will go into more detail on the wiring for this later in the guide.

For these connections use a female to female jumper lead and slide the connections over each end of the reed switch then use cellotape to secure them firmly in place. Do this for all of the reed switches.

Connected Jumper Leads
Connected Jumper Leads
Reed Switch Example
Reed Switch Example

Wiring the 3v3

The Raspberry Pi can supply 3 volts, commonly referred to as 3v3 from multiple pins on the GPIO. However, for this project there are more switches than there are 3v3 pins on the Raspberry Pi so we must break out the supply using a breadboard.

A breadboard is a great prototyping tool that enables components and circuits to be tested before soldering. Typically a breadboard has two sets of two rails which are on the outer most reaches of the board. These rails are marked + and — and refer to a positive voltage, in this case our 3v3 supply from the Pi and a negative voltage which is commonly called Ground, often shortened to GND.

Any connection made on a rail is available to all of the holes in the same rail as the supply. So a 3v3 supply from the Pi connected to the + rail will be available to all other holes in the + rail.

For this project we need to connect the 3v3 supply from our Raspberry Pi to the + rail of our breadboard. 3v3 is supplied via pin 1 of the Raspberry Pi, which is the top left pin nearest the micro SD card slot. Connect pin 1 of your Raspberry Pi to the breadboard, and then using Male to Male jumper leads connect the reed switch 3v3 connections to the + rail of the breadboard.

3v3 Reed Switches Layout
3v3 Reed Switches Layout

Connecting the switches to their respective GPIO pins

In this project we have used a number of reed switches which are triggered by magnets and supplied 3v3 from the Raspberry Pi. When a magnet is placed over the reed switch, the switch will close and supply 3v3 from the Pi, through the switch and back to the Pi via another wire connected to the other side of the reed switch. On the Raspberry Pi we will configure the receiving pins for each reed switch attached to each Python and resistor square to wait for a signal, in this case the voltage change and when it receives it will trigger an audio file to play. To connect our switches to their corresponding GPIO pin, please refer to the table below.

Connecting our Push Button

With the reed switches complete, we now need to install the push button that controls the roll of our dice. Our push button is connected from the 3v rail of our breadboard and to the GPIO 13 pin, which is pin 33 using the logical numbering layout.

The best way to ensure a good connection is to solder the wires directly to the push button. If you can’t solder it is easy to learn but should be attempted with an experienced maker and with safety equipment present. There are many great videos on Youtube to help your learning, including one from Carrie Anne at the Raspberry Pi Foundation

You can wind the wire around the terminals of the button for a temporary “no solder” test.

Push Button Example
Push Button Example
Push Button Connected to Breadboard
Push Button Connected to Breadboard

Pythons GPIO Map

The Pythons in our Pythons and Resistors game need to be attached to specific GPIO connections:

Board Square   Broadcom GPIO Numbering
38 24
47 25
68 8
99 7

Resistors GPIO Map

The Resistors in our Pythons and Resistors game need to be attached to specific GPIO connections:

Board Square Broadcom GPIO Numbering
13 14
39 15
67 18
72 23
GPIO Settings on Breadboard
GPIO Settings on Breadboard

Wiring up our Dice

Rather than use traditional dice, we will build our own dice using 6 LED connected to the Raspberry Pi GPIO. The LED will light up to signify the roll of our dice: if I roll a 3 then 3 LEDs will light up.

The Raspberry Pi outputs 3.3v per GPIO pin which flows to the LED and will light it up rather brightly[. However,] just like Roy Batty from Blade Runner, “The light that burns twice as bright, lasts half as long,” so we need to drop the voltage down using a resistor. For this project I chose to use 82 ohm resistors which run in line from the GPIO pin to the LED, dropping the voltage before it reaches the LED.

Wiring Dice Example
LED Dice Example

For each of the LED that make up our dice, we need to complete these connections, using a breadboard:

Dice LED Connections
Dice LED Connections

To connect each of the dice LED, use the above diagram as a guide. Your wiring may vary depending on your breadboard.

The pins for each of the dice LED are as follows.

Dice Broadcom GPIO Numbering

1

12
2 16
3 20
4 21
5 5
6 6

3. Configure the Software

For this project we are using Python, a robust and easy to learn language that is well suited to the Raspberry Pi. Python enables our project to use the GPIO pins which connect to the components on the game board and this is achieved via an external library, one of a few that will be used in this project.

Libraries

A library is a collection of code that can be included inside of our Python project. Typically a library is one of the many thousands that have been written, tested, and approved by the Python community. You can easily make your own library for personal use. Using a library is achieved by importing the contents of the library into the application, as in this example:

Typically you will use libraries that are already installed with your version of Python. In this project we will use a library that is not installed as standard, pygame.

Pygame is a library of functions that can be used to build games in Python. It includes functions to handle sprites, sounds, images and keyboard events. In this project we will use it to play sounds for the Pythons and Resistors.

In order to use this library we need to install it on our Raspberry Pi, and for that we need to install the Python package manager, called PIP. PIP will have the latest versions of the libraries but it does not come as standard on a fresh Raspbian install.

To install PIP, open a new terminal window by double clicking on the LXTerminal desktop icon, or navigating to the LXMenu > Accessories > LXTerminal.

In the terminal window, issue the following command and press enter:

When prompted for a password, type it in and press enter. It will take a few moments for pip to be installed on your system. When completed keep the terminal window open and enter the following command.

Pip will now install the pygame library for this project and, once complete, you are now free to close the terminal window. With all of the software dependencies met, we can now focus on the Python code that controls our game.

Python Code

When working in Python, I like to use a set format to structure my code as follows, in order:

  • Imports
  • Variables
  • Configurations
  • Functions (if any)
  • Main body of the code

And for this project I have followed this format.

Imports

Importing libraries is a vital skill to learn. It enables code written by others to be easily used in our projects. Giving us the convenience of growing our code with out the need to build concepts such as random numbers, GPIO functionality and GUI interfaces.

In the code we first import libraries that are needed. In the code snippet below, I have added comments to explain what each import achieves.

Variables

With these imports completed we now need to create the variables that will contain:

  • the position of the resistors and Pythons on the board and their GPIO pin reference.
  • the sounds used in the game.
  • the GPIO pins used for each of the LED for our dice.
  • the GPIO pin used for the push button that triggers our dice.

Configurations

Next we configure both pygame and the GPIO pins used in the game. Pygame requires us to initialize both the actual library and the mixer functions. It looks like this

The GPIO configuration needs us to set the board layout. We can use the physical pin layout, where pin 1 is at the top left of the board, next to the micro SD slot. This is an easy layout to use and works great for beginners. For this article we will use the official Broadcom pin mapping, which is not a logical numbering system but is easy to use and my preferred choice. To do this code.

Using the variables for the resistors and pythons we created earlier, we will now configure each of the pins to be an input. Each pin will receive current when the switch is triggered. In order to protect the Raspberry Pi rather delicate GPIO we will use an internal pull down resistor which buffers the pins to prevent any accidental damage.

With the inputs configured, we now do the same to the pins that make our digital dice, which are output devices that receive power from the GPIO pins to make them light up.

Lastly, on the setup we tell the pin used for d2 to turn off, as by default it is active and sending power out to any devices attached. We do this by typing the following command:

Functions

Next, we create a function to handle playback of the audio files used in the game. Our function takes an argument, the filename of the audio file. Once triggered, the function loads the audio file into memory, and then plays the audio once per trigger. While the audio plays, the function checks to see if the audio is still playing and if so it waits until complete before triggering again if needed.

Next we have a function which handles the roll of our digital dice. Once triggered, it uses the random int function from the random library to pick a number between 1 and 6. Then, using a series of conditional logic, the number rolled is compared to each number 1 to 6 and when a match is found the corresponding LED are lit up. So if I roll a 5, 5 of the LED will light up.

Main Body of the Code

Now we move to the final part of our code, the main body that is a series of conditional statements that look to see if any squares have been triggered or dice has been rolled. Once the trigger occurs, either the dice roll or audio file playback function is called and output via the Raspberry Pi.

And now we reach the end of this project. I hope that you have enjoyed yourself and learned a little more about what the new Raspberry Pi B+ board is capable of with just a few easy and cheap to purchase components.

The full code for this project, along with the image file for the game board is available via Github (link below). Links for parts below are for Element14, which has a ton of possibilities, but you may have some or all parts on hand.

Learn More

Raspberry Pi Boardgame on Github

https://github.com/lesp/RaspberryPi_Boardgame

Build a board game with the Raspberry Pi B+ on Element14

http://www.element14.com/community/community/raspberry-pi/raspberrypi_projects/blog/2014/09/21/build-a-board-game-with-the-raspberry-pi-b

Element14

http://www.element14.com/
http://www.element14.com/community/community/

Game Board to Print Out

https://raw.githubusercontent.com/lesp/RaspberryPi_Boardgame/master/Board/board.png

Raspberry Pi Model B+

http://www.element14.com/community/view-product.jspa?fsku=2431426&nsku=68X0155&COM=noscript

Breadboard

http://www.element14.com/community/view-product.jspa?fsku=2295701&nsku=15R8319&COM=noscript

10 x Reed Switches

http://www.element14.com/community/view-product.jspa?fsku=2409191&nsku=62X0734&COM=noscript

6 x 82 ohm Resistors

http://www.element14.com/community/view-product.jspa?fsku=2329555&nsku=95W5774&COM=noscript

1 x Button

http://www.element14.com/community/view-product.jspa?fsku=1638335&nsku=12P7703&COM=noscript