-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
33 lines (28 loc) · 877 Bytes
/
timer.py
File metadata and controls
33 lines (28 loc) · 877 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
import pyray as pr
class Timer:
def __init__(self, duration:int,function:callable = None, auto_start:bool = True, repeat:bool = False):
self.duration = duration
self.time = 0
self.function = function
self.repeat = repeat
self.started = False
if auto_start:
self.start()
def update(self):
if not self.started:
return
self.time += 1
if self._is_done():
if self.function:
self.function()
if self.repeat:
self.reset()
else:
self.time = self.duration # Keep time at duration to avoid exceeding it
def _is_done(self):
return self.time >= self.duration
def start(self):
self.started = True
self.time = 0
def reset(self):
self.time = 0