1818
1919
2020class 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
4950class 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
188229def _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
212232def _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
0 commit comments