-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgame_timer.py
More file actions
34 lines (27 loc) · 980 Bytes
/
game_timer.py
File metadata and controls
34 lines (27 loc) · 980 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
34
# -*- coding: utf-8 -*-
#
# @author SEMOOC
import math
import time
class GameTimer(object):
"""
Timer of the game.
Attributes:
interval: timer interval, default 1.0 second.
trigger: a function to call when timer triggered.
is_running: a flag to show running status, to be used in asynchronous system.
"""
def __init__(self, trigger, interval=1.0):
self.interval = interval
self.trigger = trigger
self.is_running = False
def start(self):
"""Start the timer. It triggers only when current time is around integer times of self.interval."""
self.is_running = True
while self.is_running:
time.sleep(math.ceil(time.time() / self.interval) * self.interval - time.time())
if self.is_running:
self.trigger()
def stop(self):
"""Stop the timer before next triggering. It works in asynchronous system."""
self.is_running = False