mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): add Timer
This commit is contained in:
parent
5291ca5de5
commit
e31195d486
32
src/game/timer.py
Normal file
32
src/game/timer.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from typing import Callable, Optional
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
from attrs import define, field
|
||||||
|
|
||||||
|
|
||||||
|
@define
|
||||||
|
class Timer:
|
||||||
|
duration: int = field(converter=int)
|
||||||
|
repeated: bool = field(default=False)
|
||||||
|
func: Optional[Callable[[None], None]] = field(default=None)
|
||||||
|
start_time: int = 0
|
||||||
|
active: bool = False
|
||||||
|
|
||||||
|
def activate(self) -> None:
|
||||||
|
self.active = True
|
||||||
|
self.start_time = pygame.time.get_ticks()
|
||||||
|
|
||||||
|
def deactivate(self) -> None:
|
||||||
|
self.active = False
|
||||||
|
self.start_time = 0
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
|
current_time = pygame.time.get_ticks()
|
||||||
|
if current_time - self.start_time >= self.duration and self.active:
|
||||||
|
if self.func and self.start_time:
|
||||||
|
self.func()
|
||||||
|
|
||||||
|
self.deactivate()
|
||||||
|
|
||||||
|
if self.repeated:
|
||||||
|
self.activate()
|
||||||
Loading…
Reference in New Issue
Block a user