From 5f37bfea353ed01bd4302e4edce3d48f4a94f59b Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Fri, 29 Dec 2023 23:56:57 +0200 Subject: [PATCH] fix(game): movement Now all blocks don't leave gaps when moving --- src/py2048/grid.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/py2048/grid.py b/src/py2048/grid.py index ac981fa..c7b9afa 100644 --- a/src/py2048/grid.py +++ b/src/py2048/grid.py @@ -9,8 +9,15 @@ from .utils import Direction class Grid(pygame.sprite.Group): def move(self, direction: Direction): - block: Block - for block in self: + blocks = list(self.sprites()) + + match direction: + case Direction.DOWN: + blocks.sort(key=lambda block: block.rect.y, reverse=True) + case Direction.RIGHT: + blocks.sort(key=lambda block: block.rect.x, reverse=True) + + for block in blocks: block.move(direction) def generate_random_block(self, count: int = 1) -> None: