-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
223 lines (177 loc) · 6.23 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
import pygame
from typing import List, Tuple, Dict, Any, Optional
from dataclasses import dataclass
from abc import ABC, abstractmethod
from src.levels import SNES
from src.tetriminos import (
Matrix,
TetriminoDisplay,
TetriminoQueue,
Text,
ReactiveText,
)
from src.settings import (
WIDTH,
HEIGHT,
FPS,
)
from utils.io import asset_resource_path
def calculate_score(lines_cleared: List[int]) -> int:
total_score = 0
for i in lines_cleared:
total_score += int(2 ** (i - 1) * 1000)
return total_score
@dataclass
class Scene(ABC):
screen: pygame.display
def __post_init__(self):
self.init_assets()
self.init_state()
self.init_widgets()
self.clock = pygame.time.Clock()
self.running = True
def run(self):
while self.running:
next_scene, params = self.update()
if next_scene:
return next_scene, params
self.render()
pygame.display.flip()
self.clock.tick(FPS)
return None, None
def init_widgets(self):
pass
def init_state(self):
pass
def init_assets(self):
pass
def update(self) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key in {pygame.K_ESCAPE, ord("q")}:
self.running = False
return None, None
@abstractmethod
def render(self):
pass
@dataclass
class GameScene(Scene):
def init_widgets(self):
self.shape_generator = TetriminoQueue()
self.stashed_tetrimino = TetriminoDisplay(self.screen, (400, 100))
self.matrix = Matrix(self.screen, (400, 260), self.shape_generator)
self.next_tetrimino = TetriminoDisplay(self.screen, (720, 100))
self.score_text = ReactiveText(
self.screen, (560, 100), (160, 60), self.font, "0"
)
self.level_text = Text(
self.screen, (560, 160), (160, 60), self.font, f"Level {self.level}"
)
self.mousables = [self.score_text]
def init_state(self):
self.running = True
self.can_stash = True
self.locked = False
self.total_score = 0
self.lines_cleared = 0
self.start_time = pygame.time.get_ticks()
def init_assets(self):
self.font = pygame.font.SysFont("monospace", 34)
@property
def level(self) -> int:
return self.lines_cleared // 10
def update(self):
self.next_tetrimino.set_tetrimino(*self.shape_generator.peek())
active_tetrimino = self.matrix.get_tetrimino()
if not self.locked:
for event in pygame.event.get():
if event.type in {
pygame.MOUSEMOTION,
pygame.MOUSEBUTTONUP,
pygame.MOUSEBUTTONDOWN,
}:
for subscriber in self.mousables:
subscriber.push(event)
if event.type == pygame.KEYDOWN:
if event.key in {pygame.K_ESCAPE, ord("q")}:
self.running = False
if event.key == pygame.K_LEFT:
self.matrix.move_left()
if event.key == pygame.K_RIGHT:
self.matrix.move_right()
if event.key == pygame.K_DOWN:
self.matrix.move_down()
if event.key == pygame.K_SPACE:
self.locked = True
if event.key == pygame.K_UP:
self.matrix.rotate()
# if event.key == ord("s"):
# self.matrix.rotate()
if event.key == ord("a"):
self.matrix.rotate(-1)
if event.key == pygame.K_RETURN and self.can_stash:
self.can_stash = False
stash = self.matrix.stash()
self.stashed_tetrimino.set_tetrimino(*stash)
if pygame.time.get_ticks() - self.start_time > SNES.ticks(self.level):
self.matrix.move_down()
self.start_time = pygame.time.get_ticks()
else:
self.matrix.move_down()
lines_cleared = 0
if active_tetrimino.placed:
self.locked = False
self.can_stash = True
lines_cleared = self.matrix.clear_lines()
if lines_cleared:
self.lines_cleared += sum(lines_cleared)
self.total_score += SNES.score(lines_cleared, 0, self.level)
self.score_text.set_text(self.total_score)
self.level_text.set_text(f"Level {self.level}")
if self.matrix.is_game_over():
self.running = False
return "game_over", {"score": self.total_score}
return None, None
def render(self):
self.screen.fill((0, 0, 0))
self.matrix.render()
self.stashed_tetrimino.render()
self.next_tetrimino.render()
self.score_text.render()
self.level_text.render()
@dataclass
class GameOverScene(Scene):
score: int
def init_assets(self):
self.font = pygame.font.SysFont("monospace", 50)
def init_widgets(self):
self.game_over = ReactiveText(
self.screen, (500, 500), (160, 120), self.font, "GAME OVER"
)
self.score_text = Text(
self.screen, (500, 700), (160, 120), self.font, str(self.score)
)
def render(self):
self.game_over.render()
self.score_text.render()
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH + 800, HEIGHT + 400))
# drop_sound = pygame.mixer.Sound(asset_resource_path("drop.wav"))
# pygame.mixer.music.load(asset_resource_path("bgm.wav"))
# pygame.mixer.music.play(-1)
scenes = {
"game": GameScene,
"game_over": GameOverScene,
}
next_scene_key, params = "game", {}
running = True
while running:
scene = scenes[next_scene_key](screen, **params)
print(next_scene_key)
next_scene_key, params = scene.run()
if not next_scene_key:
break
pygame.quit()
if __name__ == "__main__":
main()