-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_menu.py
58 lines (51 loc) · 2.28 KB
/
main_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
"""
Displays the main menu
Allows user to play or quit
"""
import pygame
import pygame_gui
import constants
import game_states
background_color = (100, 100, 100)
def main_menu(surface: pygame.surface.Surface):
"""
Initiates a few required variables
(GUI management, fonts, text)
"""
gui_manager = pygame_gui.UIManager(
(constants.window_width, constants.window_height), 'main_menu_theme.json')
clock = pygame.time.Clock()
font = pygame.font.Font("ARCADECLASSIC.TTF", 98)
title_img = font.render("Smart Asteroids", True,
(200, 200, 200, 200), background_color)
title_rect = title_img.get_rect()
title_rect.center = (constants.window_width*0.5,
constants.window_height*235/720)
title_rect.size = (constants.window_width*700//1280,
constants.window_height*250//720)
play_button_rect = pygame.Rect((constants.window_width*470//1280, constants.window_height*405//720),
(constants.window_width*340//1280, constants.window_height*80//720))
play_button = pygame_gui.elements.UIButton(
relative_rect=play_button_rect, text="Play", manager=gui_manager)
quit_button_rect = pygame.Rect((constants.window_width*470//1280, constants.window_height*529//720),
(constants.window_width*340//1280, constants.window_height*80/720))
quit_button = pygame_gui.elements.UIButton(
relative_rect=quit_button_rect, text="Quit", manager=gui_manager)
is_running = True
while is_running:
dt = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame.USEREVENT:
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == play_button:
return game_states.GAME_STATES.IN_GAME
elif event.ui_element == quit_button:
return game_states.GAME_STATES.QUIT
gui_manager.process_events(event)
gui_manager.update(dt)
surface.fill(background_color)
surface.blit(title_img, title_rect)
gui_manager.draw_ui(surface)
pygame.display.update()