Skip to content

Commit 464a2a8

Browse files
committed
working on TetrisTwoBlockApp
1 parent 84c9f8e commit 464a2a8

3 files changed

Lines changed: 99 additions & 35 deletions

File tree

dumbdisplay_examples/tetris/_common.py

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,78 @@ def __init__(self, x: int, y: int, block_grid: Grid, block_pen: LayerTurtle):
5858
_draw_grid(block_grid, block_pen)
5959

6060
def move_down(self, grid: Grid) -> bool:
61-
if _check_block_grid_placement(self.block_grid, self.x, self.y + 1, grid=grid):
61+
if _check_bad_block_grid_placement(self.block_grid, self.x, self.y + 1, grid=grid):
6262
return False
6363
self.y += 1
6464
self.sync_image()
6565
return True
6666

6767
def move_right(self, grid: Grid) -> bool:
68-
if _check_block_grid_placement(self.block_grid, self.x + 1, self.y, grid=grid):
68+
if _check_bad_block_grid_placement(self.block_grid, self.x + 1, self.y, grid=grid):
6969
return False
7070
self.x += 1
7171
self.sync_image()
7272
return True
7373

7474
def move_left(self, grid: Grid) -> bool:
75-
if _check_block_grid_placement(self.block_grid, self.x - 1, self.y, grid=grid):
75+
if _check_bad_block_grid_placement(self.block_grid, self.x - 1, self.y, grid=grid):
7676
return False
7777
self.x -= 1
7878
#print(f"* left ==> x={self.x}")
7979
self.sync_image()
8080
return True
8181

82+
def rotate(self, grid: Grid) -> bool:
83+
(rotated_block_grid, y_offset) = _rotate_block_grid_if_possible(self.block_grid, self.x, self.y, grid=grid)
84+
if rotated_block_grid is None:
85+
return False
86+
self.block_grid = rotated_block_grid
87+
self.y += y_offset
88+
self.block_pen.clear()
89+
_draw_grid(self.block_grid, self.block_pen)
90+
return True
91+
92+
8293
def sync_image(self):
8394
anchor_x = self.x * _block_unit_width
8495
anchor_y = self.y * _block_unit_width
8596
self.block_pen.setLevelAnchor(anchor_x, anchor_y)
8697

8798

99+
100+
def _check_bad_block_grid_cells_placement(grid_cells: list[list[int]], block_grid_x_off: int, block_grid_y_offset: int, grid: Grid, check_boundary: bool = True) -> bool:
101+
n_rows = len(grid_cells)
102+
n_cols = len(grid_cells[0]) if n_rows > 0 else 0
103+
for y in range(n_rows):
104+
for x in range(n_cols):
105+
if grid_cells[y][x] != 0:
106+
row_idx = y + block_grid_y_offset
107+
col_idx = x + block_grid_x_off
108+
if row_idx < 0:
109+
continue # never mind above the grid
110+
if row_idx < 0 or row_idx >= grid.n_rows:
111+
if not check_boundary:
112+
continue
113+
return True
114+
if col_idx < 0 or col_idx >= grid.n_cols:
115+
if not check_boundary:
116+
continue
117+
return True
118+
if grid.get_value(row_idx, col_idx) != 0:
119+
return True
120+
return False
121+
122+
123+
def _rotate_block_grid_cells(grid_cells: list[list[int]]) -> list[list[int]]:
124+
rotated = []
125+
for x in range(len(grid_cells[0])):
126+
new_row = []
127+
for y in range(len(grid_cells) -1, -1, -1):
128+
new_row.append(grid_cells[y][x])
129+
rotated.append(new_row)
130+
return rotated
131+
132+
88133
def _randomize_grid(randomize_row_count: int) -> Grid:
89134
grid_cells = []
90135
for y in range(_grid_n_rows):
@@ -120,25 +165,38 @@ def _draw_grid(grid: Grid, pen: LayerTurtle):
120165
color_number = grid.get_value(y, x)
121166
_draw(x, y, color_number, pen)
122167

123-
def _check_block_grid_placement(block_grid: Grid, block_grid_x_off: int, block_grid_y_offset: int, grid: Grid, check_boundary: bool = True) -> bool:
124-
for y in range(block_grid.n_rows):
125-
for x in range(block_grid.n_cols):
126-
if block_grid.get_value(y, x) != 0:
127-
row_idx = y + block_grid_y_offset
128-
col_idx = x + block_grid_x_off
129-
if row_idx < 0:
130-
continue # never mind above the grid
131-
if row_idx < 0 or row_idx >= grid.n_rows:
132-
if not check_boundary:
133-
continue
134-
return True
135-
if col_idx < 0 or col_idx >= grid.n_cols:
136-
if not check_boundary:
137-
continue
138-
return True
139-
if grid.get_value(row_idx, col_idx) != 0:
140-
return True
141-
return False
168+
def _check_bad_block_grid_placement(block_grid: Grid, block_grid_x_off: int, block_grid_y_offset: int, grid: Grid, check_boundary: bool = True) -> bool:
169+
if True:
170+
return _check_bad_block_grid_cells_placement(block_grid.grid_cells, block_grid_x_off, block_grid_y_offset, grid, check_boundary)
171+
else:
172+
for y in range(block_grid.n_rows):
173+
for x in range(block_grid.n_cols):
174+
if block_grid.get_value(y, x) != 0:
175+
row_idx = y + block_grid_y_offset
176+
col_idx = x + block_grid_x_off
177+
if row_idx < 0:
178+
continue # never mind above the grid
179+
if row_idx < 0 or row_idx >= grid.n_rows:
180+
if not check_boundary:
181+
continue
182+
return True
183+
if col_idx < 0 or col_idx >= grid.n_cols:
184+
if not check_boundary:
185+
continue
186+
return True
187+
if grid.get_value(row_idx, col_idx) != 0:
188+
return True
189+
return False
190+
191+
192+
def _rotate_block_grid_if_possible(block_grid: Grid, block_grid_x_off: int, block_grid_y_offset: int, grid: Grid) -> (Grid, int):
193+
block_grid_cells = block_grid.grid_cells
194+
rotated_cells = _rotate_block_grid_cells(block_grid_cells)
195+
y_offset = len(rotated_cells) - len(block_grid_cells)
196+
if _check_bad_block_grid_cells_placement(rotated_cells, block_grid_x_off, block_grid_y_offset + y_offset, grid, check_boundary=True):
197+
return (None, None)
198+
rotated_block_grid = Grid(grid_cells=rotated_cells)
199+
return (rotated_block_grid, y_offset)
142200

143201

144202
def _commit_block_grid(block_grid: Grid, block_grid_x_off: int, block_grid_y_offset: int, grid: Grid):

dumbdisplay_examples/tetris/tetris_classic.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from dumbdisplay.layer_lcd import LayerLcd
1313
from dumbdisplay_examples.tetris._common import Grid, _colors, _grid_n_rows, _grid_n_cols, _block_unit_width, \
1414
_width, _height, _left, _top, _draw_grid, _commit_block_grid, Block, \
15-
_check_block_grid_placement, _randomize_grid
15+
_check_bad_block_grid_placement, _randomize_grid, _rotate_block_grid_if_possible
1616
from dumbdisplay_examples.tetris._shapes import _randomize_block_grid
1717

1818
from dumbdisplay_examples.utils import DDAppBase, create_example_wifi_dd
@@ -78,7 +78,7 @@ def reset_block(self) -> bool:
7878
block_grid = _randomize_block_grid()
7979
x -= int((block_grid.n_cols - 1) / 2)
8080
y += 1 - block_grid.n_rows
81-
if _check_block_grid_placement(block_grid, x, y, grid=self.grid, check_boundary=False):
81+
if _check_bad_block_grid_placement(block_grid, x, y, grid=self.grid, check_boundary=False):
8282
return False
8383
self.block = Block(x, y, block_grid=block_grid, block_pen=self.block_pen)
8484
self.sync_image()
@@ -96,6 +96,9 @@ def move_block_down(self) -> bool:
9696
def move_block_right(self) -> bool:
9797
return self.block.move_right(self.grid)
9898

99+
def rotate_block(self) -> bool:
100+
return self.block.rotate(self.grid)
101+
99102
def move_block_left(self) -> bool:
100103
return self.block.move_left(self.grid)
101104

@@ -163,9 +166,14 @@ def initializeDD(self):
163166
right_button.writeLine("➡️")
164167
right_button.enableFeedback("f", lambda *args: self.moveBlockRight())
165168

169+
rotate_button = LayerLcd(self.dd, 2, 1, char_height=28)
170+
rotate_button.noBackgroundColor()
171+
rotate_button.writeLine("🔄")
172+
rotate_button.enableFeedback("f", lambda *args: self.rotateBlock())
173+
166174
AutoPin('V',
167175
AutoPin('S'),
168-
AutoPin('H', left_button, right_button)).pin(self.dd)
176+
AutoPin('H', left_button, rotate_button, right_button)).pin(self.dd)
169177

170178
self.score = score
171179
self.block_pen = block_pen
@@ -254,20 +262,18 @@ def moveBlockLeft(self) -> bool:
254262
self.startGame()
255263
return False
256264
return self.shape.move_block_left()
257-
# if self.shape.move_block_left():
258-
# self.drawBlock()
259-
# return True
260-
# return False
261265

262266
def moveBlockRight(self) -> bool:
263267
if self.shape is None:
264268
self.startGame()
265269
return False
266270
return self.shape.move_block_right()
267-
# if self.shape.move_block_right():
268-
# self.drawBlock()
269-
# return True
270-
# return False
271+
272+
def rotateBlock(self) -> bool:
273+
if self.shape is None:
274+
self.startGame()
275+
return False
276+
return self.shape.rotate_block()
271277

272278

273279
if __name__ == "__main__":

dumbdisplay_examples/tetris/tetris_two_block.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from dumbdisplay.layer_lcd import LayerLcd
1212
from dumbdisplay_examples.tetris._common import Grid, _colors, _grid_n_rows, _grid_n_cols, _block_unit_width, \
1313
_width, _height, _left, _top, _draw_grid, _commit_block_grid, Block, \
14-
_check_block_grid_placement, _randomize_grid
14+
_check_bad_block_grid_placement, _randomize_grid
1515

1616
from dumbdisplay_examples.utils import DDAppBase, create_example_wifi_dd
1717

@@ -109,7 +109,7 @@ def reset_block(self) -> bool:
109109
block_grid = _randomize_block_grid()
110110
x -= int((block_grid.n_cols - 1) / 2)
111111
y += 1 - block_grid.n_rows
112-
if _check_block_grid_placement(block_grid, x, y, grid=self.grid, check_boundary=False):
112+
if _check_bad_block_grid_placement(block_grid, x, y, grid=self.grid, check_boundary=False):
113113
return False
114114
self.block = Block(x, y, block_grid=block_grid, block_pen=self.block_pen)
115115
self.sync_image()

0 commit comments

Comments
 (0)