-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
385 lines (314 loc) · 12.4 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# %%
from game.sprite.menu_background import MenuBackground
from game.sprite.message import Message
from game.sprite.button import Button
from game.utility.response_menu import ResponseMenu
from game.utility.sliding_menu import SlidingMenu
from game.utility.confirm_menu import ConfirmMenu
from game.utility.debug import Debugger
from game.library import Library
from game.utility.tracker import Tracker
from game.utility.transition import Transition
from game.utility.slideshow import Slideshow
from game.utility.tutorial_overlay import TutorialOverlay
from game.module.scene_builder import Scene
from game.module.region import Map
from game.module.setting import Setting
from game.module.prologue import Prologue
import pygame
import os, sys
import random
class Main:
"""
This will be the main module of the game.
This runs the main menu of the game when played.
"""
def __init__(self):
# Setting up the debugger
self.debug = Debugger()
# Centering the game window on the screen
os.environ["SDL_VIDEO_CENTERED"] = "1"
# Setting up the game
pygame.init()
pygame.mixer.init()
pygame.mixer.set_num_channels(16)
pygame.mouse.set_cursor(pygame.cursors.tri_left)
# Setting up the data
self.data = Library()
self.screen = None
self.is_full_screen = self.data.setting["full_screen"]
self.clock = None
self.initialize_game()
self.volume_bgm = self.data.setting["bgm"]
self.volume_sfx = self.data.setting["sfx"]
self.mixer_meta_channel = pygame.mixer.Channel(0)
self.mixer_buttons_channel = pygame.mixer.Channel(1)
self.mixer_coins_channel = pygame.mixer.Channel(2)
self.mixer_notifications_channel = pygame.mixer.Channel(3)
self.mixer_channels = [
self.mixer_meta_channel,
self.mixer_buttons_channel,
self.mixer_coins_channel,
self.mixer_notifications_channel,
]
pygame.mixer.music.set_volume(self.volume_bgm)
for channel in self.mixer_channels:
channel.set_volume(self.volume_sfx)
self.show_studio_intro = self.data.meta["intro_show"]
# Screen surface (for transitions)
self.display_surface = pygame.Surface(
(self.data.setting["game_width"], self.data.setting["game_height"])
)
self.display_surface.fill(self.data.colors["black"])
self.display_surface.convert_alpha()
# Utility class
self.transition = Transition(
self,
transition_length=self.data.meta["intro_transition"],
duration_length=self.data.meta["intro_duration"],
display_image=self.data.meta_images["studio"],
hold_sfx=self.data.music["studio_intro"],
)
self.slide_show = None
# Menus
self.sliding_menu = SlidingMenu(self)
self.confirm_menu = ConfirmMenu(self)
self.response_menu = ResponseMenu(self)
# Initialize system tracker
self.tracker = None
# Setting up other windows
self.scene_window = None
self.map_window = None
if self.data.progress is not None:
self.sliding_menu.initialize_modules(self)
self.tracker = Tracker(self)
self.scene_window = Scene(self)
self.map_window = Map(self)
self.tutorial_overlay = TutorialOverlay(self)
self.setting_window = Setting(self)
# Sprites and sprite groups
self.buttons = pygame.sprite.Group()
self.new_game_button = Button(
self,
self.check_save_file,
top_left_coordinates=(
int(self.data.setting["game_width"] * 0.375),
int(self.data.setting["game_height"] * 0.45),
),
**{
"idle": self.data.title_screen["new_game_button_idle"],
"hovered": self.data.title_screen["new_game_button_hovered"],
},
)
self.new_game_button.add(self.buttons)
self.continue_button = Button(
self,
self.continue_game,
top_left_coordinates=(
int(self.data.setting["game_width"] * 0.375),
int(self.data.setting["game_height"] * 0.57),
),
**{
"idle": self.data.title_screen["continue_button_idle"],
"hovered": self.data.title_screen["continue_button_hovered"],
"disabled": self.data.title_screen["continue_button_disabled"],
},
)
self.continue_button.add(self.buttons)
self.setting_button = Button(
self,
self.setting_callback,
top_left_coordinates=(
int(self.data.setting["game_width"] * 0.375),
int(self.data.setting["game_height"] * 0.69),
),
**{
"idle": self.data.title_screen["setting_button_idle"],
"hovered": self.data.title_screen["setting_button_hovered"],
},
)
self.setting_button.add(self.buttons)
self.exit_button = Button(
self,
self.exit_callback,
top_left_coordinates=(
int(self.data.setting["game_width"] * 0.375),
int(self.data.setting["game_height"] * 0.81),
),
**{
"idle": self.data.title_screen["exit_button_idle"],
"hovered": self.data.title_screen["exit_button_hovered"],
},
)
self.exit_button.add(self.buttons)
# Mouse related variable
self.last_mouse_pos = None
if self.show_studio_intro:
self.transition.run()
self.transition.change_state(self.transition.FINISHED)
# Main loop
self.debug.log("Memory after initialization:")
self.debug.memory_log()
self.running = True
self.main_loop()
def initialize_game(self):
# Title
pygame.display.set_caption(
self.data.meta["title"] + " " + self.data.meta["version"]
)
self.debug.log(f"Game Title: {self.data.meta['title']}")
self.debug.log(f"Game Version: {self.data.meta['version']}")
# Icon
pygame.display.set_icon(self.data.meta_images["icon"])
# Setting the screen size
if self.is_full_screen:
self.screen = pygame.display.set_mode(
(self.data.setting["game_width"], self.data.setting["game_height"]),
pygame.FULLSCREEN | pygame.SCALED,
)
else:
self.screen = pygame.display.set_mode(
(self.data.setting["game_width"], self.data.setting["game_height"])
)
self.debug.log(f"Display Width: {self.data.setting['game_width']}")
self.debug.log(f"Display Height: {self.data.setting['game_height']}")
# Setting the clock
self.clock = pygame.time.Clock()
self.debug.log(f"Game FPS: {self.data.setting['fps']}")
def refresh_display(self):
pygame.display.update()
self.clock.tick(self.data.setting["fps"])
def close_game(self):
pygame.mixer.quit()
pygame.quit()
self.debug.close()
sys.exit()
def mouse_click_events(self, event):
click_coordinates = event.pos
# If the user clicked on left mouse button
if event.button == 1:
if self.tracker:
self.tracker.add_click()
for button in self.buttons:
button.check_clicked(click_coordinates)
# If the user clicked on the right mouse button
if event.button == 3:
pass
# If the user scrolls the mouse wheel upward
if event.button == 4:
pass
# If the user scrolls the mouse wheel downward
if event.button == 5:
pass
def mouse_drag_events(self, event):
# This variable always saves the last mouse location to be used by the
# key_events() function, because we can't always get the location
# of the mouse if the only event is key press
self.last_mouse_pos = event.pos
# Making sure the user only holds one button at a time
if event.buttons[0] + event.buttons[1] + event.buttons[2] == 1:
# If the user is dragging the mouse with left mouse button
if event.buttons[0] == 1:
pass
# If the user is dragging the mouse with the right mouse button
if event.buttons[2] == 1:
pass
# Hovering through display check
else:
for button in self.buttons:
button.check_hovered(self.last_mouse_pos)
def key_events(self, keys):
# If the user pressed the "del" key, (enter explanation here)
if keys[pygame.K_DELETE]:
pass
# If the user pressed the "a" key, (enter explanation here)
if keys[pygame.K_a]:
pass
# If the user pressed the "d" key, (enter explanation here)
if keys[pygame.K_d]:
pass
def global_key_down_events(self, key):
if key == pygame.K_F1:
self.debug.log(f"F1 Trigger: Memory check")
self.debug.memory_log()
def check_save_file(self, *args):
if self.data.progress == None:
self.create_new_game()
else:
self.confirm_menu.set_message_and_callback(
[
"Are you sure you want",
" to start a new game?",
"",
"All your progress will",
"be reset.",
],
self.create_new_game,
)
self.confirm_menu.enable = True
def create_new_game(self):
self.debug.log("Create new game entered")
self.slide_show = Slideshow(self)
self.prologue = Prologue(self)
self.prologue.run()
self.sliding_menu.initialize_modules(self)
if self.scene_window is None:
self.scene_window = Scene(self)
else:
self.scene_window.reconstruct(self)
if self.map_window is None:
self.map_window = Map(self)
self.tracker = Tracker(self)
self.scene_window.run()
def continue_game(self, *args):
self.debug.new_line()
self.debug.log("Continue game entered")
self.scene_window.run()
def setting_callback(self, *args):
self.setting_window.run()
def exit_callback(self, *args):
self.close_game()
def play_random_coin_sfx(self):
index = str(random.randint(1, 15))
self.mixer_coins_channel.play(self.data.music[f"earn_coins_{index}"])
def main_loop(self):
pygame.mixer.music.load(self.data.music["main_menu"])
pygame.mixer.music.play(-1)
while self.running:
# Screen rendering
self.screen.blit(self.data.title_screen["title_screen"], (0, 0))
# Updating sprites
self.buttons.update()
# Checking if menus will be displaying
self.confirm_menu.update()
self.response_menu.update()
# Event processing
for event in pygame.event.get():
if self.response_menu.enable:
self.response_menu.handle_event(event)
elif self.confirm_menu.enable:
self.confirm_menu.handle_event(event)
else:
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
self.mouse_click_events(event)
elif event.type == pygame.MOUSEMOTION:
self.mouse_drag_events(event)
elif event.type == pygame.KEYDOWN:
self.global_key_down_events(event.key)
# Key pressing events (holding keys applicable)
keys = pygame.key.get_pressed()
self.key_events(keys)
# Final checks on the continue button for initial game exit to menu
if self.data.progress is None:
self.continue_button.set_disabled(True)
else:
self.continue_button.set_disabled(False)
# Updating the display
self.refresh_display()
# Closing the game properly
self.close_game()
if __name__ == "__main__":
Main()
# %%