Skip to content

Commit 9774463

Browse files
committed
working on TetrisTwoBlockApp
1 parent 464a2a8 commit 9774463

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

dumbdisplay_examples/tetris/_common.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from dumbdisplay.layer_turtle import LayerTurtle
44

55

6+
7+
68
_width = 400
79
_height = 700
810
_top = 230
@@ -45,13 +47,16 @@ def set_value(self, row_idx, col_idx, value):
4547

4648

4749
class Block:
48-
def __init__(self, x: int, y: int, block_grid: Grid, block_pen: LayerTurtle):
50+
def __init__(self, x: int, y: int, block_grid: Grid, block_pen: LayerTurtle, rotate_with_level: bool = False):
4951
self.x = x
5052
self.y = y
53+
self.rotation = 0
5154
self.block_grid = block_grid
5255
self.block_pen = block_pen
56+
self.rotate_with_level = rotate_with_level
57+
self.move_reach_in_millis = 50
5358
block_pen.clear()
54-
if True:
59+
if not rotate_with_level:
5560
# make the block tiled a bit
5661
block_pen.setLevelRotation(2, 90, 120) # calculated from _left and _top
5762
self.sync_image()
@@ -61,22 +66,21 @@ def move_down(self, grid: Grid) -> bool:
6166
if _check_bad_block_grid_placement(self.block_grid, self.x, self.y + 1, grid=grid):
6267
return False
6368
self.y += 1
64-
self.sync_image()
69+
self.sync_image(self.move_reach_in_millis)
6570
return True
6671

6772
def move_right(self, grid: Grid) -> bool:
6873
if _check_bad_block_grid_placement(self.block_grid, self.x + 1, self.y, grid=grid):
6974
return False
7075
self.x += 1
71-
self.sync_image()
76+
self.sync_image(self.move_reach_in_millis)
7277
return True
7378

7479
def move_left(self, grid: Grid) -> bool:
7580
if _check_bad_block_grid_placement(self.block_grid, self.x - 1, self.y, grid=grid):
7681
return False
7782
self.x -= 1
78-
#print(f"* left ==> x={self.x}")
79-
self.sync_image()
83+
self.sync_image(self.move_reach_in_millis)
8084
return True
8185

8286
def rotate(self, grid: Grid) -> bool:
@@ -85,15 +89,21 @@ def rotate(self, grid: Grid) -> bool:
8589
return False
8690
self.block_grid = rotated_block_grid
8791
self.y += y_offset
88-
self.block_pen.clear()
89-
_draw_grid(self.block_grid, self.block_pen)
92+
self.rotation = (self.rotation + 90) % 360
93+
if self.rotate_with_level:
94+
self.sync_image(self.move_reach_in_millis)
95+
else:
96+
self.block_pen.clear()
97+
_draw_grid(self.block_grid, self.block_pen)
9098
return True
9199

92100

93-
def sync_image(self):
101+
def sync_image(self, reach_in_millis: int = 0):
94102
anchor_x = self.x * _block_unit_width
95103
anchor_y = self.y * _block_unit_width
96-
self.block_pen.setLevelAnchor(anchor_x, anchor_y)
104+
self.block_pen.setLevelAnchor(anchor_x, anchor_y, reach_in_millis)
105+
if self.rotate_with_level:
106+
self.block_pen.setLevelRotation(self.rotation + 2, 90 + 10, 120 + 10, reach_in_millis) # calculated from _left and _top
97107

98108

99109

dumbdisplay_examples/tetris/tetris_classic.py

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

2020
_RANDOMIZE_ROW_COUNT = 2
21+
_ROTATE_WITH_LEVEL = True
2122

2223

2324
_delay = 0.3 # For time/sleep
@@ -80,7 +81,7 @@ def reset_block(self) -> bool:
8081
y += 1 - block_grid.n_rows
8182
if _check_bad_block_grid_placement(block_grid, x, y, grid=self.grid, check_boundary=False):
8283
return False
83-
self.block = Block(x, y, block_grid=block_grid, block_pen=self.block_pen)
84+
self.block = Block(x, y, block_grid=block_grid, block_pen=self.block_pen, rotate_with_level=_ROTATE_WITH_LEVEL)
8485
self.sync_image()
8586
return True
8687

0 commit comments

Comments
 (0)