Skip to content

Commit 479c40f

Browse files
committed
working on TetrisTwoBlockApp
1 parent e4ef3a8 commit 479c40f

File tree

3 files changed

+61
-35
lines changed

3 files changed

+61
-35
lines changed

dumbdisplay_examples/tetris/_common.py

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919

2020
class Grid:
21-
def __init__(self, grid_cells):
21+
def __init__(self, grid_cells: list[list[int]], grid_cell_type = None):
2222
self.grid_cells = grid_cells
23+
self.grid_cell_type = grid_cell_type
2324
self.grid_dirty = []
2425
for grid_row in self.grid_cells:
2526
grid_dirty_row = []
@@ -47,14 +48,14 @@ def set_value(self, row_idx, col_idx, value):
4748

4849

4950
class Block:
50-
def __init__(self, x: int, y: int, block_grid: Grid, block_pen: LayerTurtle, rotate_with_level: bool = False):
51+
def __init__(self, x: int, y: int, block_grid: Grid, block_pen: LayerTurtle, move_reach_in_millis: int = 50, rotate_with_level: bool = False):
5152
self.x = x
5253
self.y = y
5354
self.rotation = 0
5455
self.block_grid = block_grid
5556
self.block_pen = block_pen
5657
self.rotate_with_level = rotate_with_level
57-
self.move_reach_in_millis = 50
58+
self.move_reach_in_millis = move_reach_in_millis
5859
block_pen.clear()
5960
if not rotate_with_level:
6061
# make the block tiled a bit
@@ -100,14 +101,54 @@ def rotate(self, grid: Grid) -> bool:
100101

101102

102103
def sync_image(self, reach_in_millis: int = 0):
104+
from dumbdisplay_examples.tetris._shapes import _vertical_line, _horizontal_line, _left_l, _right_l, _left_s, _right_s, _t
103105
if self.rotate_with_level:
104106
angle = 90 * self.rotation + 2
105107
pivot_x = 90 + 10
106108
pivot_y = 120 + 10
107-
anchor_x = self.x * _block_unit_width
108-
anchor_y = self.y * _block_unit_width
109-
# if self.rotation == 2:
110-
# anchor_x += _block_unit_width
109+
x = self.x
110+
y = self.y
111+
# n_rows = self.block_grid.n_rows
112+
# n_cols = self.block_grid.n_cols
113+
rotation = self.rotation
114+
block_type = self.block_grid.grid_cell_type
115+
# if rotation == 1:
116+
# print("rotated 90")
117+
# if True: # square
118+
# pass
119+
if block_type is _vertical_line: # _vertical_line
120+
if rotation == 1:
121+
x += 2
122+
elif rotation == 2:
123+
x -= 1
124+
elif rotation == 3:
125+
y -= 1
126+
elif block_type is _horizontal_line: # _horizontal_line
127+
if rotation == 1:
128+
x -= 1
129+
elif rotation == 2:
130+
x += 2
131+
y -= 1
132+
# elif self.rotation == 3:
133+
# pass
134+
elif block_type is _left_l or block_type is _right_l: # _left_l and _right_l
135+
# if self.rotation == 1:
136+
# pass
137+
if rotation == 2:
138+
x += 2
139+
y -= 1
140+
# elif self.rotation == 3:
141+
# pass
142+
elif block_type is _left_s or block_type is _right_s or block_type is _t: # _left_s and _right_s and _t
143+
# if self.rotation == 1:
144+
# pass
145+
if rotation == 2:
146+
x += 1
147+
y -= 1
148+
# elif self.rotation == 3:
149+
# pass
150+
anchor_x = x * _block_unit_width
151+
anchor_y = y * _block_unit_width
111152
self.block_pen.setLevelRotation(angle, pivot_x, pivot_y, reach_in_millis) # calculated from _left and _top
112153
self.block_pen.setLevelAnchor(anchor_x, anchor_y, reach_in_millis)
113154
else:
@@ -186,28 +227,7 @@ def _draw_grid(grid: Grid, pen: LayerTurtle):
186227
_draw(x, y, color_number, pen)
187228

188229
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:
189-
if True:
190-
return _check_bad_block_grid_cells_placement(block_grid.grid_cells, block_grid_x_off, block_grid_y_offset, grid, check_boundary)
191-
else:
192-
for y in range(block_grid.n_rows):
193-
for x in range(block_grid.n_cols):
194-
if block_grid.get_value(y, x) != 0:
195-
row_idx = y + block_grid_y_offset
196-
col_idx = x + block_grid_x_off
197-
if row_idx < 0:
198-
continue # never mind above the grid
199-
if row_idx < 0 or row_idx >= grid.n_rows:
200-
if not check_boundary:
201-
continue
202-
return True
203-
if col_idx < 0 or col_idx >= grid.n_cols:
204-
if not check_boundary:
205-
continue
206-
return True
207-
if grid.get_value(row_idx, col_idx) != 0:
208-
return True
209-
return False
210-
230+
return _check_bad_block_grid_cells_placement(block_grid.grid_cells, block_grid_x_off, block_grid_y_offset, grid, check_boundary)
211231

212232
def _rotate_block_grid_if_possible(block_grid: Grid, block_grid_x_off: int, block_grid_y_offset: int, grid: Grid) -> (Grid, int):
213233
block_grid_cells = block_grid.grid_cells
@@ -216,7 +236,8 @@ def _rotate_block_grid_if_possible(block_grid: Grid, block_grid_x_off: int, bloc
216236
y_offset = 0
217237
if _check_bad_block_grid_cells_placement(rotated_cells, block_grid_x_off, block_grid_y_offset + y_offset, grid, check_boundary=True):
218238
return (None, None)
219-
rotated_block_grid = Grid(grid_cells=rotated_cells)
239+
grid_cell_type = block_grid.grid_cell_type
240+
rotated_block_grid = Grid(grid_cells=rotated_cells, grid_cell_type=grid_cell_type)
220241
return (rotated_block_grid, y_offset)
221242

222243

dumbdisplay_examples/tetris/_shapes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232

3333
def _randomize_block_grid() -> Grid:
3434
block_grid = random.choice(_shapes)
35-
if False:
36-
block_grid = _vertical_line
35+
# if True:
36+
# block_grid = _t
3737
color = random.randint(1, len(_colors) - 1)
38+
block_grid_cell_type = block_grid
3839
block_grid = [[color if cell != 0 else 0 for cell in row] for row in block_grid]
39-
return Grid(grid_cells=block_grid)
40+
return Grid(grid_cells=block_grid, grid_cell_type=block_grid_cell_type)
4041

dumbdisplay_examples/tetris/tetris_classic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
from dumbdisplay_examples.utils import DDAppBase, create_example_wifi_dd
1919

2020
_RANDOMIZE_ROW_COUNT = 2
21-
_ROTATE_WITH_LEVEL = False
21+
_ROTATE_WITH_LEVEL = True
2222

2323

2424
_delay = 0.3 # For time/sleep
25+
_level_animation_millis = 50
26+
27+
# _delay = 5000 # TODO: reset after debug
28+
# _level_animation_millis = 5000
2529

2630
def _check_grid(shape: 'Shape', score: LayerTurtle) -> (bool, int):
2731
grid = shape.grid
@@ -81,7 +85,7 @@ def reset_block(self) -> bool:
8185
y += 1 - block_grid.n_rows
8286
if _check_bad_block_grid_placement(block_grid, x, y, grid=self.grid, check_boundary=False):
8387
return False
84-
self.block = Block(x, y, block_grid=block_grid, block_pen=self.block_pen, rotate_with_level=_ROTATE_WITH_LEVEL)
88+
self.block = Block(x, y, block_grid=block_grid, block_pen=self.block_pen, move_reach_in_millis=_level_animation_millis, rotate_with_level=_ROTATE_WITH_LEVEL)
8589
self.sync_image()
8690
return True
8791

0 commit comments

Comments
 (0)