Testing Your Speed

Exclusive offer: get 50% off this eBook here
Raspberry Pi Projects for Kids

Raspberry Pi Projects for Kids — Save 50%

Start your own coding adventure with your kids by creating cool and exciting games and applications on the Raspberry Pi with this book and ebook

£8.99    £4.50
by Daniel Bates | March 2014 | Open Source

In this article by Daniel Bates, the author of Rasberry Pi Projects for Kids, discusses how to create a new game that will test how quickly the player can react. To do this, we will create our own game controller—something you can't do on a normal computer—and write a program to handle when the controller's buttons are to be pressed.

(For more resources related to this topic, see here.)

Creating the game controller

In order to design a controller, we first need to know what sort of game is going to be played. I am going to explain how to make a game where the player is told a letter, and he/she has to press the button of that letter as quickly as possible. They then are told about another letter. The player has to hit as many buttons correctly as they can in a 30 second time limit.

There are many ways in which this game can be varied; instead of ordering the player to press a particular button, the game could ask the player a multiple-choice question, and instead of colors, the buttons could be labeled with Yes, No, Maybe or different colors. You could give the player multiple commands at once, and make sure that he/she presses all the buttons in the right order. It would even be possible to make a huge controller and treat it as more of a board game. I will leave the game design up to you, but I recommend that you follow the instructions in this article until the end, and then change things to your liking once you know how everything works.

The controller base

So now that we know how the game is going to be played, it's time to design the controller. This is what my design looks like with four different letters:

Make sure each button area is at least a little bigger than a paper clip, as these are what the buttons will be made of. I recommend a maximum of eight buttons.

Draw your design on to the card, decorate it however you like, and then cut it out.

Adding buttons

Now for each button, we need to perform the following steps:

  1. Poke two small holes in the card, roughly 3 cm apart (or however long your paper clips are), as shown in the following figure. Use a sharp pencil or a pair of scissors to do this.

  2. Push a paper fastener through each hole and open them out, as shown in the following figures:

  3. Wrap a paper clip around the head of one of the fasteners, and (if necessary) bend it so that it grips the fastener tightly, as shown in the following figure:

  4. Bend the other end of the paper clip up very slightly, so it doesn't touch the second fastener unless you press down on it, as shown in the following figure:

  5. Turn the card over and tape one leg of each fastener in place, making sure that they don't touch, as shown in the following figure:

  6. Tape a length of wire to each of the two remaining legs of the fasteners. The ends of the wires should be exposed metal so that electricity can flow through the wire, paper fastener, and paper clip (as shown in the following figure). You may like to delay this step until later, when you have a better idea of how long the wire should be.

Connecting to the Raspberry Pi

Now that the controller is ready, it's time to connect it to the Raspberry Pi. One of the things that distinguishes the Raspberry Pi from a normal computer is its set of general purpose input/output (GPIO) pins. These are the 26 pins at the top-left corner of the Raspberry Pi, just above the logo. As the name suggests, they can be used for any purpose, and are capable of both sending and receiving signals.

The preceding figure shows what each of the pins does. In order to create a (useful) circuit, we need to connect one of the power pins to one of the ground pins, with some sort of electrical component in between. The GPIO pins are particularly useful because we can make them behave like either power or ground pins, and they can also detect what they're connected to.

Note that there are two versions of the pin numbering system. You will almost certainly have a revision 2 Raspberry Pi. The revision 2 board has two mounting holes, while the revision 1 board has none. (These holes are surrounded by metal and are large enough to put a screw through. It's easy to spot them if they're there.) It is safest to simply not use any of the pins that have different numbers in different revisions.

To connect your controller to the Raspberry Pi, connect one wire from each button to a 3V3 Power pin, and each of the remaining wires to a different GPIO pin (one with GPIO in its name as in the previous figure). In my example, I will use pins 22, 23, 24, and 25. Everything is now connected as shown in the following figure:

Summary

In this article, we used the Python programming language to create a game. We created an electronic circuit to act as the game controller, and used code to detect when the buttons were being pressed. We learned the basics of the Python language, and saw how separating the code into multiple functions makes it more flexible and easier to manage.

Resources for Article:


Further resources on this subject:


Raspberry Pi Projects for Kids Start your own coding adventure with your kids by creating cool and exciting games and applications on the Raspberry Pi with this book and ebook
Published: March 2014
eBook Price: £8.99
Book Price: £14.99
See more
Select your format and quantity:

About the Author :


Daniel Bates

Daniel Bates is a Computer Science researcher at the University of Cambridge. His day job involves inventing designs for future mobile phone processors, and when he goes home, he likes playing games or working on one of his coding projects (or both!). Daniel has been a volunteer for the Raspberry Pi  Foundation since 2011, and is enthusiastic about introducing new people to computing. He has previously written Instant Minecraft: Pi Edition Coding How-to, Packt Publishing.

Books From Packt


Instant Raspberry Pi Gaming [Instant]
Instant Raspberry Pi Gaming [Instant]

Raspberry Pi Home Automation with Arduino
Raspberry Pi Home Automation with Arduino

The Business Analyst's Guide to Oracle Hyperion Interactive Reporting 11
Raspberry Pi Networking Cookbook

Instant Minecraft: Pi Edition Coding How-to [Instant]
Instant Minecraft: Pi Edition Coding How-to [Instant]

Raspberry Pi Media Center
Raspberry Pi Media Center

Raspberry Pi Robotic Projects
Raspberry Pi Robotic Projects

Raspberry Pi Super Cluster
Raspberry Pi Super Cluster

Raspberry Pi for Secret Agents
Raspberry Pi for Secret Agents

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software