Repository navigation
Expand file tree
/
Copy pathGameClock.py
More file actions
56 lines (45 loc) · 1.64 KB
/
Copy pathGameClock.py
File metadata and controls
56 lines (45 loc) · 1.64 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from pygame.time import Clock
from Observable import Observable, EventType
class GameClock (Observable):
"""Keeps track of how fast the game clock runs"""
# Fires when game time advances
# :param dt: Amount of game time that elapsed
@EventType
def REAL_TIME_ADVANCE(dt):
"""Game time has advanced
:param dt: Amount of game time that elapsed, in "seconds"
"""
@EventType
def GAME_TIME_ADVANCE(dt):
"""Real time has passed
:param dt: Amount of real time that elapsed, in milliseconds
"""
def __init__(self, framerate, multiplier=1.0):
super(GameClock, self).__init__()
self.clock = Clock()
self._multiplier = multiplier
self.framerate = framerate
self.isPaused = False
@property
def multiplier(self):
"""Get the real/game time ratio"""
if self.isPaused:
return 0
else:
return self._multiplier
@multiplier.setter
def multiplier(self, multiplier):
"""Get the real/game time ratio"""
self._multiplier = multiplier
def toggle(self):
self.isPaused ^= True
def update(self):
# pygame.time.Clock::tick() returns milliseconds
real_dt = self.clock.tick(self.framerate)
#self.notify(GameClock.REAL_TIME_ADVANCE, real_dt)
game_dt = real_dt * self.multiplier / 1000.0
self.notify(GameClock.GAME_TIME_ADVANCE, game_dt)
def zero(self):
"""Drain any time off the internal clock without calling any updaters"""
real_dt = self.clock.tick(self.framerate)
#self.notify(GameClock.REAL_TIME_ADVANCE, real_dt)