fix(game): movement sorting

This commit is contained in:
Kristofers Solo 2024-01-01 00:15:24 +02:00
parent 54787e727b
commit abdecc36d2
2 changed files with 11 additions and 4 deletions

View File

@ -3,7 +3,6 @@ import random
from typing import Union
import pygame
from loguru import logger
from .colors import COLORS
from .config import Config
@ -75,7 +74,6 @@ class Block(pygame.sprite.Sprite):
self.group.remove(self)
self.value += other.value
self.update()
logger.debug(f"Merging block({id(self)}) with block({id(other)})")
self.group.add(self)
def update(self) -> None:

View File

@ -1,6 +1,7 @@
import random
import pygame
from loguru import logger
from .block import Block
from .config import Config
@ -12,8 +13,15 @@ class Board(pygame.sprite.Group):
blocks = self.sprites()
block: Block
if direction in {Direction.DOWN, Direction.RIGHT}:
blocks.sort(key=lambda block: (block.rect.x, block.rect.y), reverse=True)
match direction:
case Direction.UP:
blocks.sort(key=lambda block: block.rect.y)
case Direction.DOWN:
blocks.sort(key=lambda block: block.rect.y, reverse=True)
case Direction.LEFT:
blocks.sort(key=lambda block: block.rect.x)
case Direction.RIGHT:
blocks.sort(key=lambda block: block.rect.x, reverse=True)
for block in blocks:
block.move(direction)
@ -39,4 +47,5 @@ class Board(pygame.sprite.Group):
if not colliding_blocks:
self.add(block)
logger.debug(f"Created block at {block.pos()}")
break