Skip to main content
Stuart fox Music Chat

Build a Raspberry Pi Space Invaders Controller

Build a three-button controller on a breadboard, wire it to the Pi and play Space Invaders with it. A Cotswold Pi Jam tutorial.

Build a Raspberry Pi Space Invaders Controller

From my days helping out at the fabulous Cotswold Pi Jam: a Python project I wrote for one of the tutorial sessions, and a great first step into wiring physical buttons and switches into your own code.

We’ll build a small three-button control panel, wire it to a Raspberry Pi’s GPIO pins, write a few lines of Python to read the buttons, then use what we’ve learnt to play a version of Space Invaders modified to work with our new controller.

A Space Invaders controller tutorial session in progress at the Cotswold Raspberry Jam
A Space Invaders session at the Cotswold Jam. “Parents under instruction not to beat their children’s high score.” (@CotswoldJam)

What you’ll learn

  • How a breadboard connects components
  • Wiring push buttons to the Pi’s GPIO pins
  • Reading button presses in Python with the gpiozero library
  • How a real game uses the same technique

Level: beginner  ·  Time: about an hour  ·  Tutorial by Stuart Fox, with thanks to Scott Bowman & Lee Robinson  ·  Public domain

What you’ll need

  • A Raspberry Pi with Raspberry Pi OS, keyboard, mouse and screen
  • 1 × mini breadboard
  • 3 × momentary push buttons
  • 6 × male-to-female jumper leads (pin to socket)
The kit laid out: a mini breadboard, three push buttons and six jumper leads
The kit

Part 1: Build the controller

1 Bend the buttons

Hold a button by its connectors and carefully bend it so it sits at 90 degrees to the pins. Go slowly and gently so the connectors don’t snap. Do the same with the other two.

Diagram: button bent at 90 degrees to its pins

2 Place the buttons on the breadboard

Lay the breadboard in front of you as in the diagram, and use its numbers and letters to find each hole.

  • Button 1 into holes E1 and E3, facing away from you
  • Button 2 into E5 and E7
  • Button 3 into E15 and E17
Breadboard with rows numbered and columns lettered
Breadboard rows and columns
All three push buttons in place on the mini breadboard, jumper leads ready
All three buttons in place

Part 2: Wire it to the Pi

Before you start: shut the Pi down and unplug the power.

Wiring diagram: three buttons on the breadboard connected to the Raspberry Pi GPIO header
The finished wiring

The colour of the jumper leads doesn’t matter. Where you plug them in does. Connect each breadboard hole to the Pi pin shown:

BreadboardRaspberry Pi pin
A1Pin 29 (GPIO 5)Button 1
A3Pin 39 (GND)Button 1
A5Pin 31 (GPIO 6)Button 2
A7Pin 25 (GND)Button 2
A15Pin 33 (GPIO 13)Button 3
A17Pin 34 (GND)Button 3
Raspberry Pi GPIO header pin numbers
The Pi’s GPIO pin numbers

Each button gets one lead to a GPIO pin (which the program watches) and one to a ground pin (GND). Pressing the button connects the two. Double-check every lead before you power up.

Part 3: Test it with some code

Power up the Pi and open Thonny Python IDE from the Programming menu. Type in this program:

from gpiozero import Button
from signal import pause

button_1 = Button(5)
button_2 = Button(6)
button_3 = Button(13)

def buttonone():
    print("Button 1")

def buttontwo():
    print("Button 2")

def buttonthree():
    print("Button 3")

button_1.when_pressed = buttonone
button_2.when_pressed = buttontwo
button_3.when_pressed = buttonthree

pause()

Save it with File → Save As (any name, ending in .py), then click ▶ Run at the top of the window. Press each button: the program should print its name every time.

How it works

from gpiozero import Button
from signal import pause

The from lines tell the computer to learn some new things. Programs can borrow code other people have written; these collections are called libraries. We need Button from the gpiozero library to detect presses, and pause from the signal library to keep the program waiting.

button_1 = Button(5)
button_2 = Button(6)
button_3 = Button(13)

These give each button a name and tell the program which GPIO it’s wired to: GPIO 5, 6 and 13, which are physical pins 29, 31 and 33 on the header.

def buttonone():
    print("Button 1")

This sets up a function: a set of instructions waiting to be triggered. Each of ours does one thing, printing some text. In the Space Invaders code you’ll see functions like these doing several things at once.

button_1.when_pressed = buttonone
pause()

This connects the button to its function, so a press runs it. pause() keeps the program running, waiting for presses, until you stop it.

Part 4: Play Space Invaders

With the controller built and tested, it’s time to defend Earth. Download the game (buttoninvaders.zip), unzip it on your Pi, then in Thonny open buttoninvaders.py with File → Open. Keep the fonts, images and sounds folders next to it.

Turn on line numbers (Tools → Options → Editor → Show line numbers), then look at lines 12–20, 55–63 and 451–484. They’re commented to show how the game was changed to use our buttons. You’ll recognise the technique from Part 3.

Click ▶ Run, then press fire (or the space bar) to start. If Thonny says No module named pygame, open a Terminal and type sudo apt install python3-pygame.

  • Button 1: move left
  • Button 2: move right
  • Button 3: fire
Space Invaders

Download the game (ZIP)Space Invaders by Lee Robinson, modified for the controller, plus the button test program

Download the printable tutorial (PDF)The original handout from the Cotswold Pi Jam packs

Tutorial, photos and controller modifications by Stuart Fox, released into the public domain. Space Invaders game by Lee Robinson (MIT licence). The original is also on Wikimedia Commons and Wikibooks.

Composer for TV and film. If you're working on something, I'd love to hear about it.

Keep reading

The mini Machine

Builds

The mini Machine

A tiny one-player bartop arcade cabinet in black and red carbon fibre.

Mac Pro 2019: NVMe M.2 SSD on a PCIe Card Issue

Gear

Mac Pro 2019: NVMe M.2 SSD on a PCIe Card Issue

Why my Mac Pro 2019 would not see an NVMe M.2 SSD on a PCIe card, and what fixed it.

Chip music on the ZX Spectrum Next: Popcorn and Axel F

Software & Studio

Chip music on the ZX Spectrum Next: Popcorn and Axel F

Popcorn and Axel F, rebuilt on a ZX Spectrum Next using NextDAW and its three AY sound chips.

Let's talk

Tell me about your project

Stuart Fox

I'm in the studio now Stuart Fox

Message me directly. It's me on the other end, not a bot. Fill in the form and I'll reply to you personally, usually the same day.

Chat now
Contact Form Demo

Or call 01452 780826, Monday to Friday, 9am–5pm.