fix(game): block generation

This commit is contained in:
Kristofers Solo 2023-12-31 20:01:13 +02:00
parent df1a5b7069
commit df7015a7d4

View File

@ -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)