|
| 1 | +from dumbdisplay.layer_turtle import LayerTurtle |
| 2 | + |
| 3 | + |
| 4 | +_width = 400 |
| 5 | +_height = 700 |
| 6 | +_top = 230 |
| 7 | +_left = -110 |
| 8 | +_block_unit_width = 20 |
| 9 | +#_colors = ['black', 'red', 'lightblue', 'blue', 'orange', 'yellow', 'green', 'purple'] |
| 10 | +_colors = ['black', 'crimson', 'cyan', 'ivory', 'coral', 'gold', 'lime', 'magenta'] |
| 11 | + |
| 12 | +_grid_n_cols = 12 |
| 13 | +_grid_n_rows = 24 |
| 14 | + |
| 15 | + |
| 16 | +class Grid: |
| 17 | + def __init__(self, grid_cells): |
| 18 | + self.grid_cells = grid_cells |
| 19 | + self.grid_dirty = [] |
| 20 | + for grid_row in self.grid_cells: |
| 21 | + grid_dirty_row = [] |
| 22 | + for cell in grid_row: |
| 23 | + dirty = True if cell != 0 else False |
| 24 | + grid_dirty_row.append(dirty) |
| 25 | + self.grid_dirty.append(grid_dirty_row) |
| 26 | + self.n_cols = len(self.grid_cells[0]) |
| 27 | + self.n_rows = len(self.grid_cells) |
| 28 | + |
| 29 | + def check_reset_need_redraw(self, row_idx, col_idx): |
| 30 | + dirty = self.grid_dirty[row_idx][col_idx] |
| 31 | + if not dirty: |
| 32 | + return False |
| 33 | + self.grid_dirty[row_idx][col_idx] = False |
| 34 | + return True |
| 35 | + |
| 36 | + def get_value(self, row_idx, col_idx): |
| 37 | + return self.grid_cells[row_idx][col_idx] |
| 38 | + |
| 39 | + def set_value(self, row_idx, col_idx, value): |
| 40 | + if self.grid_cells[row_idx][col_idx] != value: |
| 41 | + self.grid_cells[row_idx][col_idx] = value |
| 42 | + self.grid_dirty[row_idx][col_idx] = True |
| 43 | + |
| 44 | + |
| 45 | +def _draw(x, y, color_number, pen: LayerTurtle): |
| 46 | + screen_x = _left + (x * _block_unit_width) # each turtle 20x20 pixels |
| 47 | + screen_y = _top - (y * _block_unit_width) |
| 48 | + # (screen_x, screen_y) = _calc_screen_position(x, y) |
| 49 | + color = _colors[color_number] |
| 50 | + pen.penColor(color) |
| 51 | + pen.goTo(screen_x, screen_y, with_pen=False) |
| 52 | + pen.rectangle(_block_unit_width - 2, _block_unit_width - 2, centered=True) |
| 53 | + |
| 54 | +# def _draw_block(block: 'Block', block_pen: LayerTurtle): |
| 55 | +# block_pen.clear() |
| 56 | +# _draw(block.x, block.y, block.color, block_pen) |
| 57 | + |
| 58 | +def _draw_grid(grid: Grid, pen: LayerTurtle): |
| 59 | + for y in range(grid.n_rows): |
| 60 | + for x in range(grid.n_cols): |
| 61 | + if not grid.check_reset_need_redraw(y, x): |
| 62 | + continue |
| 63 | + color_number = grid.get_value(y, x) |
| 64 | + _draw(x, y, color_number, pen) |
| 65 | + |
| 66 | + |
0 commit comments