feat(game): add horizontal border

This commit is contained in:
Kristofers Solo 2024-01-04 03:12:51 +02:00
parent d28f24cf0a
commit dbc445b323
2 changed files with 16 additions and 2 deletions

View File

@ -18,3 +18,6 @@ class Block(pygame.sprite.Sprite):
def vertical_collision(self, x: int) -> bool:
return not 0 <= x < CONFIG.game.columns
def horizontal_collision(self, y: int) -> bool:
return y >= CONFIG.game.rows

View File

@ -20,8 +20,11 @@ class Tetromino:
]
def move_down(self) -> None:
for block in self.blocks:
block.pos.y += 1
if not self._check_horizontal_collision(
self.blocks, Direction.RIGHT
): # Direction.RIGHT = 1 aka DOWN
for block in self.blocks:
block.pos.y += 1
def move_horizontal(self, direction: Direction) -> None:
if not self._check_vertical_collision(self.blocks, direction):
@ -35,3 +38,11 @@ class Tetromino:
block.vertical_collision(int(block.pos.x + direction.value))
for block in self.blocks
)
def _check_horizontal_collision(
self, blocks: list[Block], direction: Direction
) -> bool:
return any(
block.horizontal_collision(int(block.pos.y + direction.value))
for block in self.blocks
)