Raspberry_Pi_Bop_it/Bop_it_sensors.py

83 lines
2.5 KiB
Python

import random
import time
from gpiozero import Button
from gpiozero import RotaryEncoder
laser = Button(17) #Die Zahl entspricht dem Pin auf dem ARPI600 Board
sound = Button(18, pull_up=True)
rotate = RotaryEncoder(20, 21)
tilt = Button(23)
def command_tilt():
print("Tilt me!")
result = tilt.wait_for_press(timeout=10)
if result:
time.sleep(0.3) # soll verhindern, dass die vorherige Runde die jetzige beeinflusst
print(success_message())
return 1
else:
return -1
def command_blind():
print('Blind me!')
result = laser.wait_for_press(timeout=10)
if result:
print(success_message())
return 1
else:
return -1
def command_scream():
print('Make noise at me!')
result = sound.wait_for_press(timeout=10)
if result:
time.sleep(0.3) #soll verhindern, dass dasGeräuschsignal aus der vorherigen Runde gezählt wird
print(success_message())
return 1
else:
return -1
def command_spin():
directions = ["clockwise", "counter-clockwise"]
used_direction = random.choice(directions)
print(f"Spin me... {used_direction}!")
rotate.value = 0 # damit jede Runde wieder bei Null anfängt, Werte würden allgemein vom -1 bis 1 reichen
result = rotate.wait_for_rotate(timeout=10)
print(rotate.value)
print(result)
if result and rotate.value > 0 and used_direction == "clockwise" or result and rotate.value < 0 and used_direction == "counter-clockwise":
print(success_message())
return 1
else:
print("You waited too long or used the wrong direction!")
return -1
def success_message():
list_of_messages = ["Great job!", "Correct!", "Nice!", "You know it!", "That's what I'm talking about", "Perfect!"]
message = random.choice(list_of_messages)
return message
def main():
score = 0
result = 0
print("Raspberry will now tell you what to do.")
print("Every correct action will give you a point.")
print("How many can you get?")
print("Let's go!")
list_command_choices =[command_spin] #command_tilt, command_spin, command_blind, command_scream]
while result != -1:
score += result # war zuerst ein paar Zeilen weiter unten, hat dann aber nach einer falschen Runde den Score erhöht
time.sleep(0.5)
print("Get ready for the next round!")
round = random.choice(list_command_choices)
result = round()
print(f"Game over! Your Score is {score}")
main()