From d28f24cf0ac8ebe19cac155f97f4850f4d0f87da Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 4 Jan 2024 03:05:26 +0200 Subject: [PATCH] feat(game): add vertical borders style(game): rename method style(game): wrong method name style(game): fix wrong method name --- src/game/block.py | 3 +++ src/game/tetromino.py | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/game/block.py b/src/game/block.py index 01aee4b..19c5149 100644 --- a/src/game/block.py +++ b/src/game/block.py @@ -15,3 +15,6 @@ class Block(pygame.sprite.Sprite): def update(self) -> None: self.rect.topleft = self.pos * CONFIG.game.cell.width + + def vertical_collision(self, x: int) -> bool: + return not 0 <= x < CONFIG.game.columns diff --git a/src/game/tetromino.py b/src/game/tetromino.py index ad62ae4..66842de 100644 --- a/src/game/tetromino.py +++ b/src/game/tetromino.py @@ -24,9 +24,14 @@ class Tetromino: block.pos.y += 1 def move_horizontal(self, direction: Direction) -> None: - for block in self.blocks: - block.pos.x += direction.value + if not self._check_vertical_collision(self.blocks, direction): + for block in self.blocks: + block.pos.x += direction.value - def next_move_horizontal_collide(self, block: Block, direction: Direction) -> None: - for block in self.blocks: - block.pos.x += direction.value + def _check_vertical_collision( + self, blocks: list[Block], direction: Direction + ) -> bool: + return any( + block.vertical_collision(int(block.pos.x + direction.value)) + for block in self.blocks + )