-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
72 lines (59 loc) · 2.73 KB
/
game.py
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
from components.audio import AudioEngine
from components.controls import Controls
from components.engine import PlayerStats, RaceEngine
from components.graphics import Display
from components.leds import LEDController
from components.menus import (IntroMenu, MainMenu, TrolleyShopMenu, GarageMenu, TrolleyUpgradesMenu, TrackMenu,
CrashMenu, WinMenu, LoseMenu, DrawMenu)
from utils.resource_manager import cleanup
class Application:
def __init__(self):
self.current_menu = 'intro'
self.last_menu = None # No last menu to start with
self.menu_passthrough = None # Used to pass data between menus
self.root_display = Display()
self.player_stats = PlayerStats()
self.controls = Controls()
self.led_controller = LEDController(led_count=5, brightness=0.01)
self.audio_engine = AudioEngine()
# We'll store class references instead of instances
self.menu_classes = {
'intro': IntroMenu,
'main': MainMenu,
'race': RaceEngine,
'trolley_shop': TrolleyShopMenu,
'garage': GarageMenu,
'trolley_upgrades': TrolleyUpgradesMenu,
'track_select': TrackMenu,
'crash_menu': CrashMenu,
'win_menu': WinMenu,
'lose_menu': LoseMenu,
'draw_menu': DrawMenu,
}
self.menus = {} # Empty dictionary to store menu instances
def menu_controller(self):
"""
Main menu controller that handles the switching between menus.
:return:
"""
while True:
# Check if the current menu is instantiated; if not, instantiate it
if self.current_menu not in self.menus:
self.menus[self.current_menu] = self.menu_classes[self.current_menu](self)
# Show the current menu
self.menu_passthrough = self.menus[self.current_menu].show(self.menu_passthrough)
# Check if the current menu has changed after showing the menu (e.g., if an action in the menu changed
# the current_menu)
if self.current_menu != self.last_menu:
# Clear resources related to the last menu
if self.last_menu and self.last_menu in self.menus:
self.menus[self.last_menu].cleanup_labels_sprites()
del self.menus[self.last_menu] # Remove the instance from the dictionary
# Update the last menu to the current one after processing
self.last_menu = self.current_menu
# Clear the display for the new menu
self.root_display.clear_main_display_group()
cleanup()
cleanup()
app = Application()
app.menu_controller()