1212
1313from dumbdisplay_examples .utils import DDAppBase , create_example_wifi_dd
1414
15- #_INIT_BLOCK_X = 5
16- _RANDOMIZE_ROW_COUNT = 4
15+ _RANDOMIZE_ROW_COUNT = 21
1716
1817
1918_width = 400
2322_block_unit_width = 20
2423#_colors = ['black', 'red', 'lightblue', 'blue', 'orange', 'yellow', 'green', 'purple']
2524_colors = ['black' , 'crimson' , 'cyan' , 'ivory' , 'coral' , 'gold' , 'lime' , 'magenta' ]
25+ _grid_n_cols = 12
26+ _grid_n_rows = 24
2627
2728
2829_delay = 0.3 # For time/sleep
29- _grid_n_cols = 12
30- _grid_n_rows = 24
3130
3231class Grid :
33- def __init__ (self , grid ):
34- self .grid = grid
32+ def __init__ (self , grid_cells ):
33+ self .grid_cells = grid_cells
3534 self .grid_dirty = []
36- for grid_row in self .grid :
35+ for grid_row in self .grid_cells :
3736 grid_dirty_row = []
3837 for cell in grid_row :
3938 dirty = True if cell != 0 else False
4039 grid_dirty_row .append (dirty )
4140 self .grid_dirty .append (grid_dirty_row )
42- self .n_cols = len (self .grid [0 ])
43- self .n_rows = len (self .grid )
41+ self .n_cols = len (self .grid_cells [0 ])
42+ self .n_rows = len (self .grid_cells )
4443
4544 def check_reset_need_redraw (self , row_idx , col_idx ):
4645 dirty = self .grid_dirty [row_idx ][col_idx ]
@@ -50,11 +49,11 @@ def check_reset_need_redraw(self, row_idx, col_idx):
5049 return True
5150
5251 def get_value (self , row_idx , col_idx ):
53- return self .grid [row_idx ][col_idx ]
52+ return self .grid_cells [row_idx ][col_idx ]
5453
5554 def set_value (self , row_idx , col_idx , value ):
56- if self .grid [row_idx ][col_idx ] != value :
57- self .grid [row_idx ][col_idx ] = value
55+ if self .grid_cells [row_idx ][col_idx ] != value :
56+ self .grid_cells [row_idx ][col_idx ] = value
5857 self .grid_dirty [row_idx ][col_idx ] = True
5958
6059
@@ -71,10 +70,10 @@ def set_value(self, row_idx, col_idx, value):
7170def _randomize_block_grid () -> Grid :
7271 color = random .randint (1 , len (_colors ) - 1 )
7372 block_grid = [[color ]]
74- return Grid (grid = block_grid )
73+ return Grid (grid_cells = block_grid )
7574
7675def _randomize_grid () -> Grid :
77- grid = []
76+ grid_cells = []
7877 for y in range (_grid_n_rows ):
7978 grid_row = []
8079 for x in range (_grid_n_cols ):
@@ -83,8 +82,8 @@ def _randomize_grid() -> Grid:
8382 else :
8483 color = 0
8584 grid_row .append (color )
86- grid .append (grid_row )
87- return Grid (grid = grid )
85+ grid_cells .append (grid_row )
86+ return Grid (grid_cells = grid_cells )
8887
8988def _check_block_grid_overlap (block_grid : Grid , block_grid_x_off : int , block_grid_y_offset : int , grid : Grid ) -> bool :
9089 for y in range (block_grid .n_rows ):
@@ -94,6 +93,15 @@ def _check_block_grid_overlap(block_grid: Grid, block_grid_x_off: int, block_gri
9493 return True
9594 return False
9695
96+
97+ def _commit_block_grid (block_grid : Grid , block_grid_x_off : int , block_grid_y_offset : int , grid : Grid ):
98+ for y in range (block_grid .n_rows ):
99+ for x in range (block_grid .n_cols ):
100+ color = block_grid .get_value (y , x )
101+ if color != 0 :
102+ grid .set_value (y + block_grid_y_offset , x + block_grid_x_off , color )
103+
104+
97105def _draw (x , y , color_number , pen : LayerTurtle ):
98106 screen_x = _left + (x * _block_unit_width ) # each turtle 20x20 pixels
99107 screen_y = _top - (y * _block_unit_width )
@@ -115,18 +123,14 @@ def _draw_grid(grid: Grid, pen: LayerTurtle):
115123
116124class Block :
117125 def __init__ (self , x : int , y : int , block_grid : Grid , block_pen : LayerTurtle ):
118- # self.x = _INIT_BLOCK_X
119- # self.y = 0
120126 self .x = x
121127 self .y = y
122128 self .block_grid = block_grid
123129 self .color = block_grid .get_value (0 , 0 ) # assume a single cell
124- #self.color = random.randint(1, len(_colors) - 1)
125130 self .block_pen = block_pen
126- self . block_pen .clear ()
131+ block_pen .clear ()
127132 self .sync_image ()
128- #_draw_grid(block_grid, block_pen)
129- #_draw(self.x, self.y, self.color, self.block_pen)
133+ _draw_grid (block_grid , block_pen )
130134
131135 def commit (self , grid : Grid ):
132136 grid .set_value (self .y , self .x , self .color )
@@ -157,8 +161,10 @@ def move_left(self, grid: Grid) -> bool:
157161 return False
158162
159163 def sync_image (self ):
160- self .block_pen .clear ()
161- _draw (self .x , self .y , self .color , self .block_pen )
164+ #anchor_x = (self.x - _INIT_BLOCK_X) * _block_unit_width
165+ anchor_x = self .x * _block_unit_width
166+ anchor_y = self .y * _block_unit_width
167+ self .block_pen .setLevelAnchor (anchor_x , anchor_y )
162168
163169
164170
@@ -223,7 +229,8 @@ def reset_block(self) -> bool:
223229 return True
224230
225231 def commit_block (self ):
226- self .block .commit (self .grid )
232+ _commit_block_grid (self .block .block_grid , self .block .x , self .block .y , self .grid )
233+ #self.block.commit(self.grid)
227234 self .sync_image ()
228235 self .block = None
229236
0 commit comments