-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
172 lines (147 loc) · 4.82 KB
/
main.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
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
import sys
import pygame
from functools import wraps
from helpers import (
HEIGHT,
SCALE,
WIDTH,
brick_height,
brick_width,
track1,
track2,
track3,
track_path,
AutoEnum,
)
from models import Color
from pages import Info, MainMenu, Settings, ModeSelection
from pages import loading_screen
from models import Database, Session
class GameState(AutoEnum):
"""Enum to manage game states."""
MAIN_MENU: int
MODE_SELECTION: int
SETTINGS: int
INFO: int
EXIT: int
def handle_event(state):
"""Decorator to handle state-specific events."""
def decorator(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
if self.current_state == state:
return func(self, *args, **kwargs)
return None
return wrapper
return decorator
class Game:
def __init__(
self,
*,
height=HEIGHT,
width=WIDTH,
scale=SCALE,
) -> None:
self.height = height
self.width = width
self.scale = scale
self.colors = Color()
self.clock = pygame.time.Clock()
self._music_files = track_path, track1, track2, track3
self.music_is_playing = False
self.trails = {}
self.current_state = GameState.MAIN_MENU
self.main_menu = None
self.settings_page = None
self.info_page = None
self.mode_selection = None
self.db = Database(
db_file="BrickMania.zdb",
key_file="BrickMania_key.zkey",
iv_file="Brickmania_iv.ziv",
)
self.session = Session(self.db, "W")
self.volume = 0.0
def pre_load_music(self):
"""Pre-load music with set volume."""
for music_file in self.music_files:
music_file.set_volume(self.volume)
def initialize_pages(self):
"""Initialize the pages."""
self.main_menu = MainMenu(
self.screen, self.height, self.width, self.scale, self
)
self.settings_page = Settings(
self.screen, self.height, self.width, self.scale, self
)
self.info_page = Info(self.screen, self.height, self.width, self.scale, self)
self.mode_selection = ModeSelection(
self.screen, self.height, self.width, self.scale, self
)
@property
def music_files(self):
return self._music_files[1:]
def run_loading_screen(self):
"""Call the dynamic loading screen function."""
return loading_screen(self.colors)
@handle_event(GameState.MAIN_MENU)
def handle_main_menu(self):
"""Handle logic for the main menu."""
if not self.main_menu:
self.initialize_pages()
selected_option = self.main_menu.generate(
self.colors, brick_width, brick_height
)
if selected_option == 0:
self.current_state = GameState.MODE_SELECTION
elif selected_option == 1:
self.current_state = GameState.SETTINGS
elif selected_option == 2:
self.current_state = GameState.INFO
elif selected_option == 3:
self.current_state = GameState.EXIT
@handle_event(GameState.MODE_SELECTION)
def handle_mode_selection(self):
"""Handle logic for the mode selection page."""
back_to_main_menu = self.mode_selection.run(
self.colors, self.clock, self.trails
)
if back_to_main_menu:
self.current_state = GameState.MAIN_MENU
@handle_event(GameState.SETTINGS)
def handle_settings(self):
"""Handle logic for the settings page."""
back_to_main_menu = self.settings_page.display()
if back_to_main_menu:
self.current_state = GameState.MAIN_MENU
@handle_event(GameState.INFO)
def handle_info(self):
"""Handle logic for the info page."""
back_to_main_menu = self.info_page.scroll(self.colors)
if back_to_main_menu:
self.current_state = GameState.MAIN_MENU
def gameloop(self):
"""Main game loop."""
self.pre_load_music()
pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height))
self.screen.fill(self.colors.BLACK)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.handle_main_menu()
self.handle_mode_selection()
self.handle_settings()
self.handle_info()
if self.volume:
pygame.mixer.music.load(self._music_files[0])
pygame.mixer.music.play(-1)
else:
pygame.mixer.music.stop()
pygame.display.update()
self.clock.tick(60)
if __name__ == "__main__":
game = Game()
game.gameloop()