From df7015a7d420be69dc41534b0944bb82aca5a6e2 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Sun, 31 Dec 2023 20:01:13 +0200 Subject: [PATCH] fix(game): block generation --- src/py2048/grid.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/py2048/grid.py b/src/py2048/grid.py index d47260b..021503e 100644 --- a/src/py2048/grid.py +++ b/src/py2048/grid.py @@ -20,20 +20,21 @@ class Grid(pygame.sprite.Group): for block in blocks: block.move(direction) + self.generate_block() + def generate_block(self, amount: int = 1, *pos: tuple[int, int]) -> None: """Generate `amount` number of blocks.""" if pos: for coords in pos: self.add(Block(coords[0] * Config.BLOCK_SIZE, coords[1] * Config.BLOCK_SIZE)) return - for _ in range(amount): while True: x = random.randint(0, 3) * Config.BLOCK_SIZE # random column position y = random.randint(0, 3) * Config.BLOCK_SIZE # random row position block = Block(x, y) - colliding_blocks = self.spritecollide(block, self, False) # check collision + colliding_blocks = pygame.sprite.spritecollide(block, self, False) # check collision if not colliding_blocks: self.add(block)