feat(game): add drop timer

This commit is contained in:
Kristofers Solo 2024-01-06 16:58:34 +02:00
parent a56f457482
commit e77dce308b
3 changed files with 10 additions and 3 deletions

View File

@ -285,6 +285,7 @@ class Tetris(BaseScreen):
Timer(self.initial_block_speed, True, self.move_down), Timer(self.initial_block_speed, True, self.move_down),
Timer(CONFIG.game.movment_delay), Timer(CONFIG.game.movment_delay),
Timer(CONFIG.game.rotation_delay), Timer(CONFIG.game.rotation_delay),
Timer(CONFIG.game.drop_delay),
) )
self.timers.vertical.activate() self.timers.vertical.activate()
@ -293,6 +294,7 @@ class Tetris(BaseScreen):
self.initial_block_speed = CONFIG.game.initial_speed self.initial_block_speed = CONFIG.game.initial_speed
self.increased_block_speed = self.initial_block_speed * 0.4 self.increased_block_speed = self.initial_block_speed * 0.4
self.down_pressed = False self.down_pressed = False
self.drop_pressed = False
self.level: int = 1 self.level: int = 1
self.score: int = 0 self.score: int = 0
self.lines: int = 0 self.lines: int = 0
@ -351,6 +353,7 @@ class Tetris(BaseScreen):
counter_clockwise_keys = ( counter_clockwise_keys = (
keys[pygame.K_e] or keys[pygame.K_i] or keys[pygame.K_LEFT] keys[pygame.K_e] or keys[pygame.K_i] or keys[pygame.K_LEFT]
) )
if not self.timers.rotation.active: if not self.timers.rotation.active:
if clockwise_keys: if clockwise_keys:
self.rotate() self.rotate()
@ -375,8 +378,9 @@ class Tetris(BaseScreen):
"""Handle the drop key [K_SPACE].""" """Handle the drop key [K_SPACE]."""
drop_keys = keys[pygame.K_SPACE] drop_keys = keys[pygame.K_SPACE]
if drop_keys: if not self.timers.drop.active and drop_keys:
self.drop() self.drop()
self.timers.drop.activate()
def _reset_game_state(self) -> None: def _reset_game_state(self) -> None:
"""Reset the game state.""" """Reset the game state."""

View File

@ -63,8 +63,10 @@ class Timers(NamedTuple):
vertical: Timer for vertical movement. vertical: Timer for vertical movement.
horizontal: Timer for horizontal movement. horizontal: Timer for horizontal movement.
rotation: Timer for rotation. rotation: Timer for rotation.
drop: Timer for dropping.
""" """
vertical: Timer vertical: Timer
horizontal: Timer horizontal: Timer
rotation: Timer rotation: Timer
drop: Timer

View File

@ -21,9 +21,10 @@ class Game:
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, -1) offset: Vec2 = Vec2(columns // 2, -1)
initial_speed: float | int = 200 initial_speed: float | int = 300
movment_delay: int = 200 movment_delay: int = 100
rotation_delay: int = 200 rotation_delay: int = 200
drop_delay: int = 200
score: dict[int, int] = {1: 40, 2: 100, 3: 300, 4: 1200} score: dict[int, int] = {1: 40, 2: 100, 3: 300, 4: 1200}