From 482843b0782760881ba685530028267fd548cb60 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 28 Dec 2023 18:53:04 +0200 Subject: [PATCH] style(game): display readable block coords style(game): update log output --- src/py2048/block.py | 16 ++++++++++++---- src/py2048/game.py | 18 +++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/py2048/block.py b/src/py2048/block.py index d5eccb7..c59d525 100644 --- a/src/py2048/block.py +++ b/src/py2048/block.py @@ -7,6 +7,11 @@ from .colors import COLORS from .config import Config +def _show_pos(pos: int) -> int: + """Return the position in the grid.""" + return pos // Config.BLOCK_SIZE + 1 + + class Block(pygame.sprite.Sprite): def __init__(self, x: int, y: int, value: int | None = 2): """Initialize a block""" @@ -14,6 +19,9 @@ class Block(pygame.sprite.Sprite): self.image = pygame.Surface((Config.BLOCK_SIZE, Config.BLOCK_SIZE)) self.rect = self.image.get_rect() self.rect.topleft = (x, y) + + logger.debug(f"Generated block({id(self)}) at {self}") + self.value: int = value if value is not None else 2 if random.random() <= Config.BLOCK_VALUE_PROBABILITY else 4 self.font = pygame.font.SysFont(Config.FONT_FAMILY, Config.FONT_SIZE) self.update() @@ -29,11 +37,9 @@ class Block(pygame.sprite.Sprite): new_x = self.rect.x + dx new_y = self.rect.y + dy if 0 <= new_x <= Config.WIDTH - Config.BLOCK_SIZE and 0 <= new_y <= Config.HEIGHT - Config.BLOCK_SIZE: - logger.debug(f"Moving block({id(self)}): ({self.rect.x}, {self.rect.y}) => ({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 - else: - logger.debug("Move blocked: out of bounds") def update(self) -> None: """Update the block""" @@ -59,17 +65,19 @@ class Block(pygame.sprite.Sprite): def __add__(self, other: "Block") -> None: """Add the value of two blocks and update the current block""" + logger.debug(f"Merging blocks ({id(self)}) and ({id(other)}) => ({id(self)}), {self}") self.value += other.value self.update() def __iadd__(self, other: "Block") -> None: """Add the value of two blocks and updae the current block""" + logger.debug(f"Merging blocks ({id(self)}) and ({id(other)}) => ({id(self)}), {self}") self.value += other.value self.update() def __repr__(self) -> str: """Return a string representation of the block""" - return f"Block({self.rect.x}, {self.rect.y})" + return f"({_show_pos(self.rect.x)}, {_show_pos(self.rect.y)})" def __str__(self) -> str: """Return a string representation of the block""" diff --git a/src/py2048/game.py b/src/py2048/game.py index 10d0de8..69efe3f 100644 --- a/src/py2048/game.py +++ b/src/py2048/game.py @@ -78,18 +78,23 @@ class Game: def _move_blocks(self, dx: int, dy: int) -> None: """Move all the blocks by `dx` and `dy`.""" moved_blocks = pygame.sprite.Group() # Keep track of moved blocks to avoid double merging + blocks_to_remove = [] for block in self.sprites: block.move(dx, dy) - for block in self.sprites: # FIX: different value block merge - collidin_blocks = pygame.sprite.spritecollide(block, self.sprites, False) + for block in self.sprites: + colliding_blocks = pygame.sprite.spritecollide(block, self.sprites, False) - for other_block in collidin_blocks: + for other_block in colliding_blocks: if block != other_block and block.value == other_block.value and other_block not in moved_blocks: - block.increase_value() - self.sprites.remove(other_block) - moved_blocks.add(block) + new_block = block + other_block + moved_blocks.add(new_block) + blocks_to_remove.extend([block, other_block]) + + for block in blocks_to_remove: + self.sprites.remove(block) + self._generate_random_block() def _generate_random_block(self, count: int = 1) -> None: @@ -104,7 +109,6 @@ class Game: if not colliding_blocks: self.sprites.add(block) - logger.debug(f"Generated block({id(block)}) at ({x}, {y})") break def exit(self) -> None: