-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
284 lines (247 loc) · 11.2 KB
/
game.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
"""
Game state, include the pause menu and the game over menu
"""
import pygame
import pygame_gui
import constants
import numpy
import rocket
import asteroid
from game_states import GAME_STATES
background_color = (0, 0, 0)
def calc_quality(ast : asteroid.asteroid):
return - ast.avg_dist_sqrt * ast.avg_dist_samples
def ingame(surface: pygame.surface.Surface):
"""
Create the asteroids
Initialise necessary variables
"""
asteroid_count = 8
clock = pygame.time.Clock()
font = pygame.font.Font("ARCADECLASSIC.TTF", 48)
score = 0
score_img = font.render(
str(score), True, (200, 200, 200), background_color)
score_rect = score_img.get_rect()
score_rect.topright = (constants.window_width - 30, 30)
asteroids_group = pygame.sprite.Group()
for i in range(0, asteroid_count):
tmp_start_vel = [0, 0]
tmp_start_pos = [0, 0]
if numpy.random.random() < 0.5:
tmp_start_pos = [-constants.asteroid_radius, constants.window_height *
(0.1 + numpy.random.random()*0.8)]
tmp_start_vel = [constants.asteroid_start_vel, 0]
else:
tmp_start_pos = [constants.window_width + constants.asteroid_radius,
constants.window_height * numpy.random.random()]
tmp_start_vel = [-constants.asteroid_start_vel, 0]
tmp = asteroid.asteroid(asteroid_count, numpy.array(tmp_start_pos),
constants.generalise_height(constants.asteroid_radius),
numpy.array(tmp_start_vel))
asteroids_group.add(tmp)
player = rocket.rocket()
bullets = []
parent = None
parent_qual = 0
last_best_check_tick = -1000
is_running = True
while is_running:
"""
Calculate the time taken for the previous frame and cap FPS to 60
"""
dt = clock.tick(60)/1000.0
"""
Handle input and quit events
"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
return GAME_STATES.QUIT
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
ret = pause_menu(surface)
if ret != GAME_STATES.IN_GAME:
return ret
"""
If the player is dead, display the game over menu
"""
if player.status == rocket.ROCKET_STATUS.DEAD:
return game_over_menu(surface, score)
"""
Update positions and velocities of bullets, asteroids and player
"""
asteroids_group.update(asteroids_group, player, dt, bullets)
player.update(asteroids_group, dt, bullets)
for i in range(0, len(bullets)):
bullets[i].update(dt)
"""
Remove destroyed asteroids,
and create new ones if necessary
"""
to_be_removed = []
if parent is None:
parent = asteroids_group.sprites()[0]
parent_qual = calc_quality(parent)
for a in asteroids_group:
qual = calc_quality(a)
if qual > parent_qual:
parent = a
parent_qual = qual
for ast in asteroids_group:
if ast.status == asteroid.ASTEROID_STATUS.DESTROYED:
to_be_removed.append(ast)
if ast.destroyed_by_player:
"""
Increment the score and update the score display
if an asteroid has been destroyed by the player
"""
score += 1
score_img = font.render(
str(score), True, (200, 200, 200), background_color)
score_rect = score_img.get_rect()
score_rect.topright = (constants.window_width-30, 30)
for r in to_be_removed:
asteroids_group.remove(r)
tmp_start_vel = [0, 0]
tmp_start_pos = [0, 0]
if numpy.random.random() < 0.5:
tmp_start_pos = [0 - constants.asteroid_radius, constants.window_height *
(0.1 + numpy.random.random()*0.8)]
tmp_start_vel = [constants.asteroid_start_vel, 0]
else:
tmp_start_pos = [constants.window_width + constants.asteroid_radius,
constants.window_height * numpy.random.random()]
tmp_start_vel = [-constants.asteroid_start_vel, 0]
tmp = asteroid.asteroid(asteroid_count, numpy.array(tmp_start_pos),
constants.generalise_height(constants.asteroid_radius),
numpy.array(tmp_start_vel))
tmp.evolve_from(parent)
asteroids_group.add(tmp)
to_be_removed = []
"""
Clear the background and draw the player, asteroids
and the bullets
"""
surface.fill(background_color)
for i in range(0, len(bullets)):
pos = bullets[i].position
if pos[0] < 0 or pos[0] > constants.window_width or pos[1] < 0 or\
pos[1] > constants.window_height:
to_be_removed.append(bullets[i])
bullets[i].draw(surface)
for i in to_be_removed:
bullets.remove(i)
asteroids_group.draw(surface)
surface.blit(player.image, player.rect)
surface.blit(score_img, score_rect)
pygame.display.update()
def pause_menu(surface: pygame.surface.Surface):
"""
Displays the pause menu
"""
menu_rect = pygame.rect.Rect((constants.window_width//4, constants.window_width//4),
(constants.window_width//2, constants.window_height//2))
menu_rect.center = (constants.window_width//2, constants.window_height//2)
paused_background = (50, 50, 50)
paused_foreground = (200, 200, 200)
gui_manager = pygame_gui.UIManager(
(constants.window_width, constants.window_height), 'pause_menu_theme.json')
clock = pygame.time.Clock()
font = pygame.font.Font("ARCADECLASSIC.TTF",
constants.generalise_height(75))
pause_img = font.render("game paused", True,
paused_foreground, paused_background)
pause_rect = pause_img.get_rect()
pause_rect.center = (constants.generalise_width(640),
constants.generalise_height(292))
resume_button_rect = pygame.Rect((constants.generalise_width(549),
constants.generalise_height(350)),
(constants.generalise_width(164),
constants.generalise_height(41)))
quit_button_rect = pygame.Rect((constants.generalise_width(549),
constants.generalise_height(409)),
(constants.generalise_width(164),
constants.generalise_height(41)))
menu_button_rect = pygame.Rect((constants.generalise_width(549),
constants.generalise_height(468)),
(constants.generalise_width(164),
constants.generalise_height(41)))
resume_button = pygame_gui.elements.UIButton(
relative_rect=resume_button_rect, text="Resume", manager=gui_manager)
quit_button = pygame_gui.elements.UIButton(
relative_rect=quit_button_rect, text="Quit", manager=gui_manager)
menu_button = pygame_gui.elements.UIButton(
relative_rect=menu_button_rect, text="Exit to menu", manager=gui_manager)
is_paused = True
while is_paused:
dt = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
return GAME_STATES.QUIT
if event.type == pygame.USEREVENT:
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == quit_button:
return GAME_STATES.QUIT
if event.ui_element == resume_button:
return GAME_STATES.IN_GAME
if event.ui_element == menu_button:
return GAME_STATES.MAIN_MENU
gui_manager.process_events(event)
gui_manager.update(dt)
surface.fill(paused_background, menu_rect)
surface.blit(pause_img, pause_rect)
gui_manager.draw_ui(surface)
pygame.display.update(menu_rect)
def game_over_menu(surface: pygame.surface.Surface, score: int):
"""
Displays the game over menu
"""
menu_rect = pygame.rect.Rect((constants.window_width//4, constants.window_width//4),
(constants.window_width//2, constants.window_height//2))
menu_rect.center = (constants.window_width//2, constants.window_height//2)
over_background = (50, 50, 50)
over_foreground = (200, 200, 200)
gui_manager = pygame_gui.UIManager(
(constants.window_width, constants.window_height), 'game_over_theme.json')
clock = pygame.time.Clock()
font = pygame.font.Font("ARCADECLASSIC.TTF",
constants.generalise_height(75))
over_img = font.render("game over", True,
over_foreground, over_background)
over_rect = over_img.get_rect()
over_rect.center = (constants.generalise_width(640),
constants.generalise_height(292))
score_font = pygame.font.Font(
"ARCADECLASSIC.TTF", constants.generalise_height(48))
string = "Score is " + str(score)
score_img = score_font.render(string, True, over_foreground, over_background)
score_rect = score_img.get_rect()
score_rect.center = (constants.generalise_width(
549+82), constants.generalise_height(370))
restart_button_rect = pygame.Rect((constants.generalise_width(549), constants.generalise_height(
409)), (constants.generalise_width(164), constants.generalise_height(41)))
quit_button_rect = pygame.Rect((constants.generalise_width(549), constants.generalise_height(
468)), (constants.generalise_width(164), constants.generalise_height(41)))
restart_button = pygame_gui.elements.UIButton(
relative_rect=restart_button_rect, text="restart", manager=gui_manager)
quit_button = pygame_gui.elements.UIButton(
relative_rect=quit_button_rect, text="Exit to menu", manager=gui_manager)
is_paused = True
while is_paused:
dt = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
return GAME_STATES.QUIT
if event.type == pygame.USEREVENT:
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == quit_button:
return GAME_STATES.MAIN_MENU
if event.ui_element == restart_button:
return GAME_STATES.IN_GAME
gui_manager.process_events(event)
gui_manager.update(dt)
surface.fill(over_background, menu_rect)
surface.blit(over_img, over_rect)
surface.blit(score_img, score_rect)
gui_manager.draw_ui(surface)
pygame.display.update(menu_rect)