Program games yourself and manufacture your game controllers with little effort - Part 1
In order not to lose my practice, I occasionally hold online seminars at the Raspberry Pi Foundation or look at the many examples that are published on their homepage. This is how I became aware of the games Turtle Race (as an example for the Turtle Graphics module) and Pong (as an example for the pygame zero module).
In the first game, four little turtles race against each other; a random number determines the step size. I missed the participation of (up to four) players. For the second game - a clone of the first successful arcade game - I missed the paddles, as these controllers were called at the time and were manufactured by Atari in large numbers. You can only get them by chance at a flea market. So get to work and do it yourself.
In addition to the required cable and an analog-to-digital converter, everything we need for our game controller is shown in the first picture: Ü-eggs (alternatively champagne corks), buttons and potentiometers with 10 kOhm.
The pictures speak for themselves: drill through the Ü-egg above and below, open the Ü-egg (what a shame, nothing left), install a button or potentiometer, pull the cable through and solder it on. Close the Ü-egg again. Finished.
Now to the first game for which the controllers with the buttons are required.
First the circuit diagram:
For the game controller with the buttons, I use GPIOs 6, 13, 19 and 26, which are all nearby to a GND pin. So I can instantiate the button objects with the default settings of gpiozero.
I named my turtles after the unforgettable Teenage Mutant Ninja Turtles, the cinema film of the 1990s (and their names go back to the old Italian masters). So that it stays exciting even with unequal players, chance plays a part. The turtle moves with each click randint (7.9) 7, 8 or 9 pixels forward.
# Turtle race with four buttons to be pressed as often as possible
# Random steps of 7 to 9 units per click
# based on projects.rapberrypi.org/en/projects/turtle-race
# Names in memory of the unforgettable Teenage Mutant Ninja Turtles
# and their eponym the Italien Masters
from turtle import Turtle
from random import randint
from gpiozero import Button
from signal import pause
button1 = Button(6)
button2 = Button(13)
button3 = Button(19)
button4 = Button(26)
Leon = Turtle()
Rafi = Turtle()
Mick = Turtle()
Donny = Turtle()
button1.when_pressed = Leonforward
print("Red Turtle wins")
button2.when_pressed = Rafiforward
print("Green Turtle wins")
button3.when_pressed = Mickforward
print("Blue Turtle wins")
button4.when_pressed = Donnyforward
print("Pink Turtle wins")
print("Distance to finish line:")
Here the Link to the Documentation of gpiozero:
Have fun understanding and, if necessary, improving the program code, tinkering with the game controller and, of course, playing. In the second part I will show how the game controllers with the potentiometers are used as paddles.
A surprise for me when taking the screenshot: When activating the scrot program, the turtles disappeared completely from the active window. That's why I took this picture with the compact camera. Maybe at some point I will find an explanation for it.