feat: add sprite collision check

This commit is contained in:
Kristofers Solo 2023-12-23 22:10:53 +02:00
parent 104fed4705
commit 72b31e90c0

View File

@ -1,9 +1,9 @@
import random
import sys
import pygame
from .block import Block
from .colors import COLORS
from .config import Config
@ -14,8 +14,7 @@ class Game:
self.screen = pygame.display.set_mode((Config.WIDTH, Config.HEIGHT))
pygame.display.set_caption("2048")
self.sprites = pygame.sprite.Group()
block = Block(0, 0)
self.sprites.add(block)
self.generate_random_block()
def run(self) -> None:
while True:
@ -36,3 +35,16 @@ class Game:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def generate_random_block(self) -> None:
for _ in range(Config.INITIAL_BLOCK_COUNT):
while True:
x = random.randint(0, 2) * Config.BLOCK_SIZE # random column position
y = random.randint(0, 2) * Config.BLOCK_SIZE # random row position
block = Block(x, y)
colligin_blocks = pygame.sprite.spritecollide(block, self.sprites, False)
if not colligin_blocks:
self.sprites.add(block)
break