Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions arcade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

import random
from types import ModuleType

from arcade import __version__, art, scoreboard
Expand All @@ -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."""
Expand All @@ -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:
Expand All @@ -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()
Loading