-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgametime.py
87 lines (48 loc) · 2.54 KB
/
gametime.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class GameClock(object):
"""Manages time in a game."""
def __init__(self, game_ticks_per_second=100):
"""Create a Game Clock object.
game_ticks_per_second -- The number of 'AI' frames a second.
"""
self.game_ticks_per_second = float(game_ticks_per_second)
self.game_tick = 1. / self.game_ticks_per_second
self.speed = 1.
self.started = False
def start(self):
"""Starts the Game Clock. Must be called once."""
self.time = 0.
self.virtual_time = 0.
self.game_time = 0.
self.game_frame_count = 0
self.real_time_passed = 0.
self.real_time = self.get_real_time()
self.started = True
def get_real_time(self):
"""Returns the real time, as reported by the system clock.
This function should be implemented in a derived class!"""
raise NotImplementedError, "'get_real_time' must be supplied by the derived class."
def update(self):
"""Advances time, must be called once per frame. Returns a list of game frame times."""
assert self.started, "You must call 'start' before using a GameClock."
real_time_now = self.get_real_time()
self.real_time_passed = real_time_now - self.real_time
self.real_time = real_time_now
self.time += self.real_time_passed
self.virtual_time+= self.real_time_passed * self.speed
while self.game_time + self.game_tick < self.virtual_time:
yield (self.game_frame_count, self.game_time)
self.game_frame_count += 1
self.game_time = self.game_frame_count * self.game_tick
self.between_frame = ( self.virtual_time - self.game_time ) / self.game_tick
if __name__ == "__main__":
import time
class TestGameClock(GameClock):
def get_real_time(self):
return time.clock()
t = TestGameClock(20)
t.start()
while t.virtual_time < 2.:
for (frame_count, game_time) in t.update():
print "Game frame #%i, %2.2f" % (frame_count, game_time)
print "\t%2.2f%% between game frame"%(t.between_frame*100.)
time.sleep(.01)