Skip to content

Commit b7d199a

Browse files
committed
adding TetrisTwoBlockApp
1 parent 903731f commit b7d199a

2 files changed

Lines changed: 75 additions & 37 deletions

File tree

dumbdisplay_examples/tetris/tetris_common.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,44 @@ def set_value(self, row_idx, col_idx, value):
4242
self.grid_dirty[row_idx][col_idx] = True
4343

4444

45+
class Block:
46+
def __init__(self, x: int, y: int, block_grid: Grid, block_pen: LayerTurtle):
47+
self.x = x
48+
self.y = y
49+
self.block_grid = block_grid
50+
self.block_pen = block_pen
51+
block_pen.clear()
52+
self.sync_image()
53+
_draw_grid(block_grid, block_pen)
54+
55+
def move_down(self, grid: Grid) -> bool:
56+
if _check_can_place_block_grid(self.block_grid, self.x, self.y + 1, grid=grid):
57+
return False
58+
self.y += 1
59+
self.sync_image()
60+
return True
61+
62+
def move_right(self, grid: Grid) -> bool:
63+
if _check_can_place_block_grid(self.block_grid, self.x + 1, self.y, grid=grid):
64+
return False
65+
self.x += 1
66+
self.sync_image()
67+
return True
68+
69+
def move_left(self, grid: Grid) -> bool:
70+
if _check_can_place_block_grid(self.block_grid, self.x - 1, self.y, grid=grid):
71+
return False
72+
self.x -= 1
73+
self.sync_image()
74+
return True
75+
76+
def sync_image(self):
77+
#anchor_x = (self.x - _INIT_BLOCK_X) * _block_unit_width
78+
anchor_x = self.x * _block_unit_width
79+
anchor_y = self.y * _block_unit_width
80+
self.block_pen.setLevelAnchor(anchor_x, anchor_y)
81+
82+
4583
def _draw(x, y, color_number, pen: LayerTurtle):
4684
screen_x = _left + (x * _block_unit_width) # each turtle 20x20 pixels
4785
screen_y = _top - (y * _block_unit_width)

dumbdisplay_examples/tetris/tetris_two_block.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from dumbdisplay.layer_turtle import LayerTurtle
1111
from dumbdisplay.layer_lcd import LayerLcd
1212
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
13+
_width, _height, _left, _top, _draw_grid, _check_can_place_block_grid, _commit_block_grid, Block
1414

1515
from dumbdisplay_examples.utils import DDAppBase, create_example_wifi_dd
1616

@@ -139,42 +139,42 @@ def _randomize_grid() -> Grid:
139139
#
140140
#
141141

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)
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)
178178

179179

180180

0 commit comments

Comments
 (0)