-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
175 lines (156 loc) · 5.96 KB
/
Copy pathrun.py
File metadata and controls
175 lines (156 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import sys
import time
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt
from rich.text import Text
from rich.align import Align
from checks import CheckerFactory
from game import QuizGame
from helpers import ProgramHelper
console = Console()
class UIManager:
"""
Handles terminal interface rendering and main navigation.
"""
def render_header(self, title: str) -> None:
"""Renders a styled header panel using Rich."""
header_text = Text()
header_text.append(f"⛅ {title.upper()} 🌧️\n", style="bold cyan")
header_text.append(
"Welcome to Weather Wise - test your knowledge on storms, "
"climate patterns and forecasting!",
style="dim white"
)
panel = Panel(
Align.center(header_text),
border_style="bold blue",
padding=(1, 2),
)
console.print(panel)
def menu(self) -> None:
"""Displays the interactive manin menu."""
while True:
ProgramHelper.remove()
self.render_header('Weather Wise')
console.print()
menu_text = Text()
menu_text.append("1. ", style="bold bright_blue")
menu_text.append("Show Rules\n", style="white")
menu_text.append("2. ", style="bold bright_blue")
menu_text.append("Play Game\n", style="white")
menu_text.append("3. ", style="bold bright_blue")
menu_text.append("Exit Program\n", style="white")
console.print(
Panel(
menu_text,
title="[bold yellow]Main Menu[/bold yellow]",
border_style="blue",
expand=False,
)
)
console.print()
user_selects = Prompt.ask(
"[bold bright_blue]Select an option[/bold bright_blue]",
choices=["1", "2", "3"],
default="2",
)
checker = CheckerFactory.get_checker('menu', ["1", "2", "3"])
if checker.check(user_selects, "Invalid menu selection"):
ProgramHelper.remove()
if user_selects == '1':
self.rules('Rules & Instructions')
elif user_selects == '2':
quiz = QuizGame()
quiz.select_difficulty()
elif user_selects == '3':
self.exit_game()
def rules(self, title: str) -> None:
"""Displays paginated game instructions inside styled panels."""
pages = [
(
"Difficulty Selection",
". When starting the game, choose your difficulty level:\n"
" [bold cyan]1[/bold cyan] - Easy\n"
" [bold cyan]2[/bold cyan] - Medium\n"
" [bold cyan]3[/bold cyan] - Hard",
),
(
"Question Count",
". Next, select how many questions you want to answer:\n"
" [bold cyan]1[/bold cyan] - 10 questions\n"
" [bold cyan]2[/bold cyan] - 20 questions\n"
" [bold cyan]3[/bold cyan] - 30 questions",
),
(
"Answering Questions",
". Choose your answers using [bold green]A, B, C, or D[/bold green] "
"(case-insensitive).\n"
". Take your time-accuracy matters for your final score!",
),
(
"Leaderboard & Results",
". After finishing, view your final score and performance summary.\n"
". Enter your name to save your score, difficulty and timestamp to the global leaderboard!",
),
]
for i, (page_title, content) in enumerate(pages, 1):
ProgramHelper.remove()
console.print(
Panel(
f"[white]{content}[/white]",
title=f"[bold magenta]📃 {title.upper()} ({i}/{len(pages)}): {page_title}[/bold magenta]",
border_style="magenta",
padding=(1, 2),
)
)
console.print()
Prompt.ask(
"[dim]Press [bold white]ENTER[/bold white] to continue[/dim]",
default="",
)
def exit_game(self) -> None:
"""Handles exit confirmation and graceful shutdown animation."""
console.print()
confirm = Prompt.ask(
"[bold yellow]Are you sure you want to exit?[/bold yellow]",
choices=["y", "n"],
default="n",
)
checker = CheckerFactory.get_checker('exit', ["y", "n"])
if checker.check(confirm, "Invalid exit selection"):
ProgramHelper.remove()
if confirm.lower() == "y":
console.print()
for i in range(3, 0, -1):
console.print(f"[dim]Exiting in {i} seconds...[/dim]")
time.sleep(0.6)
ProgramHelper.remove()
console.print(
Panel(
Align.center(
"[bold cyan]Thank you for playing Weather Wise! Goodbye 👋[/bold cyan]"
),
border_style="bright_black",
)
)
time.sleep(1)
sys.exit()
else:
Prompt.ask("[dim]Press [bold white]ENTER[/bold white] to return to main menu[/dim]", default="")
def main():
ui_manager = UIManager()
try:
# Start the program from the menu
ui_manager.menu()
except KeyboardInterrupt:
console.print()
console.print(
Panel(
Align.center("[bold cyan]Thanks for playing Weather Wise! Goodbye 👋[/bold cyan]"),
border_style="bright_black",
)
)
sys.exit()
if __name__ == '__main__':
main()