mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): increase down speed on key press
This commit is contained in:
parent
3b4865b8a1
commit
3962ad4e14
@ -30,8 +30,11 @@ class Game:
|
||||
self.field,
|
||||
)
|
||||
|
||||
self.initial_block_speed = CONFIG.game.initial_speed
|
||||
self.increased_block_speed = self.initial_block_speed * 0.3
|
||||
self.down_pressed = False
|
||||
self.timers = Timers(
|
||||
Timer(CONFIG.game.initial_speed, True, self.move_down),
|
||||
Timer(self.initial_block_speed, True, self.move_down),
|
||||
Timer(CONFIG.game.movment_delay),
|
||||
Timer(CONFIG.game.rotation_delay),
|
||||
)
|
||||
@ -57,27 +60,38 @@ class Game:
|
||||
def handle_event(self) -> None:
|
||||
keys = pygame.key.get_pressed()
|
||||
|
||||
if not self.timers.horizontal.active:
|
||||
if keys[pygame.K_LEFT] or keys[pygame.K_a] or keys[pygame.K_h]:
|
||||
self.move_left()
|
||||
self.timers.horizontal.activate()
|
||||
elif keys[pygame.K_DOWN] or keys[pygame.K_s] or keys[pygame.K_j]:
|
||||
self.move_down()
|
||||
elif keys[pygame.K_RIGHT] or keys[pygame.K_d] or keys[pygame.K_l]:
|
||||
self.move_right()
|
||||
self.timers.horizontal.activate()
|
||||
|
||||
if not self.timers.rotation.active:
|
||||
if (
|
||||
left_keys = keys[pygame.K_LEFT] or keys[pygame.K_a] or keys[pygame.K_h]
|
||||
right_keys = keys[pygame.K_RIGHT] or keys[pygame.K_d] or keys[pygame.K_l]
|
||||
down_keys = keys[pygame.K_DOWN] or keys[pygame.K_s] or keys[pygame.K_j]
|
||||
rotate_keys = (
|
||||
keys[pygame.K_SPACE]
|
||||
or keys[pygame.K_r]
|
||||
or keys[pygame.K_UP]
|
||||
or keys[pygame.K_w]
|
||||
or keys[pygame.K_k]
|
||||
):
|
||||
)
|
||||
|
||||
if not self.timers.horizontal.active:
|
||||
if left_keys:
|
||||
self.move_left()
|
||||
self.timers.horizontal.activate()
|
||||
elif right_keys:
|
||||
self.move_right()
|
||||
self.timers.horizontal.activate()
|
||||
|
||||
if not self.timers.rotation.active:
|
||||
if rotate_keys:
|
||||
self.tetromino.rotate()
|
||||
self.timers.rotation.activate()
|
||||
|
||||
if not self.down_pressed and down_keys:
|
||||
self.down_pressed = True
|
||||
self.timers.vertical.duration = self.increased_block_speed
|
||||
|
||||
if self.down_pressed and not down_keys:
|
||||
self.down_pressed = False
|
||||
self.timers.vertical.duration = self.initial_block_speed
|
||||
|
||||
def move_down(self) -> None:
|
||||
self.tetromino.move_down()
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ from attrs import define, field
|
||||
|
||||
@define
|
||||
class Timer:
|
||||
duration: int = field(converter=int)
|
||||
duration: float = field(converter=float)
|
||||
repeated: bool = field(default=False)
|
||||
func: Optional[Callable[[], None]] = field(default=None)
|
||||
start_time: int = 0
|
||||
|
||||
Loading…
Reference in New Issue
Block a user