-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
372 lines (349 loc) · 13.3 KB
/
ui.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
import pygame
import data
import support
import button
from consts import *
class UI:
def __init__(self):
self.resources_font = data.assets.font(18)
self.weapons_font = data.assets.font(17)
self.info_font = data.assets.font(12)
self.help_font = data.assets.font(16)
self.fps_font = data.assets.font(14)
self.giga_font = data.assets.font(150)
self.subgiga_font = data.assets.font(60)
self.big_font = data.assets.font(40)
self.btn_font = data.assets.font(35)
self.help_font_s = data.assets.font(14)
self.time_font = data.assets.font(50)
self.resource_images = [
data.assets.get_asteroid(
UI_RESOURCE_SIZE / 2, UI_RESOURCE_DEFAULT, RESOURCE_POINTS_RANGE
)
for _ in range(PLAYER_MAX_RESOURCES)
]
self.inventory_images = {
kind: data.assets.get_asteroid(
UI_RESOURCE_SIZE / 2,
RESOURCES[kind][RESOURCE_COLID],
RESOURCE_POINTS_RANGE,
)
for kind in RESOURCES.keys()
}
self.weapon_images = {
kind: pygame.transform.scale(
data.assets.get_weapon(kind, True), (UI_WEAPON_SIZE, UI_WEAPON_SIZE)
)
for kind in WEAPONS.keys()
}
self.number_images = [
self.help_font.render(f"{i+1}", True, "white") for i in range(len(WEAPONS))
]
self.number_offset = self.number_images[0].get_width() * 1.5
self.gameover_txt = self.giga_font.render("GAME OVER", True, UI_BAD_COL)
self.gameover_subtitle = self.subgiga_font.render(
"YOU EXPLODED", True, UI_BAD_COL
)
self.win_txt = self.giga_font.render("VICTORY", True, UI_OK_COL)
self.win_subtitle = self.subgiga_font.render(
"ALL RESOURCES COLLECTED", True, UI_OK_COL
)
self.difficulty_txt = self.big_font.render(
f"MODE: {data.game.difficulty_name.upper()}", True, "white"
)
for txt in [
self.gameover_txt,
self.gameover_subtitle,
self.win_subtitle,
self.win_txt,
self.difficulty_txt,
]:
txt.set_alpha(0)
self.overlay = data.assets.get_overlay().copy()
self.help_font_s.align = pygame.FONT_CENTER
self.help_grab = self.help_font_s.render(
"CRASH IN TO THE ASTEROIDS UNTIL THEY BREAK AND GRAB THE RESOURCES (1/2)\nTAB TO SKIP",
True,
"white",
)
self.help_collect = self.help_font_s.render(
"DROP THE RESOURCES IN A BLACK HOLE TO COLLECT THEM (2/2)\nTAB TO SKIP",
True,
"white",
)
self.resources_help = self.help_font.render("E / RMB", True, "white")
self.time_txt, self.time_rect = None, None
self.finish_alpha = 0
self.overlay_alpha = 0
self.weapon_rects = []
self.restart_button = button.Button(
self.btn_font.render("RESTART", True, "white"),
(WIDTH / 2, HEIGHT / 2 + HEIGHT / 8),
BTN_COL,
BTN_HOVER,
)
self.menu_button = button.Button(
self.btn_font.render("MENU", True, "white"),
(WIDTH / 2, HEIGHT / 2 + HEIGHT / 4),
BTN_COL,
BTN_HOVER,
fixed_size=self.restart_button.text_img.get_size(),
)
txt = data.assets.font(30).render("| |", True, (160, 160, 160))
self.pause_button = button.Button(
txt,
txt.get_rect(
topright=(WIDTH - UI_RESOURCE_SIZE - UI_S * 6, UI_S * 2)
).center,
(120, 120, 120),
(200, 200, 200),
draw_outline=False,
)
self.grab_button = button.Button(
self.subgiga_font.render("E", True, "white"),
(WIDTH / 12, HEIGHT - HEIGHT / 8),
(120, 120, 120),
(200, 200, 200),
True,
)
def update(self):
if data.game.finished and data.ticks - data.game.finish_time >= 2200:
if self.restart_button.update():
data.game.restart()
if self.menu_button.update():
data.app.enter_menu()
return
if self.pause_button.update():
if data.game.paused:
data.game.pause.unpause()
else:
data.game.pause.pause()
if data.game.mobile:
self.grab_button.update()
def draw_finish(self):
self.finish_alpha += data.dt * UI_FINISH_SPEED
self.overlay_alpha += data.dt * UI_FINISH_SPEED * 2
if self.finish_alpha >= 255:
self.finish_alpha = 255
else:
for txt in [
self.gameover_txt,
self.gameover_subtitle,
self.win_subtitle,
self.win_txt,
self.difficulty_txt,
]:
txt.set_alpha(self.finish_alpha)
if self.overlay_alpha >= UI_OVERLAY_ALPHA:
self.overlay_alpha = UI_OVERLAY_ALPHA
else:
self.overlay.set_alpha(self.overlay_alpha)
data.screen.blit(self.overlay, (0, 0))
self.draw_resources_amount()
if data.game.finish_reason == "gameover":
self.draw_gameover()
else:
self.draw_win()
if data.ticks - data.game.finish_time >= 2200:
self.restart_button.draw()
self.menu_button.draw()
def draw_gameover(self):
self.draw_generic_finish(self.gameover_txt, self.gameover_subtitle)
def draw_win(self):
bottom = self.draw_generic_finish(self.win_txt, self.win_subtitle)
time = self.time_font.render(
f"{'NEW BEST ' if data.game.is_best_time else ''}TIME: {int((data.game.time_elapsed)/1000)} S",
True,
"white",
)
data.screen.blit(time, time.get_rect(midtop=(WIDTH / 2, bottom + UI_S * 2)))
def draw_generic_finish(self, title, subtitle):
title_rect = title.get_rect(midtop=(WIDTH / 2, HEIGHT / 8))
data.screen.blit(title, title_rect)
subtitle_rect = subtitle.get_rect(midtop=(WIDTH / 2, title_rect.bottom))
data.screen.blit(subtitle, subtitle_rect)
data.screen.blit(
self.difficulty_txt,
self.difficulty_txt.get_rect(midbottom=(WIDTH / 2, HEIGHT)),
)
return subtitle_rect.bottom
def draw(self):
self.draw_resources()
self.draw_map()
self.draw_health()
self.draw_weapons()
self.draw_help()
self.draw_time()
self.pause_button.draw()
if data.game.mobile:
self.grab_button.draw()
if WEB:
txt = self.info_font.render(
"GRAPHICS REDUCED FOR WEB", True, (120, 120, 120)
)
data.screen.blit(txt, txt.get_rect(bottomleft=(0, HEIGHT)))
def draw_time(self):
if not data.game.paused:
self.time_txt = self.resources_font.render(
f"{int((data.ticks-data.game.start_time)/1000)} S", True, "white"
)
self.time_rect = self.time_txt.get_rect(
topright=(self.pause_button.rect.left - UI_S, UI_S)
)
if self.time_txt is not None:
data.screen.blit(self.time_txt, self.time_rect)
def draw_help(self):
midbottom = (WIDTH / 2, HEIGHT - UI_S * 3)
r1, r2 = (
self.help_grab.get_rect(midbottom=midbottom),
self.help_collect.get_rect(midbottom=midbottom),
)
mpos = pygame.mouse.get_pos()
if (
data.game.mobile
and (r1.collidepoint(mpos) or r2.collidepoint(mpos))
and pygame.mouse.get_pressed()[pygame.BUTTON_LEFT - 1]
):
data.game.grabbed_one_resource = True
data.game.collected_one_resource = True
if not data.game.grabbed_one_resource:
data.screen.blit(self.help_grab, r1)
return
if not data.game.collected_one_resource:
data.screen.blit(self.help_collect, r2)
def draw_weapons(self):
y = UI_HEALTH_SIZE[1] + UI_S
self.weapon_rects = []
for i, kind in enumerate(list(WEAPONS.keys())):
price = WEAPONS[kind][WEAPON_PRICEID]
resource = WEAPONS[kind][WEAPON_RESOURCEID]
current = data.game.inventory[resource]
color = (
UI_OK_COL
if current >= price or resource in data.game.starter_inventory
else UI_BAD_COL
)
weapon_img = self.weapon_images[kind]
res_img = self.inventory_images[resource]
number_img = self.number_images[i]
text = (
f"{current}/{price}"
if resource not in data.game.starter_inventory
else "FREE"
)
amount_img = self.weapons_font.render(text, True, color)
weapon_rect = weapon_img.get_rect(topleft=(0, y))
number_rect = number_img.get_rect(midleft=(UI_S, weapon_rect.centery))
data.screen.blit(number_img, number_rect)
weapon_rect.midleft = (self.number_offset + UI_S, number_rect.centery)
self.weapon_rects.append((weapon_rect, kind))
data.screen.blit(weapon_img, weapon_rect)
amount_rect = amount_img.get_rect(
midleft=(weapon_rect.right + UI_S, weapon_rect.centery)
)
data.screen.blit(amount_img, amount_rect)
data.screen.blit(
res_img,
res_img.get_rect(
midleft=(amount_rect.right + UI_S, amount_rect.centery)
),
)
y += UI_S + weapon_rect.h
def draw_health(self):
rect = ((0, 0), UI_HEALTH_SIZE)
br = UI_BR * 3, 0, 0, 0, UI_BR * 3
pygame.draw.rect(data.screen, UI_HEALTH_BG, rect, 0, *br)
pygame.draw.rect(
data.screen,
UI_HEALTH_FILL,
(
0,
0,
UI_HEALTH_SIZE[0] * data.player.health / PLAYER_HEALTH,
UI_HEALTH_SIZE[1],
),
0,
*br,
)
pygame.draw.rect(data.screen, UI_HEALTH_OUTLINE, rect, 1, *br)
def draw_resources(self):
bg_rect = (
WIDTH - UI_S * 2 - UI_RESOURCE_SIZE,
0,
UI_S * 2 + UI_RESOURCE_SIZE,
UI_RESOURCE_SIZE * PLAYER_MAX_RESOURCES + UI_S * (PLAYER_MAX_RESOURCES + 1),
)
pygame.draw.rect(
data.screen,
UI_BG
if data.ticks - data.player.pickup_fail_time >= 1000
else (200, 0, 40),
bg_rect,
0,
UI_BR,
0,
0,
UI_BR,
0,
)
pygame.draw.rect(data.screen, UI_OUTLINE, bg_rect, 1, UI_BR, 0, 0, UI_BR, 0)
y = UI_S
for i in range(PLAYER_MAX_RESOURCES):
if len(data.player.resources) - 1 >= i:
image = pygame.transform.scale(
data.player.resources[i].image, (UI_RESOURCE_SIZE, UI_RESOURCE_SIZE)
)
else:
image = self.resource_images[i]
data.screen.blit(image, (WIDTH - UI_S - UI_RESOURCE_SIZE, y))
y += UI_S + UI_RESOURCE_SIZE
self.draw_resources_amount()
if data.player.hovering_resources:
mpos = pygame.mouse.get_pos()
data.screen.blit(
self.resources_help,
self.resources_help.get_rect(topleft=(mpos[0] + 10, mpos[1] + 10)),
)
def draw_resources_amount(self):
txt = self.resources_font.render(
f"RESOURCES {data.game.collected_resources}/{data.game.resources_amount}",
True,
"white",
)
data.screen.blit(txt, txt.get_rect(midtop=(WIDTH / 2, 0)))
def draw_map(self):
map_rect = pygame.Rect(0, 0, UI_MAP_SIZE, UI_MAP_SIZE).move_to(
bottomright=(WIDTH, HEIGHT)
)
pygame.draw.rect(data.screen, (10, 10, 20), map_rect, 0, UI_BR, UI_BR, 0, 0, 0)
res = data.game.resources.sprites()
for i in range(
min(len(data.game.resources), (UI_MAX_RESOURCES if data.fps > 60 else 50))
):
data.screen.set_at(
(support.project_map(res[i].rect.center, map_rect.topleft)), "blue"
)
for bh in data.game.blackholes:
pygame.draw.circle(
data.screen,
"yellow",
support.project_map(bh.pos, map_rect.topleft),
max(SCALE_RES(3), 2),
)
if data.game.pack is not None:
for enemy in data.game.pack.enemies:
pygame.draw.circle(
data.screen,
"green",
support.project_map(enemy.rect.center, map_rect.topleft),
2,
)
pygame.draw.circle(
data.screen,
"red",
support.project_map(data.player.rect.center, map_rect.topleft),
2,
)
pygame.draw.rect(data.screen, UI_BG, map_rect, 1, UI_BR, UI_BR, 0, 0, 0)
txt = self.fps_font.render(f"{int(data.fps)} FPS", True, (120, 120, 120))
data.screen.blit(txt, txt.get_rect(bottomright=map_rect.topright))