mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
style(game): display readable block coords
style(game): update log output
This commit is contained in:
parent
848db04af2
commit
482843b078
@ -7,6 +7,11 @@ from .colors import COLORS
|
|||||||
from .config import Config
|
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):
|
class Block(pygame.sprite.Sprite):
|
||||||
def __init__(self, x: int, y: int, value: int | None = 2):
|
def __init__(self, x: int, y: int, value: int | None = 2):
|
||||||
"""Initialize a block"""
|
"""Initialize a block"""
|
||||||
@ -14,6 +19,9 @@ class Block(pygame.sprite.Sprite):
|
|||||||
self.image = pygame.Surface((Config.BLOCK_SIZE, Config.BLOCK_SIZE))
|
self.image = pygame.Surface((Config.BLOCK_SIZE, Config.BLOCK_SIZE))
|
||||||
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()
|
||||||
@ -29,11 +37,9 @@ class Block(pygame.sprite.Sprite):
|
|||||||
new_x = self.rect.x + dx
|
new_x = self.rect.x + dx
|
||||||
new_y = self.rect.y + dy
|
new_y = self.rect.y + dy
|
||||||
if 0 <= new_x <= Config.WIDTH - Config.BLOCK_SIZE and 0 <= new_y <= Config.HEIGHT - Config.BLOCK_SIZE:
|
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.x = new_x
|
||||||
self.rect.y = new_y
|
self.rect.y = new_y
|
||||||
else:
|
|
||||||
logger.debug("Move blocked: out of bounds")
|
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Update the block"""
|
"""Update the block"""
|
||||||
@ -59,17 +65,19 @@ class Block(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
def __add__(self, other: "Block") -> None:
|
def __add__(self, other: "Block") -> None:
|
||||||
"""Add the value of two blocks and update the current block"""
|
"""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.value += other.value
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def __iadd__(self, other: "Block") -> None:
|
def __iadd__(self, other: "Block") -> None:
|
||||||
"""Add the value of two blocks and updae the current block"""
|
"""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.value += other.value
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
"""Return a string representation of the block"""
|
"""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:
|
def __str__(self) -> str:
|
||||||
"""Return a string representation of the block"""
|
"""Return a string representation of the block"""
|
||||||
|
|||||||
@ -78,18 +78,23 @@ class Game:
|
|||||||
def _move_blocks(self, dx: int, dy: int) -> None:
|
def _move_blocks(self, dx: int, dy: int) -> None:
|
||||||
"""Move all the blocks by `dx` and `dy`."""
|
"""Move all the blocks by `dx` and `dy`."""
|
||||||
moved_blocks = pygame.sprite.Group() # Keep track of moved blocks to avoid double merging
|
moved_blocks = pygame.sprite.Group() # Keep track of moved blocks to avoid double merging
|
||||||
|
blocks_to_remove = []
|
||||||
|
|
||||||
for block in self.sprites:
|
for block in self.sprites:
|
||||||
block.move(dx, dy)
|
block.move(dx, dy)
|
||||||
|
|
||||||
for block in self.sprites: # FIX: different value block merge
|
for block in self.sprites:
|
||||||
collidin_blocks = pygame.sprite.spritecollide(block, self.sprites, False)
|
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:
|
if block != other_block and block.value == other_block.value and other_block not in moved_blocks:
|
||||||
block.increase_value()
|
new_block = block + other_block
|
||||||
self.sprites.remove(other_block)
|
moved_blocks.add(new_block)
|
||||||
moved_blocks.add(block)
|
blocks_to_remove.extend([block, other_block])
|
||||||
|
|
||||||
|
for block in blocks_to_remove:
|
||||||
|
self.sprites.remove(block)
|
||||||
|
|
||||||
self._generate_random_block()
|
self._generate_random_block()
|
||||||
|
|
||||||
def _generate_random_block(self, count: int = 1) -> None:
|
def _generate_random_block(self, count: int = 1) -> None:
|
||||||
@ -104,7 +109,6 @@ class Game:
|
|||||||
|
|
||||||
if not colliding_blocks:
|
if not colliding_blocks:
|
||||||
self.sprites.add(block)
|
self.sprites.add(block)
|
||||||
logger.debug(f"Generated block({id(block)}) at ({x}, {y})")
|
|
||||||
break
|
break
|
||||||
|
|
||||||
def exit(self) -> None:
|
def exit(self) -> None:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user