-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
37 lines (31 loc) · 871 Bytes
/
timer.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
37
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# vim:ts=4:sw=4:expandtab
import pygame
from pygame.locals import *
def update(time_passed):
for t in Timer.timers:
t.update(time_passed)
class Timer:
timers = []
def __init__(self, interval, oneshot, cb):
print "initiating timer"
self.time = 0
self.interval = interval
self.oneshot = oneshot
self.cb = cb
Timer.timers.append(self)
def remaining(self):
rem = (self.interval - self.time) / 1000
if rem >= 0:
return rem
return 0
def update(self, time_passed):
self.time += time_passed
if self.time > self.interval:
print "timer should trigger"
self.cb()
if self.oneshot:
Timer.timers.remove(self)
else:
self.time = 0