mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
refactor(game): remove redundant debug messaged
This commit is contained in:
parent
df7015a7d4
commit
d2f898358b
@ -19,8 +19,6 @@ class Block(pygame.sprite.Sprite):
|
|||||||
self.rect = self.image.get_rect()
|
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}")
|
|
||||||
|
|
||||||
self.value: int = value if value is not None else 2 if random.random() <= Config.BLOCK_VALUE_PROBABILITY else 4
|
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.font = pygame.font.SysFont(Config.FONT_FAMILY, Config.FONT_SIZE)
|
||||||
self.update()
|
self.update()
|
||||||
@ -39,7 +37,6 @@ class Block(pygame.sprite.Sprite):
|
|||||||
new_x, new_y = self._calc_new_pos(direction)
|
new_x, new_y = self._calc_new_pos(direction)
|
||||||
|
|
||||||
if self._is_out_if_bounds(new_x, new_y):
|
if self._is_out_if_bounds(new_x, new_y):
|
||||||
logger.debug(f"Block({id(self)}) stayed at {self.pos()} (out of bounds)")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if self._has_collision(new_x, new_y):
|
if self._has_collision(new_x, new_y):
|
||||||
@ -47,11 +44,9 @@ class Block(pygame.sprite.Sprite):
|
|||||||
if collided_block and collided_block.value == self.value:
|
if collided_block and collided_block.value == self.value:
|
||||||
self._merge(collided_block)
|
self._merge(collided_block)
|
||||||
else:
|
else:
|
||||||
logger.debug(f"Block({id(self)}) collided with another, stopped at {self.pos()}")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
self.rect.topleft = new_x, new_y
|
self.rect.topleft = new_x, new_y
|
||||||
logger.debug(f"Moving block({id(self)}): {self.pos()} => ({grid_pos(new_x)}, {grid_pos(new_y)})")
|
|
||||||
|
|
||||||
def _calc_new_pos(self, direction: Direction) -> tuple[int, int]:
|
def _calc_new_pos(self, direction: Direction) -> tuple[int, int]:
|
||||||
"""Calculate the new position of the block."""
|
"""Calculate the new position of the block."""
|
||||||
@ -69,14 +64,13 @@ class Block(pygame.sprite.Sprite):
|
|||||||
def _get_collided_block(self, x: int, y: int) -> Union["Block", None]:
|
def _get_collided_block(self, x: int, y: int) -> Union["Block", None]:
|
||||||
"""Get the block that collides with the given block."""
|
"""Get the block that collides with the given block."""
|
||||||
|
|
||||||
logger.error(list(block for block in self.groups()[0] if block != self and block.rect.collidepoint(x, y)))
|
|
||||||
return next((block for block in self.groups()[0] if block != self and block.rect.collidepoint(x, y)), None)
|
return next((block for block in self.groups()[0] if block != self and block.rect.collidepoint(x, y)), None)
|
||||||
|
|
||||||
def _merge(self, other: "Block") -> None:
|
def _merge(self, other: "Block") -> None:
|
||||||
"""Merge the block with another block."""
|
"""Merge the block with another block."""
|
||||||
self.value += other.value
|
self.value += other.value
|
||||||
self.update()
|
self.update()
|
||||||
logger.debug(f"Merging block({id(self)}) with block({id(other)}")
|
logger.debug(f"Merging block({id(self)}) with block({id(other)})")
|
||||||
self.groups()[0].remove(other)
|
self.groups()[0].remove(other)
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
|
|||||||
@ -57,19 +57,15 @@ class Game:
|
|||||||
self.exit()
|
self.exit()
|
||||||
|
|
||||||
def move_up(self) -> None:
|
def move_up(self) -> None:
|
||||||
logger.debug("Move up")
|
|
||||||
self.blocks.move(Direction.UP)
|
self.blocks.move(Direction.UP)
|
||||||
|
|
||||||
def move_down(self) -> None:
|
def move_down(self) -> None:
|
||||||
logger.debug("Move down")
|
|
||||||
self.blocks.move(Direction.DOWN)
|
self.blocks.move(Direction.DOWN)
|
||||||
|
|
||||||
def move_left(self) -> None:
|
def move_left(self) -> None:
|
||||||
logger.debug("Move left")
|
|
||||||
self.blocks.move(Direction.LEFT)
|
self.blocks.move(Direction.LEFT)
|
||||||
|
|
||||||
def move_right(self) -> None:
|
def move_right(self) -> None:
|
||||||
logger.debug("Move right")
|
|
||||||
self.blocks.move(Direction.RIGHT)
|
self.blocks.move(Direction.RIGHT)
|
||||||
|
|
||||||
def exit(self) -> None:
|
def exit(self) -> None:
|
||||||
|
|||||||
@ -18,6 +18,7 @@ class Grid(pygame.sprite.Group):
|
|||||||
blocks.sort(key=lambda block: block.rect.x, reverse=True)
|
blocks.sort(key=lambda block: block.rect.x, reverse=True)
|
||||||
|
|
||||||
for block in blocks:
|
for block in blocks:
|
||||||
|
block: Block
|
||||||
block.move(direction)
|
block.move(direction)
|
||||||
|
|
||||||
self.generate_block()
|
self.generate_block()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user