-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.py
More file actions
33 lines (25 loc) · 676 Bytes
/
timer.py
File metadata and controls
33 lines (25 loc) · 676 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from pygame.time import get_ticks
class Timer:
def __init__(self, duration, repeated = False, func = None):
self.repeated = repeated
self.func = func
self.duration = duration
self.start_time = 0
self.active = False
def activate(self):
self.active = True
self.start_time = get_ticks()
def deactivate(self):
self.active = False
self.start_time = 0
def update(self):
current_time = get_ticks()
if current_time - self.start_time >= self.duration and self.active:
# call a function
if self.func and self.start_time != 0:
self.func()
# reset timer
self.deactivate()
# repeat the timer
if self.repeated:
self.activate()