33from 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
4749class 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
0 commit comments