-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
544 lines (465 loc) · 16.5 KB
/
main.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import asyncio
import os
import sys
import hashlib
import pygame
import unidecode
from button import Button
from player import Player
from level import Level
import colors
import config
# INITIALIZE PYGAME
pygame.init()
pygame.mixer.music.set_endevent(config.SONG_ENDEVENT)
# WINDOW CONSTANTS
PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
clock = pygame.time.Clock()
# CREATE WINDOW
pygame.display.set_caption(config.DISP_TIT)
pygame.display.set_icon(pygame.image.load(config.DISP_ICO))
display = pygame.display.set_mode((config.DISP_WID, config.DISP_HEI), pygame.RESIZABLE)
display_rect = display.get_rect()
game = pygame.Surface((config.DISP_WID, config.DISP_HEI))
def render():
if display_rect.h != config.DISP_HEI or display_rect.w != config.DISP_WID:
pygame.transform.scale(game, (display_rect.w, display_rect.h), display)
else:
display.blit(game, (0, 0))
# CONSTANTS
FONTS = {
"normal": pygame.font.Font(config.FONT_TYPE, config.FONT_SIZE_NORMAL),
"big": pygame.font.Font(config.FONT_TYPE, config.FONT_SIZE_BIG),
"small": pygame.font.Font(config.FONT_TYPE, config.FONT_SIZE_SMALL),
}
def add_songs_in_folder(folder, songs, recursive=True):
for item in os.listdir(folder):
path = os.path.join(PATH, folder, item)
if os.path.isfile(path):
song_title = unidecode.unidecode(item.split(".")[0].replace("_", " "))
if len(song_title) > 16:
song_title = song_title[:7] + "[\u2026]" + song_title[-6:]
songs.append([song_title, path])
if recursive and os.path.isdir(path):
print(item)
add_songs_in_folder(path, songs, recursive)
def pager(length, cut):
return [slice(i, min(i + cut, length)) for i in range(0, length, cut)]
SONGS = [] # [[song_title, song_path], ...]
add_songs_in_folder(os.path.join(PATH, "assets", "songs"), SONGS)
# GAME LOOP
state = "start"
menu_background = pygame.image.load(config.MENU_BACKGROUND).convert()
player = ...
async def menu_start_loop():
global clock, display, display_rect, game, FONTS, SONGS, state, player, menu_background
title = FONTS["big"].render(config.GAME_TITLE, False, colors.neon["blue"])
author = FONTS["small"].render(config.GAME_AUTHOR, False, colors.neon["red"])
author2 = FONTS["small"].render(config.GAME_AUTHOR2, False, colors.neon["red"])
play_button = Button(
colors.neon["fucsia"],
300,
200,
200,
70,
image=FONTS["normal"].render("PLAY", False, (0, 0, 0)),
)
help_button = Button(
colors.neon["fucsia"],
300,
280,
200,
70,
image=FONTS["normal"].render("HELP", False, (0, 0, 0)),
)
exit_button = Button(
colors.neon["fucsia"],
300,
360,
200,
70,
image=FONTS["normal"].render("EXIT", False, (0, 0, 0)),
)
# LOOP
while state == "start":
# EVENTS
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = "close"
if event.type == pygame.VIDEORESIZE:
display_rect = display.get_rect()
config.resize = (
float(display_rect.w) / float(config.DISP_WID),
float(display_rect.h) / float(config.DISP_HEI),
)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
state = "close"
# TIME
clock.tick(config.BASE_FPS)
# LOGIC
if play_button.mouseclick():
state = "choose"
if help_button.mouseclick():
state = "help"
if exit_button.mouseclick():
state = "close"
# RENDER
game.fill((0, 0, 0))
game.blit(menu_background, (0, 0))
game.blit(title, (90, 20))
game.blit(author, (100, 100))
game.blit(author2, (100, 140))
play_button.draw(game)
help_button.draw(game)
exit_button.draw(game)
# FLIP
render()
pygame.display.update()
# ASYNC LOOP SLEEP
await asyncio.sleep(0)
async def menu_help_loop():
global clock, display, display_rect, game, FONTS, SONGS, state, player, menu_background
help_page = pygame.image.load(config.HELP_IMAGE)
help_page.set_colorkey((255, 255, 255))
seen = False
while state == "help":
# EVENTS
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = "close"
if event.type == pygame.VIDEORESIZE:
display_rect = display.get_rect()
config.resize = (
display_rect.w / config.DISP_WID,
display_rect.h / config.DISP_HEI,
)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
state = "start"
if event.type == pygame.MOUSEBUTTONDOWN:
seen = True
if event.type == pygame.MOUSEBUTTONUP and seen:
state = "start"
# RENDER
game.fill((240, 250, 240))
game.blit(help_page, (0, 0))
# FLIP
render()
pygame.display.update()
# TIME
clock.tick(config.BASE_FPS)
# ASYNC LOOP SLEEP
await asyncio.sleep(0)
async def menu_choose_loop():
# LOAD GLOBALS
global clock, display, display_rect, game, FONTS, SONGS, state, player, menu_background
# PREVIOUS STEPS
levels = []
page_back = Button(
pygame.Color("darkgray"),
10,
config.DISP_HEI - 80,
config.DISP_WID / 2 - 30,
70,
outcolor=pygame.Color("darkgray"),
image=FONTS["normal"].render("<", False, (0, 0, 0)),
)
page_forward = Button(
pygame.Color("gray"),
20 + config.DISP_WID / 2,
config.DISP_HEI - 80,
config.DISP_WID / 2 - 30,
70,
image=FONTS["normal"].render(">", False, (0, 0, 0)),
)
page = 0
pages = pager(len(SONGS), 5)
for song in SONGS:
title = FONTS["normal"].render(song[0].upper(), False, (0, 0, 0))
levels.append(
[
Button(
colors.neon[
list(colors.neon)[
int(
hashlib.md5(song[0].encode()).hexdigest(), 16
) # random but determined by song name
% len(colors.neon)
]
],
10,
10 + 80 * (len(levels) % 5),
config.DISP_WID - 20,
70,
image=title,
),
song[1],
]
)
mouse_rel = False
# ACTUAL MENU LOOP
while state == "choose":
# EVENTS
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = "close"
if event.type == pygame.VIDEORESIZE:
display_rect = display.get_rect()
config.resize = (
display_rect.w / config.DISP_WID,
display_rect.h / config.DISP_HEI,
)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
state = "start"
if event.type == pygame.MOUSEBUTTONUP:
mouse_rel = True
# TIME
clock.tick(config.BASE_FPS)
# LOGIC
for level in levels[pages[page]]:
if level[0].mouseclick() and mouse_rel:
player = level[1]
state = "loading"
if page_back.mouseclick() and mouse_rel:
page -= 1
if page < 0:
page += 1
if page == 0:
page_back.color = pygame.Color("darkgray")
page_back.outcolor = pygame.Color("darkgray")
if page != len(pages) - 1:
page_forward.color = pygame.Color("gray")
page_forward.outcolor = pygame.Color("gray") + pygame.Color(
15, 15, 15, 15
)
mouse_rel = False
if page_forward.mouseclick() and mouse_rel:
page += 1
if page > len(pages) - 1:
page -= 1
if page != 0:
page_back.color = pygame.Color("gray")
page_back.outcolor = pygame.Color("gray") + pygame.Color(15, 15, 15, 15)
if page == len(pages) - 1:
page_forward.color = pygame.Color("darkgray")
page_forward.outcolor = pygame.Color("darkgray")
mouse_rel = False
# RENDER
game.fill((0, 0, 0))
game.blit(menu_background, (0, 0))
page_back.draw(game)
page_forward.draw(game)
for level in levels[pages[page]]:
level[0].draw(game)
# FLIP
render()
pygame.display.update()
# ASYNC LOOP SLEEP
await asyncio.sleep(0)
async def loading_loop():
global clock, display, display_rect, game, FONTS, SONGS, state, player, menu_background
while state == "loading":
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = "close"
if event.type == pygame.VIDEORESIZE:
display_rect = display.get_rect()
config.resize = (
display_rect.w / config.DISP_WID,
display_rect.h / config.DISP_HEI,
)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
state = "choose"
pygame.mixer.music.unload()
game.fill((0, 0, 0))
game.blit(menu_background, (0, 0))
game.blit(
FONTS["big"].render("LOADING...", False, colors.neon["red"]), (30, 30)
)
song_title = unidecode.unidecode(
os.path.basename(str(player)).split(".")[0].replace("_", " ")
).upper()
if len(song_title) > 23:
song_title = song_title[:10] + "[\u2026]" + song_title[-9:]
game.blit(
FONTS["small"].render(song_title, False, colors.neon["red"]), (50, 100)
)
render()
pygame.display.update()
try:
player = Player(Level(player))
state = "level"
except pygame.error:
# BLIT ERROR MESSAGE
game.blit(
FONTS["small"].render(
"> ERROR LOADING SONG...",
False,
(255, 0, 0),
),
(40, 200),
)
game.blit(
FONTS["small"].render(
"> CHECK FILE FORMAT...",
False,
(255, 0, 0),
),
(40, 250),
)
game.blit(
FONTS["small"].render(
"> CLICK ANYWHERE...",
False,
(255, 0, 0),
),
(40, 300),
)
game.blit(
FONTS["small"].render(
"> TO GO TO MAIN MENU...",
False,
(255, 0, 0),
),
(40, 350),
)
render()
pygame.display.update()
# EVENT LOOP
while state == "loading":
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = "close"
if event.type == pygame.VIDEORESIZE:
display_rect = display.get_rect()
config.resize = (
display_rect.w / config.DISP_WID,
display_rect.h / config.DISP_HEI,
)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
state = "start"
if event.type == pygame.MOUSEBUTTONDOWN:
state = "choose"
# CLOCK
clock.tick(config.BASE_FPS)
await asyncio.sleep(0)
async def level_loop():
global clock, display, display_rect, game, FONTS, SONGS, state, player, menu_background
heart = pygame.image.load(config.HEART_ICON)
shield_icon = pygame.image.load(config.SHIELD_ICON)
shield_icon.set_colorkey((255, 255, 255))
damage = pygame.Surface((config.DISP_WID, config.DISP_HEI))
damage.fill((20, 0, 0, 30))
won = False
dt = 1/config.BASE_FPS
lose_played = False
lose = pygame.mixer.Sound("assets/sounds/lose.ogg")
while state == "level":
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = "close"
if event.type == pygame.VIDEORESIZE:
display_rect = display.get_rect()
config.resize = (
display_rect.w / config.DISP_WID,
display_rect.h / config.DISP_HEI,
)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
state = "start"
pygame.mixer.music.unload()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
if not player.run:
player.start()
player.spacebar()
if event.type == config.SONG_ENDEVENT:
won = True
if player.ended:
state = "start"
# LOGIC
if player.run:
player.update(dt)
# RENDER
player.draw(game)
lifes = FONTS["small"].render(
str(int(player.life)), False, colors.neon["orange"]
)
shield = FONTS["small"].render(
str(int(player.shield)), False, colors.neon["orange"]
)
lifes_rect = lifes.get_rect()
lifes_rect.topright = (700, 20)
game.blit(lifes, lifes_rect.topleft)
game.blit(heart, (lifes_rect.right, lifes_rect.center[1] - 14))
game.blit(shield, (lifes_rect.left, lifes_rect.top + 30))
game.blit(shield_icon, (lifes_rect.right, lifes_rect.center[1] + 16))
score = FONTS["small"].render(
str(int(player.score)), False, colors.neon["orange"]
)
combo = FONTS["small"].render(
" x " + str(int(player.combo)), False, colors.neon["orange"]
)
score_rect = score.get_rect()
score_rect.x = 20
score_rect.y = 20
game.blit(score, score_rect.topleft)
game.blit(combo, score_rect.topright)
if won:
end = FONTS["big"].render(config.WIN_MESSAGE, False, colors.metal["gold"])
end_rect = end.get_rect()
end_rect.center = (config.DISP_WID // 2, config.DISP_HEI // 2)
game.blit(end, end_rect.topleft)
if not player.life:
end = FONTS["big"].render(
config.DEATH_MESSAGE, False, colors.metal["silver"]
)
end_rect = end.get_rect()
end_rect.center = (config.DISP_WID // 2, config.DISP_HEI // 2)
game.blit(end, end_rect.topleft)
if not lose_played:
pygame.mixer.stop()
lose.play()
lose_played = True
# FLIP
render()
pygame.display.update()
# TIME
dt = clock.tick(config.BASE_FPS)
# ASYNC LOOP SLEEP
await asyncio.sleep(0)
# SAVING SCORE WHEN EXITING
else:
player.save()
# ASYNC LOOP SLEEP
await asyncio.sleep(0)
async def main():
global clock, display, display_rect, game, FONTS, SONGS, state, player, menu_background
while state != "close":
# EMERGENCY EXIT
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = "close"
if event.type == pygame.VIDEORESIZE:
display_rect = display.get_rect()
config.resize = (
display_rect.w / config.DISP_WID,
display_rect.h / config.DISP_HEI,
)
# START MENU
if state == "start":
await menu_start_loop()
# HELP MENU
if state == "help":
await menu_help_loop()
# LEVEL SELECTOR
if state == "choose":
await menu_choose_loop()
# LOADING SCREEN
if state == "loading":
await loading_loop()
# LEVEL ITSELF
if state == "level":
await level_loop()
# ASYNC LOOP SLEEP
await asyncio.sleep(0)
pygame.quit()
sys.exit(0)
asyncio.run(main())