mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
fix(game): block movement
This commit is contained in:
parent
fd48aa5b1b
commit
cad0e87663
@ -20,7 +20,7 @@ class Block(pygame.sprite.Sprite):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
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}")
|
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`."""
|
"""Move the block by `dx` and `dy`."""
|
||||||
dx, dy = direction.value
|
dx, dy = direction.value
|
||||||
|
|
||||||
|
while True:
|
||||||
new_x = self.rect.x + dx * Config.BLOCK_SIZE
|
new_x = self.rect.x + dx * Config.BLOCK_SIZE
|
||||||
new_y = self.rect.y + dy * 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)})")
|
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:
|
def update(self) -> None:
|
||||||
"""Update the block"""
|
"""Update the block"""
|
||||||
|
|||||||
@ -9,6 +9,7 @@ from .utils import Direction
|
|||||||
|
|
||||||
class Grid(pygame.sprite.Group):
|
class Grid(pygame.sprite.Group):
|
||||||
def move(self, direction: Direction):
|
def move(self, direction: Direction):
|
||||||
|
block: Block
|
||||||
for block in self:
|
for block in self:
|
||||||
block.move(direction)
|
block.move(direction)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user