-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameTimer.py
36 lines (28 loc) · 1000 Bytes
/
GameTimer.py
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
34
35
36
# based on comment S.G. Harmonia on https://stackoverflow.com/posts/26090325/revisions
import time
from threading import Timer
class RenewableTimer():
def __init__(self, timeout, callback):
self.start_time = time.time()
self.timeout = timeout
self.callback = callback
self.timer = Timer(self.timeout, self.callback)
self.cancel_time = time.time()
def cancel(self):
self.timer.cancel()
self.timer.join()
def start(self):
self.start_time = time.time()
self.timer.start()
def pause(self):
self.cancel_time = time.time()
self.timer.cancel()
self.timer.join()
return self.get_actual_time()
def resume(self):
self.timeout = self.get_actual_time()
self.timer = Timer(self.timeout, self.callback)
self.start_time = time.time()
self.timer.start()
def get_actual_time(self):
return self.timeout - (self.cancel_time - self.start_time)