Hello, my name is Carlos Soto (firstname.lastname@example.org), I am an engineer working in programming for kids since 10 years ago in both China and Venezuela (my home country). Our work and research are focused about “why teach programming to younger kids?”, into this amazing topic, we would like to share with this Robociti community some interesting projects to get fun and engage students in their first steps in programming and Robotics.
This GAME called “Left/Right GAME”, is simple game to learn how to use digital input and digital output using Raspberry. The GAME consists in get random state (Left -RED State- and Right -Green State-), and the player should follow the current state with two button joystick. The speed of appearance of the states will increase to give more difficulty to the game. the game ends when the player cannot match the states correctly.
We prepared a video when you can check the GAME idea and check circuit and interface during the game duration,
In the following lines, we will explain step by step the code. The first one is the declaration about GPIOzero, with this python module, we can control digital I/O. The headers about digital I/O are defined as:
from gpiozero import LED from gpiozero import Button from gpiozero import Buzzer # pins 17, 18 and 26 as digital outuput (leds and buzzer) ledRED=LED(17) ledGREEN=LED(18) bz=Buzzer(26) #pins 20, 21 as buttons (Digital input). buttonRED=Button(21) buttonGREEN=Button(20)
We will use the pins 17, 18 and 26 as digital outuput (leds and buzzer), and pins 20, 21 as buttons (Digital input). in the following picture you can find the circuit of our GAME,
With the button interruptions, we used when_pressed=FunctionGREEN, this is one instructions of GPIOzero phyton module, with this one, we can get the option from the player and following the GAME, until this options does match with target, also using two fuction: FuctionRED() and FuctionGREEN(),
from time import sleep,time from random import randint from signal import pause goalRED=0 goalGREEN=0 contadorRED=0 contadorGREEN=0 def FunctionRED(): print("RED") global contadorRED contadorRED=contadorRED+1 ledRED.off() ledGREEN.off() bz.beep(0.01,.01,1) def FunctionGREEN(): print("GREEN") global contadorGREEN contadorGREEN=contadorGREEN+1 ledRED.off() ledGREEN.off() bz.beep(0.01,.01,1) buttonRED.when_pressed=FunctionRED buttonGREEN.when_pressed=FunctionGREEN #The time to GAME duration timedown=2 start=time()
In order to get a real GAME, we need to define a graphic interface in order to get fun and play. for the graphic interface, we will use a turtle python module, using a unique windows,
Inside the windows, we should write the code, the square to get a graphic option to play, a green square and red square are drawn, import turtle #Draw the GAME Name GameNAME=turtle.Turtle() GameNAME.penup() GameNAME.goto(-170,150) GameNAME.write("Left/Right GAME", font = ('Times New Roman', 36,'bold')) #Draw the Cuadrado RED cuadradoR=turtle.Turtle() cuadradoR.penup() cuadradoR.goto(-170,-20) cuadradoR.shape('square') cuadradoR.color('red') cuadradoR.shapesize(9,10,1) #Draw the Cuadrado GREEN cuadradoG=turtle.Turtle() cuadradoG.penup() cuadradoG.goto(170,-20) cuadradoG.shape('square') cuadradoG.color('green') cuadradoG.shapesize(9,10,1)
To finish the GAME, we will use one loop "Repeat forever" to play, this loop will break when the conditions to finish the game are when the conditions to end the game are met, then the loop will be break and the GAME will be OVER,the score of the game is relative about the time during the correct a choose of Left or Right,
while True: timedown=timedown-0.05 randomG=randint(1,2) if timedown<0: timedown=0.001 print(randomG," RED Goal ",goalRED," pushed ", contadorRED," GREEN GREEN ",goalGREEN," pushed ",contadorGREEN) if randomG==1: ledRED.off() cuadradoR.hideturtle() cuadradoG.hideturtle() sleep(0.01) ledRED.on() ledGREEN.off() print(randomG) goalGREEN=goalGREEN+1 cuadradoG.showturtle() if randomG==2: ledRED.off() cuadradoR.hideturtle() cuadradoG.hideturtle() sleep(0.01) ledGREEN.on() ledGREEN.on() print(randomG) goalRED=goalRED+1 cuadradoR.showturtle() print(randomG," RED Goal ",goalRED," pushed ", contadorRED," GREEN GREEN ",goalGREEN," pushed ",contadorGREEN) sleep(timedown) now=time()-start #With this code, the game has condition to GAME OVER if (abs(goalRED-contadorRED)>1) or (abs(goalGREEN-contadorGREEN>1)) or timedown<0: print("GAME OVER ",now,timedown) #Draw the GAME over GameOVER=turtle.Turtle() GameOVER.penup() GameOVER.goto(-170,-170) GameOVER.write("GAME OVER ", font = ('Times New Roman', 36,'bold')) GameOVER.goto(-170,-220) GameOVER.write(now, font = ('Times New Roman', 36,'bold')) bz.beep(0.01,.01,10) break #Code to make a continue if randomG==1: sleep(timedown/4) if (goalGREEN-contadorGREEN==0): print('G1 ',timedown/4) continue sleep(timedown/4) if (goalGREEN-contadorGREEN==0): print('G2 ',timedown/4) continue sleep(timedown/4) if (goalGREEN-contadorGREEN==0): print('G3 ',timedown/4) continue # these command allow to continue when the player get the correct answer (Red or GREEN) if randomG==2: sleep(timedown/4) if (goalRED-contadorRED==0): print('R1 ',timedown/4) continue sleep(timedown/4) if (goalRED-contadorRED==0): print('R2 ',timedown/4) continue sleep(timedown/4) if (goalRED-contadorRED==0): print('R3 ',timedown/4) continue print('waiting4 ',timedown/4) #sleep(timedown*4)
Did you like the idea? Do you want to play? just try, if you have any questions, comments or suggestions, just write here. your ideas are welcome, we will be sharing more interesting projects and games.