From a6879057257b1a2247b6843eb90f8a46440f8fcc Mon Sep 17 00:00:00 2001 From: johnjermiah-git Date: Mon, 5 Oct 2026 16:35:48 +0530 Subject: [PATCH] Add surprise me option to arcade menu --- arcade/main.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/arcade/main.py b/arcade/main.py index e7a4f05..92a7652 100644 --- a/arcade/main.py +++ b/arcade/main.py @@ -6,6 +6,7 @@ from __future__ import annotations +import random from types import ModuleType from arcade import __version__, art, scoreboard @@ -21,6 +22,11 @@ tictactoe, ] +# Extra menu options are defined in one place. +SURPRISE_OPTION = len(GAMES) + 1 +SCORES_OPTION = len(GAMES) + 2 +QUIT_OPTION = len(GAMES) + 3 + def show_menu() -> None: """Print the list of games and the extra options.""" @@ -31,8 +37,9 @@ def show_menu() -> None: print(f" {number}. {art.bold(game.NAME)}") print(f" {art.dim(game.DESCRIPTION)}") - print(f"\n {len(GAMES) + 1}. High scores") - print(f" {len(GAMES) + 2}. Quit\n") + print(f"\n {SURPRISE_OPTION}. Surprise me") + print(f" {SCORES_OPTION}. High scores") + print(f" {QUIT_OPTION}. Quit\n") def show_high_scores() -> None: @@ -58,27 +65,40 @@ def run_game(game: ModuleType) -> None: print(art.green(" Saved.\n")) +def run_surprise() -> None: + """Choose and play a random game.""" + game = random.choice(GAMES) + print(art.cyan(f"\n Surprise! You got: {game.NAME}\n")) + run_game(game) + + def main() -> None: """Start the arcade. Loops until the player chooses to quit.""" - quit_option = len(GAMES) + 2 - scores_option = len(GAMES) + 1 - while True: show_menu() - choice = ask_int(" What would you like to play?", minimum=1, maximum=quit_option) + choice = ask_int( + " What would you like to play?", + minimum=1, + maximum=QUIT_OPTION, + ) - if choice == quit_option: + if choice == QUIT_OPTION: print(art.cyan("\n Thanks for playing. See you next time.\n")) return - if choice == scores_option: + if choice == SCORES_OPTION: show_high_scores() pause() continue + if choice == SURPRISE_OPTION: + run_surprise() + pause() + continue + run_game(GAMES[choice - 1]) pause() if __name__ == "__main__": - main() + main() \ No newline at end of file