-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris_one_block.py
More file actions
342 lines (278 loc) · 9.65 KB
/
tetris_one_block.py
File metadata and controls
342 lines (278 loc) · 9.65 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
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
# ***
# *** Adapted from TETRIS ONE BLOCK/tetris_one_block.py of https://github.com/DimaGutierrez/Python-Games
# ***
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, _draw, _draw_grid, _width, _height, _colors, _grid_n_rows, _grid_n_cols
_RANDOMIZE_ROW_COUNT = 4
_delay = 0.3 # For time/sleep
_grid = [ # 12x24
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[4,3,3,1,0,1,0,3,3,4,0,1],
[5,1,6,5,3,2,1,0,5,5,0,3],
[1,2,3,4,5,2,0,4,2,3,0,4],
[1,3,1,2,4,2,0,3,1,2,4,3],
[1,1,2,3,0,2,2,2,0,3,0,1],
[0,1,1,2,3,0,0,0,4,0,2,0],
[2,0,1,2,3,0,6,5,5,5,0,2]
]
def _create_grid() -> Grid:
if _RANDOMIZE_ROW_COUNT >= 0:
grid = []
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.append(grid_row)
else:
grid = []
for grid_row in _grid:
grid.append(grid_row.copy())
return Grid(grid)
class OneBlock:
def __init__(self, x: int, y: int, block_pen: LayerTurtle):
self.x = x
self.y = y
self.color = random.randint(1, len(_colors) - 1)
self.block_pen = block_pen
self.sync_image()
def commit(self, grid: Grid):
grid.set_value(self.y, self.x, self.color)
if True:
self.block_pen.clear()
def move_down(self, grid: Grid) -> bool:
if self.y < 23 and grid.get_value(self.y + 1, self.x) == 0:
self.y += 1
self.sync_image()
return True
return False
def move_right(self, grid: Grid) -> bool:
if self.x < 11:
if grid.get_value(self.y, self.x + 1) == 0:
self.x += 1
self.sync_image()
return True
return False
def move_left(self, grid: Grid) -> bool:
if self.x > 0:
if grid.get_value(self.y, self.x - 1) == 0:
self.x -= 1
self.sync_image()
return True
return False
def sync_image(self):
self.block_pen.clear()
_draw(self.x, self.y, self.color, self.block_pen)
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 = _create_grid()
self.score_count = 0
self.block: OneBlock = 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
if self.grid.get_value(y, x) != 0:
#self.sync_image()
return False
self.block = OneBlock(x, y, self.block_pen)
self.sync_image()
return True
def commit_block(self):
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 TetrisOneBlockApp(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("One-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)
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.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)
def resetBlock(self) -> bool:
return self.shape.reset_block()
def moveBlockDown(self) -> bool:
return self.shape.move_block_down()
def moveBlockLeft(self) -> bool:
if self.shape is None:
self.startGame()
return False
return self.shape.move_block_left()
def moveBlockRight(self) -> bool:
if self.shape is None:
self.startGame()
return False
return self.shape.move_block_right()
def run_tetris_one_block():
from dumbdisplay_examples.utils import create_example_wifi_dd
print("*** Running TETRIS ONE BLOCK ***")
app = TetrisOneBlockApp(create_example_wifi_dd())
app.run()
if __name__ == "__main__":
run_tetris_one_block()