forked from Anand1310/summer-code-jam-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
142 lines (124 loc) · 4.46 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
"""Game components."""
import logging
import os
from typing import List, Union
import blessed
from blessed.keyboard import Keystroke
from core.player import Player
from core.render import Render
if "logs" not in os.listdir():
os.mkdir("logs")
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG) # or whatever
handler = logging.FileHandler("logs/debug.log", "w", "utf-8") # or whatever
handler.setFormatter(logging.Formatter("%(name)s %(message)s")) # or whatever
root_logger.addHandler(handler)
logging.info("=" * 15)
TITLE = 0
NEXT_SCENE = 1
RESET = 2
QUIT = 3
PAUSE = 4
PLAY = 5
LOSE = 6
CREDITS = 7
TUTORIAL = 8
END = 9
LEADERBOARD = 10
INFINITE = 11
term = blessed.Terminal()
render = Render()
class Scene:
"""This should be subclassed to create each new level.
The subclass should implement the functions `rest` and `next_frame`
"""
def __init__(self, col: str = "black", bg_col: str = "peachpuff2"):
self.height = term.height
self.width = term.width
self.current_frame = ""
self.col = col
self.bg_col = bg_col
def reset(self) -> None:
"""Reset the current scene/level to its initial state."""
def next_frame(self, val: Keystroke) -> Union[str, int]:
"""Draw next frame in the scene."""
class Game:
"""Main game class. Should be initiated with a list of scenes."""
def __init__(
self,
scenes: List[Scene],
infinite: Scene,
pause: Scene,
tutorial: Scene,
leaderboard: Scene,
end_scene: Scene,
credit: Scene,
) -> None:
self.scenes = scenes
self.current_scene_index: int = 0
self.infinite = infinite
self.pause = pause
self.tutorial = tutorial
self.leaderboard = leaderboard
self.end = end_scene
self.credit = credit
self.current_scene: Scene = self.scenes[self.current_scene_index]
self.player = Player()
def run(self) -> None:
"""Run the main game loop."""
with term.cbreak():
val = Keystroke()
while True:
command = self.current_scene.next_frame(val)
# get all the frames and print
frame = render.screen()
print(frame)
if command == NEXT_SCENE:
self.current_scene.reset()
self.current_scene_index += 1
# end game if scenes end
if self.current_scene_index == len(self.scenes):
self.current_scene = self.end
continue
else:
self.current_scene = self.scenes[self.current_scene_index]
continue
elif command == INFINITE:
self.current_scene.reset()
self.current_scene = self.infinite
elif command == RESET:
self.current_scene.reset()
val = Keystroke()
continue
elif command == PAUSE:
self.current_scene.reset()
self.current_scene = self.pause
continue
elif command == PLAY:
self.pause.reset()
self.current_scene = self.scenes[self.current_scene_index]
self.current_scene.render(hard=True)
continue
elif command == CREDITS:
self.current_scene.reset()
self.current_scene = self.credit
self.current_scene.next_frame(Keystroke())
elif command == TITLE:
self.current_scene_index = 0
self.current_scene.reset()
self.current_scene = self.scenes[0]
self.current_scene.next_frame(Keystroke())
elif command == QUIT or command == LOSE:
break
elif command == LEADERBOARD:
self.current_scene.reset()
self.current_scene = self.leaderboard
continue
elif command == TUTORIAL:
self.current_scene.reset()
self.current_scene = self.tutorial
continue
elif command == END:
self.current_scene = self.end
continue
val = term.inkey(timeout=0.05) # 20 fps