feat(game): add block movement down

This commit is contained in:
Kristofers Solo 2024-01-04 02:40:19 +02:00
parent e31195d486
commit ae31baf925
4 changed files with 37 additions and 7 deletions

View File

@ -11,6 +11,7 @@ class Block(pygame.sprite.Sprite):
self.image.fill(color) self.image.fill(color)
self.pos = pygame.Vector2(pos) + CONFIG.game.offset self.pos = pygame.Vector2(pos) + CONFIG.game.offset
x = self.pos.x * CONFIG.game.cell.width self.rect = self.image.get_rect(topleft=self.pos * CONFIG.game.cell.width)
y = self.pos.y * CONFIG.game.cell.height
self.rect = self.image.get_rect(topleft=(x, y)) def update(self) -> None:
self.rect.topleft = self.pos * CONFIG.game.cell.width

View File

@ -1,7 +1,9 @@
import pygame import pygame
from utils import CONFIG, Figure from utils import CONFIG, Figure
from .log import log
from .tetromino import Tetromino from .tetromino import Tetromino
from .timer import Timer
class Game: class Game:
@ -9,22 +11,36 @@ class Game:
self.surface = pygame.Surface(CONFIG.game.size) self.surface = pygame.Surface(CONFIG.game.size)
self.dispaly_surface = pygame.display.get_surface() self.dispaly_surface = pygame.display.get_surface()
self.rect = self.surface.get_rect(topleft=CONFIG.game.pos) self.rect = self.surface.get_rect(topleft=CONFIG.game.pos)
self.surface.fill(CONFIG.colors.bg_float)
self._create_grid_surface() self._create_grid_surface()
self.sprites = pygame.sprite.Group() 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: def run(self) -> None:
self.dispaly_surface.blit(self.surface, CONFIG.game.pos) self.dispaly_surface.blit(self.surface, CONFIG.game.pos)
self.draw() self.draw()
self._timer_update()
def draw(self) -> None: def draw(self) -> None:
self.surface.fill(CONFIG.colors.bg_float)
self.update()
self.sprites.draw(self.surface) self.sprites.draw(self.surface)
self._draw_border() self._draw_border()
self._draw_grid() 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: def _create_grid_surface(self) -> None:
self.grid_surface = self.surface.copy() self.grid_surface = self.surface.copy()
self.grid_surface.fill("#00ff00") self.grid_surface.fill("#00ff00")
@ -61,3 +77,7 @@ class Game:
CONFIG.game.line_width * 2, CONFIG.game.line_width * 2,
CONFIG.game.border_radius, CONFIG.game.border_radius,
) )
def _timer_update(self) -> None:
for timer in self.timers.values():
timer.update()

View File

@ -7,7 +7,9 @@ from .block import Block
class Tetromino: 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.figure: FigureConfig = shape.value if shape else Figure.random().value
self.block_positions: list[pygame.Vector2] = self.figure.shape self.block_positions: list[pygame.Vector2] = self.figure.shape
self.color: str = self.figure.color self.color: str = self.figure.color
@ -16,3 +18,7 @@ class Tetromino:
Block(group=group, pos=pos, color=self.color) Block(group=group, pos=pos, color=self.color)
for pos in self.block_positions for pos in self.block_positions
] ]
def move_down(self) -> None:
for block in self.blocks:
block.pos.y += 1

View File

@ -17,7 +17,10 @@ class Game:
cell: Size = Size(40, 40) cell: Size = Size(40, 40)
size: Size = Size(columns * cell.width, rows * cell.width) size: Size = Size(columns * cell.width, rows * cell.width)
pos: Vec2 = Vec2(padding, padding) 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 @define