-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris_two_block.py
More file actions
313 lines (259 loc) · 9.42 KB
/
tetris_two_block.py
File metadata and controls
313 lines (259 loc) · 9.42 KB
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
# ***
# *** Adapted from TETRIS ONE BLOCK/tetris_one_block.py of https://github.com/DimaGutierrez/Python-Games
# *** - modified from dumbdisplay_examples/tetris/tetris_one_block.py
# ***
import random
import time
from dumbdisplay import *
# from dumbdisplay.core import *
# from dumbdisplay.layer_graphical import DDRootLayer
# from dumbdisplay.layer_turtle import LayerTurtle
# from dumbdisplay.layer_lcd import LayerLcd
from dumbdisplay_examples.utils import DDAppBase, create_example_wifi_dd
from dumbdisplay_examples.tetris._common import Grid, _colors, _width, _height, _draw_grid, _commit_block_grid, Block, \
_check_bad_block_grid_placement, _randomize_grid
_RANDOMIZE_ROW_COUNT = 2
_delay = 0.3 # For time/sleep
def _randomize_block_grid() -> Grid:
color = random.randint(1, len(_colors) - 1)
if True:
n_rows = random.randint(1, 2)
n_cols = random.randint(1, 2)
if False: # TODO: disable after debug
n_rows = 1
n_cols = 2
block_grid = []
for y in range(n_rows):
block_grid_row = []
for x in range(n_cols):
block_grid_row.append(color)
block_grid.append(block_grid_row)
else:
block_grid = [[color, color]]
return Grid(grid_cells=block_grid)
# def _randomize_grid() -> Grid:
# grid_cells = []
# for y in range(_grid_n_rows):
# grid_row = []
# for x in range(_grid_n_cols):
# if y >= (_grid_n_rows - _RANDOMIZE_ROW_COUNT) and random.random() < 0.7:
# color = random.randint(1, len(_colors) - 1)
# else:
# color = 0
# grid_row.append(color)
# grid_cells.append(grid_row)
# return Grid(grid_cells=grid_cells)
def _check_grid(shape: 'Shape', score: LayerTurtle) -> (bool, int):
grid = shape.grid
block = shape.block
# Check if each row is full:
empty_count = 23
deleted_count = 0
for y in range(0,24):
is_full = True
is_empty = True
y_erase = y
for x in range(0,12):
if grid.get_value(y, x) == 0:
is_full = False
else:
is_empty = False
if not is_empty and not is_full:
empty_count -= 1
break
# Remove row and shift down
if is_full:
shape.score_count += 1
score.clear()
score.write(f'Score: {shape.score_count}', align='C')
for y in range(y_erase-1, -1, -1):
for x in range(0,12):
grid.set_value(y + 1, x, grid.get_value(y, x))
deleted_count += 1
return (empty_count == 23, deleted_count)
class Shape:
def __init__(self, pen: LayerTurtle, block_pen: LayerTurtle):
self.grid = _randomize_grid(_RANDOMIZE_ROW_COUNT)
self.score_count = 0
self.block: Block = None
self.pen = pen
self.block_pen = block_pen
if not self.reset_block():
raise Exception("Failed to create initial block")
def check_grid(self, score: LayerTurtle) -> bool:
(all_empty, delete_count) = _check_grid(self, score)
if delete_count > 0:
self.sync_image()
return all_empty and self.block is None
def reset_block(self) -> bool:
x = 5
y = 0
block_grid = _randomize_block_grid()
x -= int((block_grid.n_cols - 1) / 2)
y += 1 - block_grid.n_rows
if _check_bad_block_grid_placement(block_grid, x, y, grid=self.grid, check_boundary=False):
return False
self.block = Block(x, y, block_grid=block_grid, block_pen=self.block_pen)
self.sync_image()
return True
def commit_block(self):
_commit_block_grid(self.block.block_grid, self.block.x, self.block.y, self.grid)
#self.block.commit(self.grid)
self.sync_image()
self.block = None
def move_block_down(self) -> bool:
return self.block.move_down(self.grid)
def move_block_right(self) -> bool:
return self.block.move_right(self.grid)
def move_block_left(self) -> bool:
return self.block.move_left(self.grid)
def sync_image(self):
#self.block.sync_image()
_draw_grid(self.grid, self.pen)
class TetrisTwoBlockApp(DDAppBase):
def __init__(self, dd: DumbDisplay = create_example_wifi_dd()):
super().__init__(dd)
self.score: LayerTurtle = None
self.block_pen: LayerTurtle = None
self.pen: LayerTurtle = None
self.shape: Shape = None
self.last_update_time = None
def initializeDD(self):
root = DDRootLayer(self.dd, _width, _height)
root.border(5, "darkred", "round", 1)
root.backgroundColor("black")
block_pen = LayerTurtle(self.dd, _width, _height)
block_pen.penFilled()
#block_pen.setTextSize(32)
pen = LayerTurtle(self.dd, _width, _height)
pen.penFilled()
pen.setTextSize(32)
score = LayerTurtle(self.dd, _width, _height)
score.penColor('red')
score.penUp()
score.goTo(60, -300)
score.setTextFont("Courier", 24)
#score.write('Score: 0', 'C')
border = LayerTurtle(self.dd, _width, _height)
if False:
border.rectangle(260, 490, centered=True)
border.penSize(10)
border.penUp()
border.goTo(-130, 240)
border.penDown()
border.penColor('linen')
border.rightTurn(90)
border.forward(490) # Down
border.leftTurn(90)
border.forward(260) # Right
border.leftTurn(90)
border.forward(490) # Up
border.penUp()
border.goTo(0,260)
border.setTextFont("Courier", 36)
border.write("Two-Block TETRIS", "C")
left_button = LayerLcd(self.dd, 2, 1, char_height=28)
left_button.noBackgroundColor()
left_button.writeLine("⬅️")
left_button.enableFeedback("f", lambda *args: self.moveBlockLeft())
right_button = LayerLcd(self.dd, 2, 1, char_height=28)
right_button.noBackgroundColor()
right_button.writeLine("➡️")
right_button.enableFeedback("f", lambda *args: self.moveBlockRight())
AutoPin('V',
AutoPin('S'),
AutoPin('H', left_button, right_button)).pin(self.dd)
self.score = score
self.block_pen = block_pen
self.pen = pen
self.startGame()
#self.shape = Shape()
#self.resetBlock()
self.last_update_time = time.time()
def updateDD(self):
now = time.time()
need_update = (now - self.last_update_time) >= _delay
if need_update:
self.last_update_time = now
self.update()
def update(self):
if self.shape is None:
print("... waiting to restart ...")
return
moved_down = self.moveBlockDown()
if not moved_down:
self.shape.commit_block()
won = self.checkGrid()
if won:
self.endGame(won=True)
elif not moved_down:
if not self.resetBlock():
self.endGame(won=False)
# if self.shape.block.y > 0:
# #self.shape.reset_block()
# self.resetBlock()
# else:
# self.endGame(won=False)
def startGame(self):
self.score.clear()
self.score.write('Score: 0', 'C')
self.pen.clear()
self.block_pen.clear()
self.shape = Shape(pen=self.pen, block_pen=self.block_pen)
print("... started game")
def endGame(self, won: bool):
#self.block_pen.clear()
self.shape = None
if won:
msg = "🥳 YOU WON 🥳"
color = "purple"
else:
msg = "GAME OVER 😔"
color = "darkgray"
self.pen.home(with_pen=False)
self.pen.penColor("white")
self.pen.oval(300, 100, centered=True)
self.pen.penColor(color)
self.pen.write(msg, align='C')
print("... ended game")
def checkGrid(self) -> bool:
return self.shape.check_grid(score=self.score)
# check_result = check_grid(shape=self.shape, score=self.score)
# self.drawGrid() # should only redraw if any lines were cleared
# return check_result
def resetBlock(self) -> bool:
return self.shape.reset_block()
#self.drawGrid()
#self.drawBlock()
def moveBlockDown(self) -> bool:
return self.shape.move_block_down()
# if self.shape.move_block_down():
# self.drawBlock()
# return True
# return False
def moveBlockLeft(self) -> bool:
#print("$ move left")
if self.shape is None:
self.startGame()
return False
return self.shape.move_block_left()
# if self.shape.move_block_left():
# self.drawBlock()
# return True
# return False
def moveBlockRight(self) -> bool:
if self.shape is None:
self.startGame()
return False
return self.shape.move_block_right()
# if self.shape.move_block_right():
# self.drawBlock()
# return True
# return False
def run_tetris_two_block():
from dumbdisplay_examples.utils import create_example_wifi_dd
print("*** Running TETRIS TWO BLOCK ***")
app = TetrisTwoBlockApp(create_example_wifi_dd())
app.run()
if __name__ == "__main__":
run_tetris_two_block()