1 / 3 • Pilot controls

Recently, I became a dad. Like every dad, I want to be the best dad I can be. A good way to do this is by applying my existing passions in my new role. I like to play music, so instead of only playing rock songs, I now also play children lullabies. I also like nerding out with a Raspberry Pi, so instead of coming up with random projects, I try to come up with projects which my daughter can enjoy.

So, when the Games with PyGame series from Sean Tracey started in the MagPi magazine, I decided to follow along and see what was possible. Building up to the end of the series, Sean introduced a vertical space shooter where you control the hero vessel using the mouse. This seemed to be a good occasion to add my own twist to the series, and explore if it was possible to control the game using input devices which can survive some child-like abuse. Experience teaches me that a one-year-old does not yet realize that throwing the mouse at the screen is not a very effective way of interacting with a computer. In contrast, button mashing seems to come quite natural to her.

The Raspberry Pi provides the ideal platform for such experiments through the GPIO pins. While no analog inputs are available on these pins, several I2C or SPI chips are available which extend the existing capabilities. I used an MCP3008 chip, for which Adafruit has a great guide. Together with a slider potentiometer and an arcade button I had lying around for other projects, sufficient hardware was present to whip up a quick prototype. The MCP3008 was wired up on a breadboard, and the actual controls were jammed in a cardboard box. I decided to mount the arcade button on the side of the box to get a pinball-y feel (and because it was too large to fit in my box upright). Alligator clips were used to connect the controls to the breadboard.

Looks good, on to the software! First, I added a class to handle the inputs in the gpioInput.py file. The code for the potentiometer borrows heavily from the Adafruit MCP3008 tutorial (https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/script). For example, the readadc method is copied as is, and the getPotPosition method removes the jitter which is inevitable with analog controls similarly to their algorithm.

Changing the game files themselves was quite straightforward: check how the mouse movements are handled, and add similar handlers for the GPIO controls. In aliens.py, I added instructions in the main loop to read the GPIO state, similar to how the mouse state is read. In the updateGame method, the code for firing (and blocking fire when the button is held down) was implemented similar to the mouse handling code, and the position was set using the potentiometer position instead of the mouse position. This required a change in the setPosition method of the ships.py file as well, since the potentiometer only has a 1-dimensional position, instead of the 2-dimensional position of a mouse.

That's it! The resuting code is pushed to a fork of the original repository, available on https://github.com/Sharp6/Games-with-Pygame, in the Part 9 directory. Other changes in this repository include adaptations to run the game on the Raspberry Pi Touchscreen. This required turning the resolution down to 800x480, resizing the assets, and changing the area coordinates for detecting the button press in the main menu.