-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
361 lines (297 loc) · 14.3 KB
/
menu.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
import os
import pygame as pg
from pygame.locals import *
import pygame_gui as pg_gui
from typing import Tuple, List, Dict
from pygame_gui.core import IncrementalThreadedResourceLoader
SCREEN_COLOR = pg.Color('#9bddf9')
SETTINGS_COLOR = pg.Color('#6bb6ff')
class Menu:
menu_type: str
screen: pg.Surface
screen_size: Tuple[int, int]
manager: pg_gui.UIManager
clock: pg.time.Clock
sound: Dict[str, pg.mixer.Sound]
def __init__(self, menu_type: str, screen_size: Tuple[int, int], screen: pg.Surface):
"""Initializes menu screen with given menu type and screen_size"""
pg.init()
pg.mouse.set_cursor(pg.cursors.diamond)
pause_sound = pg.mixer.Sound('music/pause.mp3')
pause_sound.set_volume(0.3)
click_sound = pg.mixer.Sound('music/click.wav')
click_sound.set_volume(0.3)
self.menu_type = menu_type
self.screen_size = screen_size
self.screen = screen
self.sound = {'pause': pause_sound, 'click': click_sound}
loader = IncrementalThreadedResourceLoader()
self.manager = pg_gui.UIManager(screen_size, 'themes/themes.json', resource_loader=loader)
self.manager.add_font_paths(font_name='IndieFlower', regular_path='fonts/IndieFlower-Regular.ttf')
self.manager.add_font_paths(font_name='LoungeThings', regular_path='fonts/Lounge-Things.otf')
font_list = [{'name': 'IndieFlower', 'point_size': 30, 'style': 'regular'},
{'name': 'LoungeThings', 'point_size': 32, 'style': 'regular'}]
self.manager.preload_fonts(font_list)
loader.start()
self.clock = pg.time.Clock()
def display(self, on: bool) -> None:
"""Display module for all menus"""
raise NotImplementedError
class NameEntry(Menu):
_name_entry: pg_gui.elements.UITextEntryLine
_enter_button: pg_gui.elements.UIButton
_player_name: str
def __init__(self, screen_size: Tuple[int, int], screen: pg.Surface):
"""Initializes NameEntry menu with given screen"""
Menu.__init__(self, 'main', screen_size, screen)
# Create Text Entry Field
text_rect_pos = (int(screen_size[0] / 2 - 150), int(screen_size[1] / 2))
text_rect = pg.Rect(text_rect_pos, (200, 60))
text_entry = pg_gui.elements.UITextEntryLine(relative_rect=text_rect,
manager=self.manager,
object_id='#' + str(1) + ',' + str(0))
# Create Enter Button
button_rect_pos = (int(screen_size[0] / 2 + 80), int(screen_size[1] / 2))
button_rect = pg.Rect(button_rect_pos, (80, 40))
button = pg_gui.elements.UIButton(relative_rect=button_rect,
text='Enter',
manager=self.manager,
object_id='#' + str(1) + ',' + str(1))
info_rect_pos = (int(screen_size[0] / 2 - 150), int(screen_size[1] / 2 - 42))
info_rect = pg.Rect(info_rect_pos, (220, 45))
info_text = pg_gui.elements.UITextBox(relative_rect=info_rect,
html_text="Please enter your name:",
manager=self.manager,
object_id='#' + str(1) + ',' + str(2))
self._name_entry = text_entry
self._enter_button = button
self._player_name = ''
def display(self, on) -> str:
while on:
time_delta = self.clock.tick(60) / 1000.0
for event in pg.event.get():
if event.type == pg.QUIT:
on = False
pg.quit()
if event.type == pg.USEREVENT:
if event.user_type == pg_gui.UI_TEXT_ENTRY_FINISHED:
self.sound['click'].play()
if event.text != '':
self._player_name = event.text
on = False
if event.user_type == pg_gui.UI_BUTTON_PRESSED:
self.sound['click'].play()
if self._name_entry.text != '':
self._player_name = self._name_entry.text
on = False
else:
print("Please enter a name")
self.manager.process_events(event)
self.manager.update(time_delta)
self.screen.fill(SCREEN_COLOR)
self.manager.draw_ui(self.screen)
pg.display.update()
return self._player_name
class MainMenu(Menu):
options: list
return_option: str
option_rects: List[pg.Rect]
option_info: List[Tuple[pg.Rect, str]]
def __init__(self, screen_size: Tuple[int, int], screen: pg.Surface):
"""Initializes Main Menu with given screen"""
Menu.__init__(self, 'main', screen_size, screen)
self.options = []
self.screen_size = screen_size
self.screen = screen
self.add_options('Start')
self.add_options('Settings')
self.add_options('Quit')
self.option_rects = []
self.option_info = []
self.return_option = ''
default_color = (154, 167, 177)
options_font = pg.font.Font('fonts/Prodelt Co.ttf', 40)
for option in self.options:
text_surface = options_font.render(option[0], True, default_color)
text_rect = text_surface.get_rect()
text_rect.center = option[1]
self.option_rects.append(text_rect)
self.option_info.append((text_rect, option[0]))
def add_options(self, option: str) -> None:
# Use the existing number of options to calculate text y-pos offset
num_options = len(self.options)
off_set = self.screen_size[1] * 0.5 + num_options * (self.screen_size[1] / 10)
# Set text position
text_pos = (int(self.screen_size[0] / 2), int(off_set))
# Generate new text info
text = (option, text_pos)
# Add text info to options list
self.options.append(text)
def display(self, on) -> str:
logo = pg.image.load("images/logo.png").convert_alpha()
logo_rect = logo.get_rect()
logo_size = logo.get_size()
logo_rect.topleft = (self.screen_size[0] / 2 - logo_size[0] / 2, self.screen_size[1] * 0.08)
while on:
mouse_pos = pg.mouse.get_pos()
for event in pg.event.get():
if event.type == pg.QUIT:
on = False
pg.quit()
if event.type == pg.MOUSEBUTTONDOWN:
self.sound['click'].play()
for option_info in self.option_info:
if option_info[0].collidepoint(mouse_pos):
self.return_option = option_info[1]
on = False
self.screen.fill(SCREEN_COLOR)
default_color = (154, 167, 177)
hover_color = (243, 166, 148)
options_font = pg.font.Font('fonts/Prodelt Co.ttf', 40)
for option in self.options:
text_surface = options_font.render(option[0], True, default_color)
text_rect = text_surface.get_rect()
text_rect.center = option[1]
if text_rect.collidepoint(mouse_pos):
text_surface = options_font.render(option[0], True, hover_color)
self.screen.blit(text_surface, text_rect)
self.screen.blit(logo, logo_rect)
pg.display.update()
return self.return_option
class Settings(Menu):
map_id: int
mode: str
option: str
_map_menu: pg_gui.elements.UIDropDownMenu
_mode_menu: pg_gui.elements.UIDropDownMenu
_logout_button: pg_gui.elements.UIButton
_exit: pg_gui.elements.UIButton
def __init__(self, screen_size: Tuple[int, int], screen: pg.Surface):
"""Initializes Settings menu with given screen"""
Menu.__init__(self, 'main', screen_size, screen)
# Create Map Dropdown List
map_menu_pos = (int(screen_size[0] * 0.3 - 75), int(screen_size[1] / 2 - 100))
map_menu_rect = pg.Rect(map_menu_pos, (150, 50))
map_num = len([m for m in os.listdir('maps/')])
map_list = []
for i in range(1, map_num + 1):
map_list.append('map{}'.format(i))
map_menu = pg_gui.elements.UIDropDownMenu(options_list=map_list, starting_option='map1',
relative_rect=map_menu_rect, manager=self.manager)
# Create Settings Button
mode_pos = (int(screen_size[0] * 0.7 - 75), int(screen_size[1] / 2 - 100))
mode_rect = pg.Rect(mode_pos, (150, 50))
mode_menu = pg_gui.elements.UIDropDownMenu(options_list=['Shortest Path', 'User Control'],
starting_option='User Control',
relative_rect=mode_rect, manager=self.manager)
# Create LogOut Button
logout_button_pos = (int(screen_size[0] * 0.8 - 150), int(screen_size[1] * 0.8))
logout_button_rect = pg.Rect(logout_button_pos, (120, 40))
logout_button = pg_gui.elements.UIButton(relative_rect=logout_button_rect,
text='Logout',
manager=self.manager)
# Create exit button
exit_button_pos = (int(screen_size[0] * 0.8), int(screen_size[1] * 0.8))
exit_button_rect = pg.Rect(exit_button_pos, (120, 40))
exit_button = pg_gui.elements.UIButton(relative_rect=exit_button_rect,
text='Exit',
manager=self.manager)
self._map_menu = map_menu
self._mode_menu = mode_menu
self._logout_button = logout_button
self._exit = exit_button
self.map_id = 1
self.mode = 'User Control'
self.option = ''
def display(self, on) -> str:
while on:
time_delta = self.clock.tick(60) / 1000.0
for event in pg.event.get():
if event.type == pg.QUIT:
on = False
pg.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
self.sound['pause'].play()
on = False
if event.type == pg.USEREVENT:
if event.user_type == pg_gui.UI_DROP_DOWN_MENU_CHANGED:
if event.ui_element == self._map_menu:
self.map_id = int(event.text[-1])
if event.ui_element == self._mode_menu:
self.mode = event.text
if event.user_type == pg_gui.UI_BUTTON_PRESSED:
self.sound['click'].play()
if event.ui_element == self._exit:
self.option = 'exit'
on = False
if event.ui_element == self._logout_button:
on = False
self.option = 'logout'
self.manager.process_events(event)
self.manager.update(time_delta)
self.screen.fill(SETTINGS_COLOR)
self.manager.draw_ui(self.screen)
pg.display.update()
return self.option
class Pause(Menu):
_exit: pg_gui.elements.UIButton
_continue: pg_gui.elements.UIButton
_cover: pg.Surface
return_option: str
def __init__(self, screen_size: Tuple[int, int], screen: pg.Surface):
"""Initializes Settings menu with given screen"""
Menu.__init__(self, 'main', screen_size, screen)
# Create Background
back_pos = (int(screen_size[0] * 0.3), int(screen_size[1] * 0.3))
back_size = (int(screen_size[0] * 0.4), int(screen_size[1] * 0.4))
off_set = 60
# Create Exit Button
exit_button_pos = (back_pos[0] + back_size[0] / 2 - 70, back_pos[1] + off_set * 2)
exit_button_rect = pg.Rect(exit_button_pos, (140, 60))
exit_button = pg_gui.elements.UIButton(relative_rect=exit_button_rect,
text='Exit',
manager=self.manager)
# Created Continue Button
continue_button_pos = (back_pos[0] + back_size[0] / 2 - 70, exit_button_pos[1] + off_set * 2)
continue_button_rect = pg.Rect(continue_button_pos, (140, 60))
continue_button = pg_gui.elements.UIButton(relative_rect=continue_button_rect,
text='Continue',
manager=self.manager)
self._exit = exit_button
self._continue = continue_button
self._cover = pg.Surface(screen_size)
self.return_option = ''
def display(self, on) -> str:
click_sound = pg.mixer.Sound('music/click.wav')
click_sound.set_volume(0.3)
while on:
time_delta = self.clock.tick(60) / 1000.0
for event in pg.event.get():
if event.type == pg.QUIT:
on = False
pg.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
self.sound['pause'].play()
self.return_option = 'continue'
on = False
if event.type == pg.USEREVENT:
if event.user_type == pg_gui.UI_BUTTON_PRESSED:
self.sound['click'].play()
if event.ui_element == self._exit:
self.return_option = 'exit'
on = False
else:
self.return_option = 'continue'
on = False
self.manager.process_events(event)
self.manager.update(time_delta)
self._cover.fill(color=(214, 218, 217))
self._cover.set_alpha(10)
self.screen.blit(self._cover, (0, 0))
self.manager.draw_ui(self.screen)
pg.display.update()
return self.return_option
def reset(self):
self.return_option = ''