-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
207 lines (169 loc) · 5.7 KB
/
menu.py
File metadata and controls
207 lines (169 loc) · 5.7 KB
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
import pygame
import sys
import os
import random
import pygame.freetype
from pygame.sprite import Sprite
from pygame.rect import Rect
from enum import Enum
from pygame.sprite import RenderUpdates
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
if getattr(sys, 'assets', False):
os.chdir(sys._MEIPASS)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Updated to conform to flake8 and black standards
from pygame.locals import (
RLEACCEL,
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
# Define constants for the screen width and height
SCREEN_WIDTH = 1040
SCREEN_HEIGHT = 720
def create_surface_with_text(text, font_size, text_rgb, bg_rgb):
""" Returns surface with text written on """
font = pygame.freetype.SysFont("Sans", font_size, bold=True)
surface, _ = font.render(text=text, fgcolor=text_rgb, bgcolor=bg_rgb)
return surface.convert_alpha()
#user interface element that can be added to a surface
class UIElement(Sprite):
def __init__(self, center_position, text, font_size, bg_rgb, text_rgb, action=None):
"""
Args:
center_position - tuple (x, y)
text - string of text to write
font_size - int
bg_rgb (background colour) - tuple (r, g, b)
text_rgb (text colour) - tuple (r, g, b)
action - the gamestate change associated with this button
"""
self.mouse_over = False
default_image = create_surface_with_text(
text=text, font_size=font_size, text_rgb=text_rgb, bg_rgb=bg_rgb
)
highlighted_image = create_surface_with_text(
text=text, font_size=font_size * 1.2, text_rgb=text_rgb, bg_rgb=bg_rgb
)
self.images = [default_image, highlighted_image]
self.rects = [
default_image.get_rect(center=center_position),
highlighted_image.get_rect(center=center_position),
]
self.action = action
super().__init__()
@property
def image(self):
return self.images[1] if self.mouse_over else self.images[0]
@property
def rect(self):
return self.rects[1] if self.mouse_over else self.rects[0]
def update(self, mouse_pos, mouse_up):
""" Updates the mouse_over variable and returns the button's
action value when clicked.
"""
if self.rect.collidepoint(mouse_pos):
self.mouse_over = True
if mouse_up:
return self.action
else:
self.mouse_over = False
def draw(self, surface):
""" Draws element onto a surface """
surface.blit(self.image, self.rect)
def title_screen(screen):
start_btn = UIElement(
center_position=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2),
font_size=30,
bg_rgb=BLACK,
text_rgb=WHITE,
text="Start Game",
action=GameState.NEWGAME,
)
quit_btn = UIElement(
center_position=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 +50),
font_size=30,
bg_rgb=BLACK,
text_rgb=WHITE,
text="Quit Game (ESC)",
action=GameState.QUIT,
)
buttons = RenderUpdates(start_btn, quit_btn)
return game_loop(screen, buttons)
def play_level(screen):
return_btn = UIElement(
center_position=(SCREEN_WIDTH//2, SCREEN_HEIGHT-50),
font_size=20,
bg_rgb=BLACK,
text_rgb=WHITE,
text="Return to main menu",
action=GameState.TITLE,
)
formation_btn = UIElement(
center_position=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 +100),
font_size=30,
bg_rgb=BLACK,
text_rgb=WHITE,
text=f"Formation of the Moon",
action=GameState.FORMATION_LEVEL,
)
earthMoon_btn = UIElement(
center_position=(SCREEN_WIDTH//2, 260),
font_size=30,
bg_rgb=BLACK,
text_rgb=WHITE,
text=f"Earth Moon Systems",
action=GameState.EARTHMOON_LEVEL,
)
AgeMoon_btn = UIElement(
center_position=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2),
font_size=30,
bg_rgb=BLACK,
text_rgb=WHITE,
text=f"Age of the Moon",
action=GameState.MOONAGE_LEVEL,
)
buttons = RenderUpdates(return_btn, earthMoon_btn, AgeMoon_btn, formation_btn)
return game_loop(screen, buttons)
#Handles game loop until an action is return by a button in the
#buttons sprite renderer.
def game_loop(screen, buttons):
bg_title = pygame.image.load(resource_path("./assets/bg_title.png"))
bg_title = pygame.transform.scale(bg_title, (SCREEN_WIDTH, SCREEN_HEIGHT))
bg_title = bg_title.convert()
while True:
mouse_up = False
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
mouse_up = True
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == QUIT:
pygame.quit()
sys.exit()
for button in buttons:
ui_action = button.update(pygame.mouse.get_pos(), mouse_up)
if ui_action is not None:
return ui_action
screen.blit(bg_title, (0, 0))
buttons.draw(screen)
pygame.display.flip()
class GameState(Enum):
QUIT = -2
TITLE = -1
NEWGAME = 0
FORMATION_LEVEL = 1
EARTHMOON_LEVEL = 2
MOONAGE_LEVEL = 3