Neues Menü, Highscores, Umstrukturierung in der main()-Schleife
This commit is contained in:
parent
5dae606ce8
commit
e9a0780b8f
@ -2,15 +2,86 @@ import random
|
|||||||
import time
|
import time
|
||||||
from gpiozero import Button
|
from gpiozero import Button
|
||||||
from gpiozero import RotaryEncoder
|
from gpiozero import RotaryEncoder
|
||||||
|
import json
|
||||||
|
|
||||||
laser = Button(17) #Die Zahl entspricht dem Pin auf dem ARPI600 Board
|
laser = Button(17) #Die Zahl entspricht dem Pin auf dem ARPI600 Board
|
||||||
sound = Button(18, pull_up=True)
|
sound = Button(18, pull_up=True)
|
||||||
rotate = RotaryEncoder(20, 21)
|
rotate = RotaryEncoder(20, 21)
|
||||||
tilt = Button(23)
|
tilt = Button(23)
|
||||||
|
round_duration = 4
|
||||||
|
highscore_file_path = "./Highscores.json" #das heisst, die Datei wird im selben Verzeichnis wie der Code gespeichert
|
||||||
|
score_separator = " --- "
|
||||||
|
highscore_length = 5
|
||||||
|
|
||||||
|
def save_highscore(highscores):
|
||||||
|
with open(highscore_file_path, mode="w", encoding="utf-8") as write_file:
|
||||||
|
json.dump(highscores, write_file)
|
||||||
|
|
||||||
|
def load_highscore():
|
||||||
|
try:
|
||||||
|
with open(highscore_file_path, mode="r" , encoding="utf-8") as read_file:
|
||||||
|
return json.load(read_file)
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def show_menu():
|
||||||
|
print(" __________________________________________________")
|
||||||
|
print("| |")
|
||||||
|
print("| MENU |")
|
||||||
|
print("|__________________________________________________|")
|
||||||
|
print("")
|
||||||
|
print("1 - Play a round of Bop it!")
|
||||||
|
print("2 - Show Highscores")
|
||||||
|
print("3 - End game")
|
||||||
|
print("----------------------------------------------------")
|
||||||
|
print("Enter choice: ")
|
||||||
|
print("----------------------------------------------------")
|
||||||
|
return input()
|
||||||
|
|
||||||
|
def show_highscores():
|
||||||
|
scores = load_highscore()
|
||||||
|
print("")
|
||||||
|
if len(scores) == 0:
|
||||||
|
print("Be the first to Bop it!")
|
||||||
|
else:
|
||||||
|
for index, element in enumerate(scores):
|
||||||
|
print(f"{index + 1}. {element}")
|
||||||
|
print("")
|
||||||
|
|
||||||
|
def get_name_for_highscore(rank, score):
|
||||||
|
print("----------------------------------------------------")
|
||||||
|
print(f"Congratulations! You are the new number {rank}")
|
||||||
|
print("----------------------------------------------------")
|
||||||
|
print("Enter your name, champ: ")
|
||||||
|
new_name = input()
|
||||||
|
return new_name + score_separator + str(score)
|
||||||
|
|
||||||
|
def check_if_highscore(score):
|
||||||
|
scores = load_highscore()
|
||||||
|
has_highscore = False
|
||||||
|
for index, entry in enumerate(scores):
|
||||||
|
name, entry_score = entry.split(score_separator)
|
||||||
|
if score > int(entry_score):
|
||||||
|
new_entry = get_name_for_highscore(index + 1, score)
|
||||||
|
scores.insert(index, new_entry)
|
||||||
|
if len(scores) > highscore_length:
|
||||||
|
loser = scores.pop().split(score_separator)[0]
|
||||||
|
print("----------------------------------------------------")
|
||||||
|
print(f"{loser} was kicked off the highscore list. Bye bye!")
|
||||||
|
print("----------------------------------------------------")
|
||||||
|
has_highscore = True
|
||||||
|
break
|
||||||
|
if not has_highscore and len(scores) < highscore_length:
|
||||||
|
scores.append(get_name_for_highscore(len(scores)+1, score))
|
||||||
|
save_highscore(scores)
|
||||||
|
|
||||||
|
def decrease_round_duration():
|
||||||
|
global round_duration
|
||||||
|
round_duration = round_duration * 0.97
|
||||||
|
|
||||||
def command_tilt():
|
def command_tilt():
|
||||||
print("Tilt me!")
|
print("Tilt me!")
|
||||||
result = tilt.wait_for_press(timeout=10)
|
result = tilt.wait_for_press(timeout=round_duration)
|
||||||
if result:
|
if result:
|
||||||
time.sleep(0.3) # soll verhindern, dass die vorherige Runde die jetzige beeinflusst
|
time.sleep(0.3) # soll verhindern, dass die vorherige Runde die jetzige beeinflusst
|
||||||
print(success_message())
|
print(success_message())
|
||||||
@ -22,7 +93,7 @@ def command_tilt():
|
|||||||
|
|
||||||
def command_blind():
|
def command_blind():
|
||||||
print('Blind me!')
|
print('Blind me!')
|
||||||
result = laser.wait_for_press(timeout=10)
|
result = laser.wait_for_press(timeout=round_duration)
|
||||||
if result:
|
if result:
|
||||||
print(success_message())
|
print(success_message())
|
||||||
return 1
|
return 1
|
||||||
@ -31,7 +102,7 @@ def command_blind():
|
|||||||
|
|
||||||
def command_scream():
|
def command_scream():
|
||||||
print('Make noise at me!')
|
print('Make noise at me!')
|
||||||
result = sound.wait_for_press(timeout=10)
|
result = sound.wait_for_press(timeout=round_duration)
|
||||||
if result:
|
if result:
|
||||||
time.sleep(0.3) #soll verhindern, dass dasGeräuschsignal aus der vorherigen Runde gezählt wird
|
time.sleep(0.3) #soll verhindern, dass dasGeräuschsignal aus der vorherigen Runde gezählt wird
|
||||||
print(success_message())
|
print(success_message())
|
||||||
@ -44,7 +115,7 @@ def command_spin():
|
|||||||
used_direction = random.choice(directions)
|
used_direction = random.choice(directions)
|
||||||
print(f"Spin me... {used_direction}!")
|
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
|
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)
|
result = rotate.wait_for_rotate(timeout=round_duration)
|
||||||
if result and rotate.value > 0 and used_direction == "clockwise" or result and rotate.value < 0 and used_direction == "counter-clockwise":
|
if result and rotate.value > 0 and used_direction == "clockwise" or result and rotate.value < 0 and used_direction == "counter-clockwise":
|
||||||
print(success_message())
|
print(success_message())
|
||||||
return 1
|
return 1
|
||||||
@ -58,23 +129,39 @@ def success_message():
|
|||||||
message = random.choice(list_of_messages)
|
message = random.choice(list_of_messages)
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def main():
|
def play_game():
|
||||||
score = 0
|
|
||||||
result = 0
|
result = 0
|
||||||
print("Raspberry will now tell you what to do.")
|
score = 0
|
||||||
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_blind, command_scream]
|
list_command_choices =[command_spin, command_tilt, command_blind, command_scream]
|
||||||
while result != -1:
|
while result != -1:
|
||||||
score += result # war zuerst ein paar Zeilen weiter unten, hat dann aber nach einer falschen Runde den Score erhöht
|
score += result # war zuerst ein paar Zeilen weiter unten, hat dann aber nach einer falschen Runde den Score erhöht
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
print("----------------------------------------------------")
|
||||||
print("Get ready for the next round!")
|
print("Get ready for the next round!")
|
||||||
|
print(f"Your time is {round_duration} seconds.")
|
||||||
|
print("----------------------------------------------------")
|
||||||
round = random.choice(list_command_choices)
|
round = random.choice(list_command_choices)
|
||||||
result = round()
|
result = round()
|
||||||
|
decrease_round_duration()
|
||||||
|
print("----------------------------------------------------")
|
||||||
print(f"Game over! Your Score is {score}")
|
print(f"Game over! Your Score is {score}")
|
||||||
|
print("----------------------------------------------------")
|
||||||
|
check_if_highscore(score)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
user_choice = None
|
||||||
|
print("=== WELCOME TO BOP IT! ===")
|
||||||
|
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!")
|
||||||
|
user_choice = show_menu()
|
||||||
|
while user_choice != "3":
|
||||||
|
if user_choice == "1":
|
||||||
|
play_game()
|
||||||
|
elif user_choice == "2":
|
||||||
|
show_highscores()
|
||||||
|
user_choice = show_menu()
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user