-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.py
384 lines (347 loc) · 11.9 KB
/
assets.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
373
374
375
376
377
378
379
380
381
382
383
384
import pygame
import support
from consts import *
import data
import math
import json
import random
class Assets:
def __init__(self):
self.stars_cache = {}
self.dust_cache = {}
self.black_overlay = pygame.Surface((WIDTH, HEIGHT))
self.easteregg = pygame.transform.scale_by(
pygame.image.load("assets/ee.png").convert_alpha(), 0.5
)
self.completed = self.make_completed()
self.make_dust()
self.make_explosions()
self.make_weapons()
self.make_enemies()
self.load_sounds()
def load_sound(self, name, vol=1):
sound = pygame.mixer.Sound(f"assets/sfx/{name}.ogg")
sound.set_volume(vol)
return {name: {"obj": sound, "vol": vol}}
def load_sounds(self):
sound = self.load_sound
self.sounds = {
**sound("asteroid_hit", 0.4),
**sound("power_unlock", 0.7),
**sound("big_explosion", 1),
**sound("grab", 0.32), ##
**sound("button_click", 0.8), #
**sound("button_hover", 0.8), ##
**sound("collect", 0.2),
**sound("player_damage", 1),
**sound("small_explosion", 1.2),
**sound("suck", 0.8),
**sound("wh_attack", 1),
**sound("gameover", 1),
**sound("supernova", 0.5),
**sound("teleport", 0.8), #
**sound("worm_hole", 0.5),
**sound("shield", 0.3),
**sound("victory", 1.2),
}
def play(self, name):
self.sounds[name]["obj"].play()
def music_play(self, name):
pygame.mixer.music.unload()
pygame.mixer.music.load(f"assets/{name}.ogg")
pygame.mixer.music.play(-1, 0, 1000)
def music_pause(self):
pygame.mixer.music.pause()
def music_resume(self):
pygame.mixer.music.unpause()
def update_volumes(self):
pygame.mixer.music.set_volume(data.app.music_vol)
for s in self.sounds.values():
s["obj"].set_volume(s["vol"] * data.app.fx_vol)
with open("volume.json", "w") as file:
json.dump({"music": data.app.music_vol, "fx": data.app.fx_vol}, file)
def get_completed(self):
return self.completed.copy()
def make_completed(self):
font = self.font(12)
txt = font.render("VICTORY", True, "green")
w = txt.get_width() + SCALE_RES(10)
base = pygame.Surface((w, w), pygame.SRCALPHA)
txt = pygame.transform.rotate(txt, 30)
pygame.draw.circle(base, (0, 20, 0), (w / 2, w / 2), w / 2)
pygame.draw.circle(base, "green", (w / 2, w / 2), w / 2, 3)
base.blit(txt, txt.get_rect(center=(w / 2, w / 2)))
return base
def get_overlay(self):
return self.black_overlay
def font(self, size=20):
return pygame.Font("assets/font.ttf", SCALE_RES(size))
def get_weapon(self, kind, color_supernova=False):
res = self.weapons[kind].copy()
if kind == "supernova" and color_supernova:
res.fill((255, 200, 0), special_flags=pygame.BLEND_RGBA_MULT)
return res
def get_particle(self):
return self.wh_particle
def get_wormhole(self, whsize, whw=15):
wh = pygame.Surface((whsize, whsize), pygame.SRCALPHA)
whrad, who = whsize / 2 - whsize / 6, whsize / 10
pygame.draw.circle(
wh,
"green",
(who + whrad, who + whrad),
whrad,
whw,
False,
True,
False,
False,
)
pygame.draw.circle(
wh,
"green",
(whsize - whrad - who, who + whrad),
whrad,
whw,
True,
False,
False,
False,
)
pygame.draw.circle(
wh,
"green",
(whrad + who, whsize - whrad - who),
whrad,
whw,
False,
False,
True,
False,
)
pygame.draw.circle(
wh,
"green",
(whsize - whrad - who, whsize - whrad - who),
whrad,
whw,
False,
False,
False,
True,
)
wh.blit(self.get_dust(whsize, (0, 255, 0, 150), False), (0, 0))
return wh
def get_shield(self):
w = WEAPONS["shield"][WEAPON_SIZEID]
cw = w / SHIELD_MULT
surf = pygame.Surface((w, w), pygame.SRCALPHA)
mask_surf = pygame.Surface((w, w), pygame.SRCALPHA)
pygame.draw.circle(mask_surf, "white", (w / 2, w / 2), cw / 2)
segment = int(cw / 24)
on = True
color = RESOURCES["quartz"][RESOURCE_COLID]
x = w / 2 - cw / 2
while x < w / 2 + cw / 2:
if on:
pygame.draw.line(
surf,
color,
(x, w / 2 - cw / 2),
(x, w / 2 + cw / 2),
segment,
)
x += segment
else:
x += segment * 2
on = not on
y = w / 2 - cw / 2
on = True
while y < w / 2 + cw / 2:
if on:
pygame.draw.line(
surf,
color,
(w / 2 - cw / 2, y),
(w / 2 + cw / 2, y),
segment,
)
y += segment
else:
y += segment * 2
on = not on
surf.blit(mask_surf, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
pygame.draw.circle(surf, color, (w / 2, w / 2), cw / 2, int(segment / 1.5))
surf.blit(self.get_dust(w, color, False), (0, 0))
return surf
def make_weapons(self):
self.wh_particle = self.get_dust(WHITEHOLE_PARTICLE_SIZE, (0, 100, 200, 150))
self.weapons = {
"red_hole": self.get_blackhole(
WEAPONS["red_hole"][WEAPON_SIZEID] / 2, (255, 0, 0), mult=1.8
),
"shield": self.get_shield(),
"purple_hole": self.get_blackhole(
WEAPONS["purple_hole"][WEAPON_SIZEID] / 2, (120, 0, 255), mult=2
),
"white_hole": self.get_blackhole(
WEAPONS["white_hole"][WEAPON_SIZEID] / 2, (0, 100, 255), "white", 2
),
"supernova": self.get_blackhole(
WEAPONS["supernova"][WEAPON_SIZEID],
(255, 255, 255),
(255, 255, 255),
1.4,
),
"worm_hole": self.get_wormhole(WEAPONS["worm_hole"][WEAPON_SIZEID]),
"worm_holeB": self.get_wormhole(
WEAPONS["worm_hole"][WEAPON_SIZEID] * 2, 15 * 2
),
}
def make_explosions(self):
self.explosions = [
pygame.Surface(((200), (200)), pygame.SRCALPHA) for _ in range(2)
]
for exp, s in zip(self.explosions, (10, 2)):
pygame.draw.circle(exp, "white", (100, 100), 100, s)
def get_explosion(self, color, i):
part = self.explosions[i].copy()
part.fill(color, special_flags=pygame.BLEND_RGBA_MULT)
return part
def get_star(self, size, color):
if (size, color) in self.stars_cache:
return self.stars_cache[(size, color)]
result = pygame.Surface((size, size), pygame.SRCALPHA)
if size == 1:
result.fill(color)
else:
pygame.draw.circle(result, color, (size // 2, size // 2), size // 2)
self.stars_cache[(size, color)] = result
return result
def get_player(self):
img = pygame.Surface(PLAYER_SIZE, pygame.SRCALPHA)
w, h = PLAYER_SIZE
cx = w / 10
cyf = h / 3.2
cyn = h / 5.2
td = cx * 1.5
tx = w / 16
ty = h / 8
tm = h / 3
points = [
# left wing
(0, h - h / 5),
# top pointy thing
(w / 2 - cx, cyf),
(w / 2 - cx, cyn),
(w / 2, 0),
(w / 2 + cx, cyn),
(w / 2 + cx, cyf),
# right wing
(w, h - h / 5),
# right thruster
(w / 2 + td, h - tm),
(w / 2 + td, h - tm + ty),
(w / 2 + td - tx, h - tm + ty),
(w / 2 + td - tx, h - tm),
# midpoint
(w / 2, h - h / 3),
# left thruster
(w / 2 - td + tx, h - tm),
(w / 2 - td + tx, h - tm + ty),
(w / 2 - td, h - tm + ty),
(w / 2 - td, h - tm),
]
pygame.draw.polygon(img, PLAYER_COL, points)
return img
def get_dust(self, size, color, cache=True):
if cache and (size, color) in self.dust_cache:
return self.dust_cache[(size, color)]
result = pygame.transform.scale(self.dust_surf, (size, size))
result.fill(color, special_flags=pygame.BLEND_RGBA_MULT)
if cache:
self.dust_cache[(size, color)] = result
return result
def make_dust(self):
s = SCALE_RES(300)
self.dust_surf = pygame.Surface((s, s), pygame.SRCALPHA)
for x in range(self.dust_surf.get_width()):
for y in range(self.dust_surf.get_height()):
dist = math.sqrt((x - s // 2) ** 2 + (y - s // 2) ** 2)
self.dust_surf.set_at(
(x, y),
pygame.Color(
255,
255,
255,
max(0, min(255, int((1 - ((dist / (s // 2)) ** 1.2)) * 255))),
),
)
def get_asteroid(self, radius, color, points_range=ASTEROID_POINTS_RANGE):
points = support.asteroid_points(random.randint(*points_range), radius, 0.3)
surf = pygame.Surface((radius * 2, radius * 2), pygame.SRCALPHA)
pygame.draw.polygon(
surf, color, [(p[0] + radius, p[1] + radius) for p in points]
)
return surf
def get_blackhole(
self, size, color=(255, 220, 0), centercolor="black", mult=BLACKHOLE_MULT
):
dustsize = int(size * mult)
result = pygame.Surface((dustsize, dustsize), pygame.SRCALPHA)
circle = pygame.Surface((size, size), pygame.SRCALPHA)
pygame.draw.circle(circle, centercolor, (size // 2, size // 2), size // 2)
dust = self.get_dust(dustsize, color, False)
result.blit(dust, (0, 0))
result.blit(
circle, circle.get_rect().move_to(center=(dustsize / 2, dustsize / 2))
)
return result
def get_enemy(self, type):
return self.enemies[type]
def make_enemy_green(self, w, h):
return [
(0, h),
(w / 2, 0),
(w, h),
]
def make_enemy_red(self, w, h):
return [
(0, h),
(w / 2, 0),
(w, h),
]
def make_enemy_pink(self, w, h):
return [(0, h), (w / 2, 0), (w, h), (w / 2, h - h / 4)]
def make_enemy_yellow(self, w, h):
bw = w / 10
return [
(0, h),
(w / 2 - bw / 2, h / 4),
(w / 2, 0),
(w / 2 + bw / 2, h / 4),
(w, h),
(w / 2, h - h / 4),
]
def make_enemy_blue(self, w, h):
tw = w / 2.5
tl, tr = w / 2 - tw / 2, w / 2 + tw / 2
sth = h - h / 1.8
tm = h - h / 3
return [
(0, h),
(tl / 2, sth),
(tl, tm),
(w / 2, 0),
(tr, tm),
(tr + tl / 2, sth),
(w, h),
(w / 2, h - h / 6),
]
def make_enemies(self):
self.enemies = {}
for enemy in ENEMIES:
size, col = ENEMIES[enemy][ENEMY_SIZEID], ENEMIES[enemy][ENEMY_COLID]
img = pygame.Surface(size, pygame.SRCALPHA)
pygame.draw.polygon(img, col, getattr(self, f"make_enemy_{enemy}")(*size))
self.enemies[enemy] = img