Spielerisch Lernen - [Teil 1]

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.

Hardware required

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.

The structure

First the circuit diagram:


The software

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

import turtle
from turtle import Turtle
from random import randint
from gpiozero import Button
from signal import pause

Leonstep=0
Rafistep=0
Mickstep=0
Donnystep=0
play=True

button1 = Button(6)
button2 = Button(13)
button3 = Button(19)
button4 = Button(26)

def Leonforward():
global Leonstep
Leonstep=randint(7,9)

def Rafiforward():
global Rafistep
Rafistep=randint(7,9)

def Mickforward():
global Mickstep
Mickstep=randint(7,9)

def Donnyforward():
global Donnystep
Donnystep=randint(7,9)

finish=Turtle()
finish.penup()
finish.goto(200,200)
finish.pendown()
finish.goto(200,-50)
finish.hideturtle()

Leon = Turtle()
Leon.color("red")
Leon.shape("turtle")
Leon.penup()
Leon.goto(-200,150)
Leon.pendown()

Rafi = Turtle()
Rafi.color("green")
Rafi.shape("turtle")
Rafi.penup()
Rafi.goto(-200,100)
Rafi.pendown()

Mick = Turtle()
Mick.color("blue")
Mick.shape("turtle")
Mick.penup()
Mick.goto(-200,50)
Mick.pendown()

Donny = Turtle()
Donny.color("pink")
Donny.shape("turtle")
Donny.penup()
Donny.goto(-200,0)
Donny.pendown()


while play:
button1.when_pressed = Leonforward
if Leonstep!=0:
Leon.forward(Leonstep)
Leonstep=0
if Leon.xcor()>200:
print("Red Turtle wins")
play=False
button2.when_pressed = Rafiforward
if Rafistep!=0:
Rafi.forward(Rafistep)
Rafistep=0
if Rafi.xcor()>200:
print("Green Turtle wins")
play=False
button3.when_pressed = Mickforward
if Mickstep!=0:
Mick.forward(Mickstep)
Mickstep=0
if Mick.xcor()>200:
print("Blue Turtle wins")
play=False
button4.when_pressed = Donnyforward
if Donnystep!=0:
Donny.forward(Donnystep)
Donnystep=0
if Donny.xcor()>200:
print("Pink Turtle wins")
play=False

print()
print("Distance to finish line:")
print("Red Turtle",200-Leon.xcor())
print("Green Turtle",200-Rafi.xcor())
print("Blue Turtle",200-Mick.xcor())
print("Pink Turtle",200-Donny.xcor())

button1.close()
button2.close()
button3.close()
button4.close()
turtle.bye()

Download Code

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.

Projects for beginners

1 comment

Tobias

Tobias

In den Links in den Code-Kommentaren fehlt ein ‘s’ (___ => projects.ra___pberrypi.org/en/projects/turtle-race).
Ansonsten Cooles Projekt, das werde ich mal ausprobieren. Klasse fände ich es, wenn ihr kleine Bundles schnüren würdet, so das man mit einem Klick das passende bestellen kann.

Gruß, Tobias

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN