feat(game): generate new figure

This commit is contained in:
Kristofers Solo 2024-01-04 03:20:52 +02:00
parent 5a12de0c88
commit a211dd0254
2 changed files with 12 additions and 3 deletions

View File

@ -15,7 +15,7 @@ class Game:
self._create_grid_surface()
self.sprites = pygame.sprite.Group()
self.tetromino = Tetromino(group=self.sprites)
self.tetromino = Tetromino(self.sprites, self.create_new_tetromino)
self.timers = Timers(
Timer(CONFIG.game.initial_speed, True, self.move_down),
@ -62,6 +62,9 @@ class Game:
def move_right(self) -> None:
self.tetromino.move_horizontal(Direction.RIGHT)
def create_new_tetromino(self) -> None:
self.tetromino = Tetromino(self.sprites, self.create_new_tetromino)
def _create_grid_surface(self) -> None:
self.grid_surface = self.surface.copy()
self.grid_surface.fill("#00ff00")

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Callable, Optional
import pygame
from utils import CONFIG, Direction, Figure, FigureConfig, Size
@ -8,11 +8,15 @@ from .block import Block
class Tetromino:
def __init__(
self, group: pygame.sprite.Group, shape: Optional[Figure] = None
self,
group: pygame.sprite.Group,
func: Callable[[None], None],
shape: Optional[Figure] = None,
) -> None:
self.figure: FigureConfig = shape.value if shape else Figure.random().value
self.block_positions: list[pygame.Vector2] = self.figure.shape
self.color: str = self.figure.color
self.create_new = func
self.blocks = [
Block(group=group, pos=pos, color=self.color)
@ -23,6 +27,8 @@ class Tetromino:
if not self._check_horizontal_collision(self.blocks, Direction.DOWN):
for block in self.blocks:
block.pos.y += 1
else:
self.create_new()
def move_horizontal(self, direction: Direction) -> None:
if not self._check_vertical_collision(self.blocks, direction):