|
10 | 10 | from dumbdisplay.layer_turtle import LayerTurtle |
11 | 11 | from dumbdisplay.layer_lcd import LayerLcd |
12 | 12 | from dumbdisplay_examples.tetris.tetris_common import Grid, _colors, _grid_n_rows, _grid_n_cols, _block_unit_width, \ |
13 | | - _width, _height, _left, _top, _draw_grid, _check_can_place_block_grid, _commit_block_grid, Block |
| 13 | + _width, _height, _left, _top, _draw_grid, _commit_block_grid, Block, \ |
| 14 | + _check_block_grid_placement |
14 | 15 |
|
15 | 16 | from dumbdisplay_examples.utils import DDAppBase, create_example_wifi_dd |
16 | 17 |
|
17 | 18 | _RANDOMIZE_ROW_COUNT = 2 |
18 | 19 |
|
19 | 20 |
|
20 | | -# _width = 400 |
21 | | -# _height = 700 |
22 | | -# _top = 230 |
23 | | -# _left = -110 |
24 | | -# _block_unit_width = 20 |
25 | | -# #_colors = ['black', 'red', 'lightblue', 'blue', 'orange', 'yellow', 'green', 'purple'] |
26 | | -# _colors = ['black', 'crimson', 'cyan', 'ivory', 'coral', 'gold', 'lime', 'magenta'] |
27 | | -# _grid_n_cols = 12 |
28 | | -# _grid_n_rows = 24 |
29 | | - |
30 | | - |
31 | 21 | _delay = 0.3 # For time/sleep |
32 | 22 |
|
33 | | -# class Grid: |
34 | | -# def __init__(self, grid_cells): |
35 | | -# self.grid_cells = grid_cells |
36 | | -# self.grid_dirty = [] |
37 | | -# for grid_row in self.grid_cells: |
38 | | -# grid_dirty_row = [] |
39 | | -# for cell in grid_row: |
40 | | -# dirty = True if cell != 0 else False |
41 | | -# grid_dirty_row.append(dirty) |
42 | | -# self.grid_dirty.append(grid_dirty_row) |
43 | | -# self.n_cols = len(self.grid_cells[0]) |
44 | | -# self.n_rows = len(self.grid_cells) |
45 | | -# |
46 | | -# def check_reset_need_redraw(self, row_idx, col_idx): |
47 | | -# dirty = self.grid_dirty[row_idx][col_idx] |
48 | | -# if not dirty: |
49 | | -# return False |
50 | | -# self.grid_dirty[row_idx][col_idx] = False |
51 | | -# return True |
52 | | -# |
53 | | -# def get_value(self, row_idx, col_idx): |
54 | | -# return self.grid_cells[row_idx][col_idx] |
55 | | -# |
56 | | -# def set_value(self, row_idx, col_idx, value): |
57 | | -# if self.grid_cells[row_idx][col_idx] != value: |
58 | | -# self.grid_cells[row_idx][col_idx] = value |
59 | | -# self.grid_dirty[row_idx][col_idx] = True |
60 | | - |
61 | | - |
62 | | -# def _draw(x, y, color_number, pen: LayerTurtle): |
63 | | -# screen_x = _left + (x * _block_unit_width) # each turtle 20x20 pixels |
64 | | -# screen_y = _top - (y * _block_unit_width) |
65 | | -# # (screen_x, screen_y) = _calc_screen_position(x, y) |
66 | | -# color = _colors[color_number] |
67 | | -# pen.penColor(color) |
68 | | -# pen.goTo(screen_x, screen_y, with_pen=False) |
69 | | -# pen.rectangle(_block_unit_width - 2, _block_unit_width - 2, centered=True) |
70 | | - |
71 | 23 |
|
72 | 24 | def _randomize_block_grid() -> Grid: |
73 | 25 | color = random.randint(1, len(_colors) - 1) |
@@ -97,86 +49,6 @@ def _randomize_grid() -> Grid: |
97 | 49 | grid_cells.append(grid_row) |
98 | 50 | return Grid(grid_cells=grid_cells) |
99 | 51 |
|
100 | | -# def _check_can_place_block_grid(block_grid: Grid, block_grid_x_off: int, block_grid_y_offset: int, grid: Grid) -> bool: |
101 | | -# for y in range(block_grid.n_rows): |
102 | | -# for x in range(block_grid.n_cols): |
103 | | -# if block_grid.get_value(y, x) != 0: |
104 | | -# row_idx = y + block_grid_y_offset |
105 | | -# col_idx = x + block_grid_x_off |
106 | | -# if row_idx < 0 or row_idx >= grid.n_rows: |
107 | | -# return True |
108 | | -# if col_idx < 0 or col_idx >= grid.n_cols: |
109 | | -# return True |
110 | | -# if grid.get_value(row_idx, col_idx) != 0: |
111 | | -# return True |
112 | | -# return False |
113 | | -# |
114 | | -# |
115 | | -# def _commit_block_grid(block_grid: Grid, block_grid_x_off: int, block_grid_y_offset: int, grid: Grid): |
116 | | -# for y in range(block_grid.n_rows): |
117 | | -# for x in range(block_grid.n_cols): |
118 | | -# color = block_grid.get_value(y, x) |
119 | | -# if color != 0: |
120 | | -# grid.set_value(y + block_grid_y_offset, x + block_grid_x_off, color) |
121 | | - |
122 | | - |
123 | | -# def _draw(x, y, color_number, pen: LayerTurtle): |
124 | | -# screen_x = _left + (x * _block_unit_width) # each turtle 20x20 pixels |
125 | | -# screen_y = _top - (y * _block_unit_width) |
126 | | -# # (screen_x, screen_y) = _calc_screen_position(x, y) |
127 | | -# color = _colors[color_number] |
128 | | -# pen.penColor(color) |
129 | | -# pen.goTo(screen_x, screen_y, with_pen=False) |
130 | | -# pen.rectangle(_block_unit_width - 2, _block_unit_width - 2, centered=True) |
131 | | -# |
132 | | -# def _draw_grid(grid: Grid, pen: LayerTurtle): |
133 | | -# for y in range(grid.n_rows): |
134 | | -# for x in range(grid.n_cols): |
135 | | -# if not grid.check_reset_need_redraw(y, x): |
136 | | -# continue |
137 | | -# color_number = grid.get_value(y, x) |
138 | | -# _draw(x, y, color_number, pen) |
139 | | -# |
140 | | -# |
141 | | - |
142 | | -# class Block: |
143 | | -# def __init__(self, x: int, y: int, block_grid: Grid, block_pen: LayerTurtle): |
144 | | -# self.x = x |
145 | | -# self.y = y |
146 | | -# self.block_grid = block_grid |
147 | | -# self.block_pen = block_pen |
148 | | -# block_pen.clear() |
149 | | -# self.sync_image() |
150 | | -# _draw_grid(block_grid, block_pen) |
151 | | -# |
152 | | -# def move_down(self, grid: Grid) -> bool: |
153 | | -# if _check_can_place_block_grid(self.block_grid, self.x, self.y + 1, grid=grid): |
154 | | -# return False |
155 | | -# self.y += 1 |
156 | | -# self.sync_image() |
157 | | -# return True |
158 | | -# |
159 | | -# def move_right(self, grid: Grid) -> bool: |
160 | | -# if _check_can_place_block_grid(self.block_grid, self.x + 1, self.y, grid=grid): |
161 | | -# return False |
162 | | -# self.x += 1 |
163 | | -# self.sync_image() |
164 | | -# return True |
165 | | -# |
166 | | -# def move_left(self, grid: Grid) -> bool: |
167 | | -# if _check_can_place_block_grid(self.block_grid, self.x - 1, self.y, grid=grid): |
168 | | -# return False |
169 | | -# self.x -= 1 |
170 | | -# self.sync_image() |
171 | | -# return True |
172 | | -# |
173 | | -# def sync_image(self): |
174 | | -# #anchor_x = (self.x - _INIT_BLOCK_X) * _block_unit_width |
175 | | -# anchor_x = self.x * _block_unit_width |
176 | | -# anchor_y = self.y * _block_unit_width |
177 | | -# self.block_pen.setLevelAnchor(anchor_x, anchor_y) |
178 | | - |
179 | | - |
180 | 52 |
|
181 | 53 | def _check_grid(shape: 'Shape', score: LayerTurtle) -> (bool, int): |
182 | 54 | grid = shape.grid |
@@ -232,9 +104,9 @@ def reset_block(self) -> bool: |
232 | 104 | x = 5 |
233 | 105 | y = 0 |
234 | 106 | block_grid = _randomize_block_grid() |
235 | | - x -= 1 - block_grid.n_cols |
236 | | - y -= 1 - block_grid.n_rows |
237 | | - if _check_can_place_block_grid(block_grid, x, y, grid=self.grid): |
| 107 | + x -= int((block_grid.n_cols - 1) / 2) |
| 108 | + y += 1 - block_grid.n_rows |
| 109 | + if _check_block_grid_placement(block_grid, x, y, grid=self.grid, check_boundary=False): |
238 | 110 | return False |
239 | 111 | self.block = Block(x, y, block_grid=block_grid, block_pen=self.block_pen) |
240 | 112 | self.sync_image() |
|
0 commit comments