Wikijunior:Raspberry Pi/Raspberry Pi Build A Raspberry Pi Space Invaders Controller

Tutorial by Stuart Fox Thanks to Scott Bowman & Lee Robinson Public Domain 28th Jan 2017 – www.cotswoldjam.org

In this tutorial, we'll build a mini game controller, wire it to a Raspberry Pi's GPIO pins and write a program in Python to recognise the button presses. Finally we’ll examine and run a Space Invaders game, modified with the methods we’ve just learnt about to work with our controller.

What's in the kit?
In your bag you will find the following components:


 * ×6 M-F Jumper leads (pin to socket)
 * ×1 Mini Breadboard
 * ×3 Momentary Push Buttons

Part 1 – Let’s build the controller
&#160;&#160;&#160;&#160;

Take a button by its connectors as shown and carefully bend the button so it is at a 90-degree angle to the pins.

Do this slowly and gently to avoid snapping the connectors.

Place the breadboard in front of you as in the image to the left. Use the numbers and letters shown in the image as a reference.

Now take your first button and push its connectors into holes E1 and E3 with the button facing away from you as shown on the right.

Repeat this process, placing your second button's connectors into holes E5 and E7 and the third button's connectors into holes E15 and E17.

Part 2 – Wiring your controller


The colour of the jumper wires in your kit is not important – where you put the jumper wires is important!

Make sure you connect to the correct holes and pins. Using the diagram above for reference, follow these steps to wire your controller:



Part 3 – The Code
Now you have a controller built and wired to your Raspberry Pi's GPIO Pins, it’s time to see if it works. Make sure your wires have been checked by a tutor before you power up your Pi.

Power up your Raspberry Pi. From the desktop menu, select Programming – Thonny Python IDE.

Let’s run the program. Use the Run button on the top of the window.

Now try pushing each of your buttons. If everything is working the program will show the text correponding to each button when it's pressed.

Type in the following code
Now use File, Save As to save your program as the name of your choice (don’t forget to put .py on the end) in the  folder.

This program shows us how to use the GPIOZero library to trigger functions in Python and will also serve to test your newly built controller.

Let's look at how this program works…
The  lines tell the computer to learn about new things. Computers can learn from programs that other people have written; we call these other programs "libraries". Our program needs the function called button from the  library which it will use to detect button presses on your controller. The function  from the   library so that we can insert pauses.

These lines set the names of our buttons and tell the program which GPIO pin they are connected to. So now the program will see our three buttons connected to these three GPIO pins: pin 5 (I2C1 SCL), pin 6 (GND) and pin 7 (GPIO 4)

Here we are setting up functions called,   and. Functions contain multiple instructions, ready to be triggered by an event or input. In this case, each function only performs one task, and that is to print some text on the screen. When we look at the Space Invaders game code, we will see functions like this performing multiple tasks at once when triggered.

This  tells the program to run forever in a loop.

Now we are telling the program to trigger a function when a button is pressed.

Part 4 – Let's Play Space Invaders


Now our control panel is built and tested, it's time to defend earth against an alien attack! In Thonny Python IDE, Use File, Open to open  program in the   folder.

Now enable line numbers: from the Tools menu select options, choose the editor tab, then tick the box by 'Show Line Numbers'.

Look at the following lines of code and see if you recognise the way this game has been modified to work with our controller.

Lines 12 to 20, lines 55 to 63, and lines 451 to 484. These lines are commented to explain the modifications.

Now, click the Run button and our modified Space Invaders game will launch.


 * Button one is left
 * Button two is right
 * Button three is fire

You can press fire or the space bar to start the game.

Files
The assets for this Space Invaders game are in the Raspberry Pi tutorials Button Invaders assets category on Wikicommons.

Space Invaders Controler Tutorial2.pdf
The original PDF for this tutorial is available on Wikimedia Commons:

LICENSE.txt
Note: The source code is available under the permissive MIT License as per the GitHub repository: https://github.com/leerob/space-invaders/blob/master/LICENSE