mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): add block movement down
This commit is contained in:
parent
e31195d486
commit
ae31baf925
@ -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
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user