import random import time from gpiozero import Button from gpiozero import RotaryEncoder import json 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) 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(): print("Tilt me!") result = tilt.wait_for_press(timeout=round_duration) 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=round_duration) if result: print(success_message()) return 1 else: return -1 def command_scream(): print('Make noise at me!') result = sound.wait_for_press(timeout=round_duration) 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=round_duration) 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 play_game(): result = 0 score = 0 list_command_choices =[command_spin, command_tilt, 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("----------------------------------------------------") print("Get ready for the next round!") print(f"Your time is {round_duration} seconds.") print("----------------------------------------------------") round = random.choice(list_command_choices) result = round() decrease_round_duration() print("----------------------------------------------------") 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()