-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathfps.py
42 lines (35 loc) · 1.04 KB
/
fps.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
import time
from curtsies import FullscreenWindow, Input, FSArray
from curtsies.fmtfuncs import red, bold, green, on_blue, yellow, on_red
import curtsies.events
class Frame(curtsies.events.ScheduledEvent):
pass
class World:
def __init__(self):
self.s = 'Hello'
def tick(self):
self.s += '|'
self.s = self.s[max(1, len(self.s)-80):]
def process_event(self, e):
self.s += str(e)
def realtime(fps=15):
world = World()
dt = 1/fps
reactor = Input()
schedule_next_frame = reactor.scheduled_event_trigger(Frame)
schedule_next_frame(when=time.time())
with reactor:
for e in reactor:
if isinstance(e, Frame):
world.tick()
print(world.s)
when = e.when + dt
while when < time.time():
when += dt
schedule_next_frame(when)
elif e == '<ESC>':
break
else:
world.process_event(e)
if __name__ == "__main__":
realtime()