mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
fix(game): block generation
This commit is contained in:
parent
df1a5b7069
commit
df7015a7d4
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user