1414
1515from dumbdisplay_examples .utils import DDAppBase , create_example_wifi_dd
1616
17- #_USE_LEVEL_ANCHOR_FOR_BLOCK = True
18- #_INIT_BLOCK_X = 5
19- _RANDOMIZE_ROW_COUNT = 1
17+ _RANDOMIZE_ROW_COUNT = 4
2018
2119
2220
23- # _width = 400
24- # _height = 700
25- # _top = 230
26- # _left = -110
27- # _block_unit_width = 20
28- # #_colors = ['black', 'red', 'lightblue', 'blue', 'orange', 'yellow', 'green', 'purple']
29- # _colors = ['black', 'crimson', 'cyan', 'ivory', 'coral', 'gold', 'lime', 'magenta']
30-
31-
3221_delay = 0.3 # For time/sleep
3322_grid = [ # 12x24
3423 [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ],
5645 [0 ,1 ,1 ,2 ,3 ,0 ,0 ,0 ,4 ,0 ,2 ,0 ],
5746 [2 ,0 ,1 ,2 ,3 ,0 ,6 ,5 ,5 ,5 ,0 ,2 ]
5847]
59- # _grid_n_cols = len(_grid[0]) # should be 12
60- # _grid_n_rows = len(_grid) # should be 24
61-
62-
63- # class Grid:
64- # def __init__(self):
65- # if _RANDOMIZE_ROW_COUNT >= 0:
66- # grid = []
67- # for y in range(_grid_n_rows):
68- # grid_row = []
69- # for x in range(_grid_n_cols):
70- # if y >= (_grid_n_rows - _RANDOMIZE_ROW_COUNT) and random.random() < 0.7:
71- # color = random.randint(1, len(_colors) - 1)
72- # else:
73- # color = 0
74- # grid_row.append(color)
75- # grid.append(grid_row)
76- # self.grid = grid
77- # else:
78- # self.grid = []
79- # for grid_row in _grid:
80- # self.grid.append(grid_row.copy())
81- # self.grid_dirty = []
82- # for grid_row in self.grid:
83- # grid_dirty_row = []
84- # for cell in grid_row:
85- # dirty = True if cell != 0 else False
86- # grid_dirty_row.append(dirty)
87- # self.grid_dirty.append(grid_dirty_row)
88- # self.n_cols = _grid_n_cols
89- # self.n_rows = _grid_n_rows
90- #
91- # def check_reset_need_redraw(self, row_idx, col_idx):
92- # dirty = self.grid_dirty[row_idx][col_idx]
93- # if not dirty:
94- # return False
95- # self.grid_dirty[row_idx][col_idx] = False
96- # return True
97- #
98- # def get_value(self, row_idx, col_idx):
99- # return self.grid[row_idx][col_idx]
100- #
101- # def set_value(self, row_idx, col_idx, value):
102- # if self.grid[row_idx][col_idx] != value:
103- # self.grid[row_idx][col_idx] = value
104- # self.grid_dirty[row_idx][col_idx] = True
105- # # old_value = self.grid[row_idx][col_idx]
106- # # self.grid[row_idx][col_idx] = value
107- # # self.grid_dirty[row_idx][col_idx] = old_value != value
108- #
109- #
110-
111-
112- # def _calc_screen_position(x: int, y : int) -> (int, int):
113- # screen_x = _left + (x * 20) # each turtle 20x20 pixels
114- # screen_y = _top - (y * 20)
115- # return (screen_x, screen_y)
11648
11749
11850def _create_grid () -> Grid :
@@ -134,40 +66,12 @@ def _create_grid() -> Grid:
13466 return Grid (grid )
13567
13668
137- # def _draw(x, y, color_number, pen: LayerTurtle):
138- # screen_x = _left + (x * _block_unit_width) # each turtle 20x20 pixels
139- # screen_y = _top - (y * _block_unit_width)
140- # # (screen_x, screen_y) = _calc_screen_position(x, y)
141- # color = _colors[color_number]
142- # pen.penColor(color)
143- # pen.goTo(screen_x, screen_y, with_pen=False)
144- # pen.rectangle(_block_unit_width - 2, _block_unit_width - 2, centered=True)
145- #
146- # # def _draw_block(block: 'Block', block_pen: LayerTurtle):
147- # # block_pen.clear()
148- # # _draw(block.x, block.y, block.color, block_pen)
149- #
150- # def _draw_grid(grid: Grid, pen: LayerTurtle):
151- # for y in range(grid.n_rows):
152- # for x in range(grid.n_cols):
153- # if not grid.check_reset_need_redraw(y, x):
154- # continue
155- # color_number = grid.get_value(y, x)
156- # _draw(x, y, color_number, pen)
157-
158-
159-
16069class OneBlock :
16170 def __init__ (self , x : int , y : int , block_pen : LayerTurtle ):
162- # self.x = _INIT_BLOCK_X
163- # self.y = 0
16471 self .x = x
16572 self .y = y
16673 self .color = random .randint (1 , len (_colors ) - 1 )
16774 self .block_pen = block_pen
168- # if _USE_LEVEL_ANCHOR_FOR_BLOCK:
169- # self.block_pen.clear()
170- # _draw(self.x, self.y, self.color, self.block_pen)
17175 self .sync_image ()
17276
17377 def commit (self , grid : Grid ):
@@ -377,23 +281,17 @@ def update(self):
377281 elif not moved_down :
378282 if not self .resetBlock ():
379283 self .endGame (won = False )
380- # if self.shape.block.y > 0:
381- # #self.shape.reset_block()
382- # self.resetBlock()
383- # else:
384- # self.endGame(won=False)
385284
386285 def startGame (self ):
387286 self .score .clear ()
388287 self .score .write ('Score: 0' , 'C' )
389288 self .pen .clear ()
390289 self .block_pen .clear ()
391290 self .shape = Shape (pen = self .pen , block_pen = self .block_pen )
392- #self.resetBlock( )
291+ print ( "... started game" )
393292
394293
395294 def endGame (self , won : bool ):
396- #self.block_pen.clear()
397295 self .shape = None
398296 if won :
399297 msg = "🥳 YOU WON 🥳"
@@ -406,45 +304,29 @@ def endGame(self, won: bool):
406304 self .pen .oval (300 , 100 , centered = True )
407305 self .pen .penColor (color )
408306 self .pen .write (msg , align = 'C' )
307+ print ("... ended game" )
409308
410309
411310 def checkGrid (self ) -> bool :
412311 return self .shape .check_grid (score = self .score )
413- # check_result = check_grid(shape=self.shape, score=self.score)
414- # self.drawGrid() # should only redraw if any lines were cleared
415- # return check_result
416312
417313 def resetBlock (self ) -> bool :
418314 return self .shape .reset_block ()
419- #self.drawGrid()
420- #self.drawBlock()
421315
422316 def moveBlockDown (self ) -> bool :
423317 return self .shape .move_block_down ()
424- # if self.shape.move_block_down():
425- # self.drawBlock()
426- # return True
427- # return False
428318
429319 def moveBlockLeft (self ) -> bool :
430320 if self .shape is None :
431321 self .startGame ()
432322 return False
433323 return self .shape .move_block_left ()
434- # if self.shape.move_block_left():
435- # self.drawBlock()
436- # return True
437- # return False
438324
439325 def moveBlockRight (self ) -> bool :
440326 if self .shape is None :
441327 self .startGame ()
442328 return False
443329 return self .shape .move_block_right ()
444- # if self.shape.move_block_right():
445- # self.drawBlock()
446- # return True
447- # return False
448330
449331
450332if __name__ == "__main__" :
0 commit comments