From ae31baf925866150f3391db551d68a384e9bb523 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 4 Jan 2024 02:40:19 +0200 Subject: [PATCH] feat(game): add block movement down --- src/game/block.py | 7 ++++--- src/game/game.py | 24 ++++++++++++++++++++++-- src/game/tetromino.py | 8 +++++++- src/utils/config.py | 5 ++++- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/game/block.py b/src/game/block.py index c48f84a..01aee4b 100644 --- a/src/game/block.py +++ b/src/game/block.py @@ -11,6 +11,7 @@ class Block(pygame.sprite.Sprite): self.image.fill(color) self.pos = pygame.Vector2(pos) + CONFIG.game.offset - x = self.pos.x * CONFIG.game.cell.width - y = self.pos.y * CONFIG.game.cell.height - self.rect = self.image.get_rect(topleft=(x, y)) + self.rect = self.image.get_rect(topleft=self.pos * CONFIG.game.cell.width) + + def update(self) -> None: + self.rect.topleft = self.pos * CONFIG.game.cell.width diff --git a/src/game/game.py b/src/game/game.py index b82c7f5..5f56d02 100644 --- a/src/game/game.py +++ b/src/game/game.py @@ -1,7 +1,9 @@ import pygame from utils import CONFIG, Figure +from .log import log from .tetromino import Tetromino +from .timer import Timer class Game: @@ -9,22 +11,36 @@ class Game: self.surface = pygame.Surface(CONFIG.game.size) self.dispaly_surface = pygame.display.get_surface() self.rect = self.surface.get_rect(topleft=CONFIG.game.pos) - self.surface.fill(CONFIG.colors.bg_float) self._create_grid_surface() self.sprites = pygame.sprite.Group() - self.tetromino = Tetromino(None, group=self.sprites) + self.tetromino = Tetromino(group=self.sprites) + + self.timers = { + "vertical move": Timer(CONFIG.game.initial_speed, True, self.move_down), + } + + self.timers["vertical move"].activate() def run(self) -> None: self.dispaly_surface.blit(self.surface, CONFIG.game.pos) self.draw() + self._timer_update() def draw(self) -> None: + self.surface.fill(CONFIG.colors.bg_float) + self.update() self.sprites.draw(self.surface) self._draw_border() self._draw_grid() + def update(self) -> None: + self.sprites.update() + + def move_down(self) -> None: + self.tetromino.move_down() + def _create_grid_surface(self) -> None: self.grid_surface = self.surface.copy() self.grid_surface.fill("#00ff00") @@ -61,3 +77,7 @@ class Game: CONFIG.game.line_width * 2, CONFIG.game.border_radius, ) + + def _timer_update(self) -> None: + for timer in self.timers.values(): + timer.update() diff --git a/src/game/tetromino.py b/src/game/tetromino.py index acd0871..6d93a81 100644 --- a/src/game/tetromino.py +++ b/src/game/tetromino.py @@ -7,7 +7,9 @@ from .block import Block class Tetromino: - def __init__(self, shape: Optional[Figure], /, group: pygame.sprite.Group) -> None: + def __init__( + self, group: pygame.sprite.Group, 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 @@ -16,3 +18,7 @@ class Tetromino: Block(group=group, pos=pos, color=self.color) for pos in self.block_positions ] + + def move_down(self) -> None: + for block in self.blocks: + block.pos.y += 1 diff --git a/src/utils/config.py b/src/utils/config.py index 91528e7..a7a2175 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -17,7 +17,10 @@ class Game: cell: Size = Size(40, 40) size: Size = Size(columns * cell.width, rows * cell.width) pos: Vec2 = Vec2(padding, padding) - offset: Vec2 = Vec2(columns // 2, 5) + offset: Vec2 = Vec2(columns // 2, -1) + initial_speed: int = 200 + movment_delay: int = 200 + rotation_delay: int = 200 @define