From cad0e87663cd88f6db611109b07754ad44e283e0 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Fri, 29 Dec 2023 16:18:49 +0200 Subject: [PATCH] fix(game): block movement --- src/py2048/block.py | 21 +++++++++++++++------ src/py2048/grid.py | 1 + 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/py2048/block.py b/src/py2048/block.py index 9a38451..d115f5c 100644 --- a/src/py2048/block.py +++ b/src/py2048/block.py @@ -20,7 +20,7 @@ class Block(pygame.sprite.Sprite): super().__init__() self.image = pygame.Surface((Config.BLOCK_SIZE, Config.BLOCK_SIZE)) self.rect = self.image.get_rect() - self.rect.topleft = (x, y) + self.rect.topleft = x, y logger.debug(f"Generated block({id(self)}) at {self}") @@ -38,13 +38,22 @@ class Block(pygame.sprite.Sprite): """Move the block by `dx` and `dy`.""" dx, dy = direction.value - new_x = self.rect.x + dx * Config.BLOCK_SIZE - new_y = self.rect.y + dy * Config.BLOCK_SIZE + while True: + new_x = self.rect.x + dx * Config.BLOCK_SIZE + new_y = self.rect.y + dy * Config.BLOCK_SIZE - if 0 <= new_x <= Config.WIDTH - Config.BLOCK_SIZE and 0 <= new_y <= Config.HEIGHT - Config.BLOCK_SIZE: + if not (0 <= new_x <= Config.WIDTH - Config.BLOCK_SIZE and 0 <= new_y <= Config.HEIGHT - Config.BLOCK_SIZE): + # logger.debug(f"Block({id(self)}) stayed at {self} (out of bounds)") + break + + collision = any(block.rect.collidepoint(new_x, new_y) for block in self.groups()[0] if block != self) + + if collision: + logger.debug(f"Block({id(self)}) collided with another block, stopped at {self}") + break + + self.rect.topleft = new_x, new_y logger.debug(f"Moving block({id(self)}): {self} => ({_show_pos(new_x)}, {_show_pos(new_y)})") - self.rect.x = new_x - self.rect.y = new_y def update(self) -> None: """Update the block""" diff --git a/src/py2048/grid.py b/src/py2048/grid.py index 43447a8..ac981fa 100644 --- a/src/py2048/grid.py +++ b/src/py2048/grid.py @@ -9,6 +9,7 @@ from .utils import Direction class Grid(pygame.sprite.Group): def move(self, direction: Direction): + block: Block for block in self: block.move(direction)