forked from Anand1310/summer-code-jam-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels.py
746 lines (652 loc) · 24.7 KB
/
levels.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
"""Examples for designing levels."""
import json
import logging
import time
from copy import copy
from random import randrange
from threading import Thread
from typing import Callable, Dict, List, Union
import blessed
from blessed.keyboard import Keystroke
from core.maze import AIR, Box, Maze
from core.player import MenuCursor, Player
from core.render import Render
from core.sound import enter_game_sound, play_level_up_sound, stop_bgm
from core.table import make_table
from game import (
CREDITS, END, INFINITE, LEADERBOARD, LOSE, NEXT_SCENE, PAUSE, PLAY, QUIT,
RESET, TITLE, TUTORIAL, Scene
)
from utils import Boundary, Vec # type: ignore
term = blessed.Terminal()
render = Render()
class Menu(Scene):
"""Example of a title scene."""
def __init__(
self,
txt: List[str],
choices: List[str],
action_on_choice: Callable,
action_on_first_frame: Callable = None,
) -> None:
super().__init__()
self.txt = txt
self.choices = choices
self.built_once = False
self.action_on_choice = action_on_choice
self.action_on_first_frame = action_on_first_frame
self.first_frame = True
self.current_frame: str = ""
self.menu: MenuCursor = None
def build(self) -> None:
"""Build self.txt"""
if self.action_on_first_frame and not self.built_once:
self.built_once = True
self.choices = self.action_on_first_frame() + self.choices
self.current_frame = term.black_on_peachpuff2 + term.clear
width = (self.width - max(len(s) for s in self.txt)) // 2
txt = self.txt + list(self.choices)
height = self.height // 2 - len(txt) // 2
for i in range(len(txt)):
self.current_frame += term.move_xy(x=width, y=height + i)
self.current_frame += txt[i]
self.menu = MenuCursor(
Vec(width - 3, height + 2),
Vec(0, len(self.choices) - 1),
list(self.choices),
)
def next_frame(self, val: Keystroke) -> Union[str, int]:
"""Returns next frame to render"""
# no need to update the frame anymore
if self.first_frame:
self.build()
self.first_frame = False
render(self.current_frame)
self.menu.render()
# return self.current_frame
elif val.is_sequence and (257 < val.code < 262):
self.menu.move(val.name)
self.menu.render()
elif str(val) == " " or val.name == "KEY_ENTER":
choice = self.menu.options[self.menu.selected]
play_level_up_sound()
return self.action_on_choice(choice)
return ""
# return ""
def reset(self) -> None:
"""Reset has no use for title scene."""
self.first_frame = True
self.menu.selected = 0
self.menu.coords = copy(self.menu.l_bounds)
class Level(Scene):
"""First basic game"""
def __init__(self, level: str = "1") -> None:
super().__init__()
with open(f"levels/{level}.json", "r") as f:
data = json.load(f)
self.instructions: Dict = {}
self.dialogues = data.pop("dialogues", None)
if self.dialogues:
for dialogue in self.dialogues:
hit_point, coordinate, text = dialogue
self.instructions[tuple(hit_point)] = [coordinate, text]
self.maze = Maze.load(data=data)
self.level_boundary = Boundary(
len(self.maze.char_matrix[0]),
len(self.maze.char_matrix),
self.maze.top_left_corner,
term,
)
self.end_loc = self.maze.end
self.first_act = True # set up what to do at the start of the level
self.show_level = 40 # number of frames to show the level
self.wait = self.show_level
self.maze_is_visible = False
self.reward_on_goal = 0
self.player: Player = Player()
for box in self.maze.boxes:
# move to top-left corner of maze + scale and extend width
# + move to top-left corner of box
box.loc = self.maze.mat2screen(box.loc) - (1, 1)
self.player.inside_box[box.col] = False
def build_level(self) -> None:
"""Load current level specific attributes"""
self.player.start_loc = copy(self.maze.start)
self.player.collision_count = 0
self.reward_on_goal = 200
def next_frame(self, val: Keystroke) -> Union[str, int]:
"""Draw next frame."""
if self.first_act:
self.first_act = False
self.show_level = 30
self.build_level()
play_level_up_sound()
# show the maze for 1 sec
frame = term.clear
# removes the main maze after 2 sec
frame = self.get_boundary_frame() # type: ignore
frame += self.maze.map
render(frame)
for box in self.maze.boxes:
box.render(self.player)
self.player.start()
return ""
elif self.wait > 0:
# block any actions from player and then remove maze
if self.instructions and self.wait == self.show_level:
Thread(target=self.instruct_player, daemon=True).start()
self.wait -= 1
if self.wait == 0:
self.remove_maze(0)
elif val.is_sequence and (257 < val.code < 262):
# update player
self.player.update(val, self.maze)
# check if game ends
if all(self.player.avi.coords == self.end_loc):
self.player.score.value += self.reward_on_goal
return NEXT_SCENE
if self.instructions:
Thread(target=self.instruct_player, daemon=True).start()
self.render()
elif val.lower() == "e":
self.player.player_movement_sound(maze=self.maze)
elif val.lower() == "q":
return PAUSE
elif val.lower() == "r":
return RESET
elif val.lower() == "h":
if not self.maze_is_visible:
self.maze_is_visible = True
render(self.maze.map)
for box in self.maze.boxes:
box.render(self.player)
self.player.render()
else:
self.remove_maze(0)
# things that should update on every frame goes here
if not self.wait > 0:
self.player.score.update(
player_inside_box=any(self.player.inside_box.values())
)
if self.player.score.value <= 0:
return LOSE
return ""
def render(self, hard: bool = False) -> None:
"""Refreshing the scene"""
if hard:
render(term.clear, bg_col="lightskyblue1")
for box in self.maze.boxes:
box.render(self.player)
render(self.level_boundary.map)
# render player
render(term.move_xy(*self.end_loc) + "&")
self.player.render()
def remove_maze(self, sleep: float = 2) -> None:
"""Erase main maze"""
self.maze_is_visible = False
time.sleep(sleep)
render(self.get_boundary_frame())
self.player.render()
for box in self.maze.boxes:
box.render(self.player)
def get_boundary_frame(self) -> str:
"""Get the frame with only boundary"""
frame = term.clear
frame += self.level_boundary.map
frame += term.move_xy(*self.end_loc) + "&" # type: ignore
return frame
def player_in_boxes(self, player: Player) -> bool:
"""Return True if player in any boxes"""
k = None
for box in self.maze.boxes:
k = box.player_in_box(player)
if k:
return True
return False
def reset(self) -> None:
"""Reset this level"""
for box in self.maze.boxes:
box.needs_cleaning = False
self.player.start()
self.first_act = True
global prev_text, prev_text_loc
prev_text = ""
def instruct_player(self) -> None:
"""Instructions"""
global prev_text, prev_text_loc
player_loc = tuple(self.maze.screen2mat(self.player.avi.coords))
if player_loc not in self.instructions.keys():
return
if prev_text != "":
frame = term.move_xy(*prev_text_loc) + " " * len(prev_text)
render(frame)
coordinate, text = self.instructions.pop(player_loc)
text_loc = self.maze.mat2screen(coordinate)
frame = term.move_xy(*text_loc) + text
render(frame)
prev_text = text
prev_text_loc = text_loc
# time.sleep(2)
# frame = term.move_xy(*text_loc) + " " * len(text)
# render(frame)
# time.sleep(4)
# frame = term.move_xy(*text_loc) + " " * len(text)
# render(frame)
class InfiniteLevel(Scene):
"""Infinite level of maze"""
instance = None
random = None
def __init__(self, random_pos: bool) -> None:
global random
super().__init__()
self.player: Player = Player()
self.player.score.value += term.width * term.height // 10
if type(self).instance is None:
self.maze = Maze.generate(term.width // 5, term.height // 3, random_pos=random_pos)
random = random_pos
# self.maze = Maze.generate(7, 7, random_pos=random_pos)
self.generate_boxes()
self.level_boundary = Boundary(
len(self.maze.char_matrix[0]),
len(self.maze.char_matrix),
self.maze.top_left_corner,
term,
)
self.end_loc = self.maze.mat2screen(self.maze.end)
self.first_act = True # set up what to do at the start of the level
self.show_level = 40 # number of frames to show the level
self.wait = self.show_level
self.maze_is_visible = False
self.reward_on_goal = 0
for box in self.maze.boxes:
# move to top-left corner of maze + scale and extend width
# + move to top-left corner of box
box.loc = self.maze.mat2screen(box.loc) - (1, 1)
self.player.inside_box[box.col] = False
type(self).instance = self
else:
logging.error(
RuntimeError("Only one instance of 'Infinitelevel' can exist at a time")
)
def build_level(self) -> None:
"""Load current level specific attributes"""
self.player.start_loc = self.maze.mat2screen(mat=self.maze.start)
self.player.collision_count = 0
self.reward_on_goal = self.maze.width * self.maze.height // 10
def next_frame(self, val: Keystroke) -> Union[str, int]:
"""Draw next frame."""
if self.instance.first_act:
self.instance.first_act = False
self.instance.show_level = 30
self.instance.wait = self.instance.show_level
self.build_level()
play_level_up_sound()
# removes the main maze after 2 sec
frame = self.get_boundary_frame()
frame += self.instance.maze.map
render(frame)
for box in self.instance.maze.boxes:
box.render(self.instance.player)
self.player.start()
return ""
elif self.instance.wait > 0:
# block any actions from player and then remove maze
self.instance.wait -= 1
if self.instance.wait == 0:
self.remove_maze(0)
elif val.is_sequence and (257 < val.code < 262):
# update player
self.instance.player.update(val, self.instance.maze)
# check if game ends
if all(self.instance.player.avi.coords == self.instance.end_loc):
self.player.score.value += self.reward_on_goal
self.instance.reset_cls()
self.instance.next_frame(Keystroke())
return
self.render()
elif val.lower() == "e":
self.player.player_movement_sound(maze=self.maze)
elif val.lower() == "q":
return END
elif val.lower() == "r":
return RESET
elif val.lower() == "h":
if not self.instance.maze_is_visible:
self.instance.maze_is_visible = True
render(self.instance.maze.map)
for box in self.instance.maze.boxes:
box.render(self.player)
self.instance.player.render()
else:
self.instance.remove_maze(0)
# things that should update on every frame goes here
if not self.wait > 0:
self.player.score.update(
player_inside_box=any(self.player.inside_box.values())
)
if self.player.score.value <= 0:
return LOSE
return ""
def render(self, hard: bool = False) -> None:
"""Refreshing the scene"""
if hard:
render(term.clear, bg_col="lightskyblue1")
for box in self.instance.maze.boxes:
box.render(self.player)
render(self.instance.level_boundary.map)
# render player
render(term.move_xy(*self.instance.end_loc) + "&")
self.player.render()
def remove_maze(self, sleep: float = 2) -> None:
"""Erase main maze"""
self.instance.maze_is_visible = False
time.sleep(sleep)
render(self.get_boundary_frame())
for box in self.instance.maze.boxes:
box.render(self.instance.player)
self.player.render()
def get_boundary_frame(self) -> str:
"""Get the frame with only boundary"""
frame = term.clear
frame += self.instance.level_boundary.map
frame += term.move_xy(*self.instance.end_loc) + "&" # type: ignore
return frame
def player_in_boxes(self, player: Player) -> bool:
"""Return True if player in any boxes"""
k = None
for box in self.instance.maze.boxes:
k = box.player_in_box(player)
if k:
return True
return False
def generate_boxes(self) -> None:
"""Generate boxes"""
num_box_x = self.maze.width // 6
num_box_y = self.maze.height // 4
if num_box_y == 0:
num_box_y = 1
if num_box_x == 0:
num_box_x = 1
radius = (
max(self.maze.width * 2 // num_box_x, self.maze.height // num_box_y) + 3
)
number_of_box = num_box_x * num_box_y
box_list = []
logging.debug("radius {}".format(radius))
logging.debug("width:" + str(self.maze.width))
logging.debug("height:" + str(self.maze.height))
logging.debug("number of box:" + str(number_of_box))
logging.debug("\n" + str(self.maze))
for y in range(0, num_box_y):
for x in range(0, num_box_x):
box_pos = None
while (
box_pos is None
or box_pos.x < 2
or box_pos.y < 2
or box_pos.y > len(self.maze.matrix[1]) - 2
or box_pos.x > len(self.maze.matrix) - 2
or self.maze.matrix[box_pos.x][box_pos.y] != AIR
or all(box_pos == self.maze.start)
or all(box_pos == self.maze.end)
):
box_pos = Vec(
self.maze.height // num_box_y // 2
+ (self.maze.height * 2 // num_box_y) * y
+ randrange(-2, 2),
self.maze.width * 2 // num_box_x // 2
+ (self.maze.width * 2 // num_box_x) * x
+ randrange(-2, 2))
logging.debug("Box pos: {}".format(str(box_pos)))
box = Box(box_pos)
box.generate_map(self.maze, radius)
box.generate_image()
box_list.append(box)
self.maze.boxes = box_list
def reset(self) -> None:
"""Reset this level"""
for box in self.instance.maze.boxes:
box.needs_cleaning = False
self.instance.player.start()
self.instance.first_act = True
@classmethod
def reset_cls(cls):
"""Reset class"""
global random
cls.instance = None
cls.instance = InfiniteLevel(random)
class EndScene(Scene):
"""Example of ending scene."""
def __init__(self):
super().__init__()
txt = "You won, what shall you be remembered as?"
txt2 = "Press enter when you've decided your name."
txt3 = "Press escape if you don't want to be remembered."
self.current_frame = term.clear + term.move_xy(
x=(self.width - len(txt)) // 2, y=self.height // 2
)
self.current_frame += txt
self.current_frame += term.move_xy(
x=(self.width - len(txt2)) // 2, y=self.height // 2 + 4
)
self.current_frame += txt2
self.current_frame += term.move_xy(
x=(self.width - len(txt3)) // 2, y=self.height - 5
)
self.current_frame += txt3
self.first_frame = True
self.name = ""
self.player: Player = Player()
def set_player(self, player: Player) -> None:
"""Set player for getting their name after game is over."""
self.player = player
def next_frame(self, val: Keystroke) -> Union[None, int]:
"""Return next frame to render."""
if self.first_frame:
stop_bgm()
self.first_frame = False
render(self.current_frame)
# return self.render(self.current_frame)
elif val.lower() == "q":
return QUIT
else:
inp = term.inkey()
if inp.code == term.KEY_ENTER:
# save score
with open("leaderboard.txt", "a") as score_file:
score_file.write(f"{self.name}>{self.player.score.value:.2f}\n")
score_file.close()
return LEADERBOARD
elif inp.code == term.KEY_ESCAPE or inp == chr(3):
# don't save score
self.name = None
return LEADERBOARD
elif not inp.is_sequence and len(self.name) < 50 and inp != ">":
self.name += inp
elif inp.code in (term.KEY_BACKSPACE, term.KEY_DELETE):
self.name = self.name[:-1]
self.current_frame += term.move_left(1)
self.current_frame += " "
self.current_frame += term.move_xy(
x=self.width // 2, y=self.height // 2 + 1
)
self.current_frame += term.bold + self.name + term.normal
render(self.current_frame)
def reset(self) -> None:
"""No use."""
pass
class Pause(Scene):
"""Pause Screen for the game."""
def __init__(self):
super().__init__()
self.first_frame = True
self.reset()
def next_frame(self, val: Keystroke) -> Union[None, int]:
"""Return next frame to render"""
# no need to update each frame
if self.first_frame:
self.first_frame = False
render(self.current_frame)
elif val.lower() == "q":
return QUIT
elif val.lower() == "p":
# remove everything from screen
self.current_frame = term.move_xy(
x=(self.width - len(self.txt)) // 2, y=self.height // 2
)
self.current_frame += " " * len(self.txt)
self.current_frame += term.move_xy(
x=(self.width - len(self.txt2)) // 2, y=self.height // 2 + 1
)
self.current_frame += " " * len(self.txt2)
self.current_frame += term.move_xy(
x=(self.width - len(self.txt3)) // 2, y=self.height
)
self.current_frame += " " * len(self.txt3)
render(self.current_frame)
return PLAY
# elif val.lower() == "h":
# # help
# elif val.lower() == "c":
# # credits
return None
def reset(self) -> None:
"""Reset current scene"""
self.txt = "PAUSED"
self.txt2 = "Hit p to play"
self.txt3 = "Hit q again to exit"
self.current_frame = term.clear + term.move_xy(
x=(self.width - len(self.txt)) // 2, y=self.height // 2
)
self.current_frame += term.bold + self.txt + term.normal
self.current_frame += term.move_xy(
x=(self.width - len(self.txt2)) // 2, y=self.height // 2 + 1
)
self.current_frame += self.txt2
self.current_frame += term.move_xy(
x=(self.width - len(self.txt3)) // 2, y=self.height - 5
)
self.current_frame += self.txt3
self.first_frame = True
class Leaderboard(Scene):
"""Example of a title scene."""
def __init__(self) -> None:
super().__init__()
txt1 = "Your name has been etched onto the sands of time."
self.current_frame = term.firebrick_on_lightgreen + term.clear
self.current_frame += term.move_xy(x=(self.width - len(txt1)) // 2, y=3)
self.current_frame += txt1
self.first_frame = True
def next_frame(self, val: Keystroke) -> Union[None, int]:
"""Returns next frame to render"""
# no need to update the frame anymore
if self.first_frame:
self.first_frame = False
with open("leaderboard.txt", "r") as f:
players = f.read()
players_sorted = sorted(
[player.split(">") for player in players.split("\n")[:-1]],
key=lambda x: float(x[1]),
reverse=True,
)
scores = make_table(
rows=players_sorted,
labels=["Name", "Persistence"],
centered=True,
)
table_width = len(scores.split("\n")[0])
for n, line in enumerate(scores.split("\n")):
self.current_frame += term.move_xy(
x=(self.width - table_width) // 2,
y=n + 5,
)
self.current_frame += line
render(self.current_frame)
# return self.current_frame
elif str(val) == " " or val.name == "KEY_ENTER":
return END
return None
# return ""
def reset(self) -> None:
"""Reset has no use for title scene."""
def title_menu_action(choice: str) -> Union[int, None]:
"""Actions for Title menu"""
logging.info("title")
if choice == "Start":
enter_game_sound()
return NEXT_SCENE
elif choice == "Infinite":
enter_game_sound()
return INFINITE
elif choice == "Credits":
return CREDITS
elif choice == "Tutorial":
enter_game_sound()
return TUTORIAL
elif choice == "Leaderboard":
return LEADERBOARD
else:
return QUIT
title_scene = Menu(
txt=["Welcome :)", ""],
choices=["Start", "Infinite", "Tutorial", "Credits", "Leaderboard", "Quit"],
action_on_choice=title_menu_action,
)
def credit_menu_action(choice: str) -> Union[int, None]:
"""Actions for Credit menu"""
logging.info("credit")
if choice == "Back":
return TITLE
return None
credit_scene = Menu(
txt=["Creidts", ""],
choices=[
"Anand",
"Pritam Dey",
"Jason Ho",
"Himi",
"Olivia",
"StoneSteel",
"",
"Back",
],
action_on_choice=credit_menu_action,
)
def pause_menu_action(choice: str) -> Union[int, None]:
"""Actions for Pause menu"""
if choice == "Return":
return PLAY
elif choice == "Quit":
return QUIT
elif choice == "Main menu":
return TITLE
return None
pause_menu = Menu(
txt=["Game Paused", ""],
choices=["Return", "Main menu", "Quit"],
action_on_choice=pause_menu_action,
)
def leaderboard_action(choice: str) -> Union[None, int]:
"""Action for leaderboard"""
if choice == "Main Menu":
return TITLE
return None
def leaderboard_first_frame() -> List[str]:
"""First frame action for leaderboard"""
with open("leaderboard.txt", "r") as f:
players = f.read()
players_sorted = sorted(
[player.split(">") for player in players.split("\n")[:-1]],
key=lambda x: float(x[1]),
reverse=True,
)
players_sorted = ["\t".join(player) for player in players_sorted]
if len(players_sorted) > 10:
players_sorted = players_sorted[:10]
return players_sorted
leaderboard_menu = Menu(
txt=["Leaderboard", ""],
choices=["Main Menu"],
action_on_choice=leaderboard_action,
action_on_first_frame=leaderboard_first_frame,
)