forked from bpython/curtsies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime.py
65 lines (54 loc) · 2.13 KB
/
realtime.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
import itertools
import random
import time
from curtsies import FullscreenWindow, Input, FSArray
from curtsies.fmtfuncs import red, bold, green, on_blue, yellow, on_red
MAX_FPS = 1000
time_per_frame = 1. / MAX_FPS
class FrameCounter(object):
def __init__(self):
self.render_times = []
self.dt = .5
def frame(self):
self.render_times.append(time.time())
def fps(self):
now = time.time()
while self.render_times and self.render_times[0] < now - self.dt:
self.render_times.pop(0)
return len(self.render_times) / max(self.dt, now - self.render_times[0] if self.render_times else self.dt)
def main():
counter = FrameCounter()
with FullscreenWindow() as window:
print('Press escape to exit')
with Input() as input_generator:
a = FSArray(window.height, window.width)
c = None
for framenum in itertools.count(0):
t0 = time.time()
while True:
t = time.time()
temp_c = input_generator.send(max(0, t - (t0 + time_per_frame)))
if temp_c is not None:
c = temp_c
if c is None:
pass
elif c == '<ESC>':
return
elif c == '<SPACE>':
a = FSArray(window.height, window.width)
else:
row = random.choice(range(window.height))
column = random.choice(range(window.width-len(c)))
a[row:row+1, column:column+len(c)] = [c]
c = None
if time_per_frame < t - t0:
break
row = random.choice(range(window.height))
column = random.choice(range(window.width))
a[row:row+1, column:column+1] = [random.choice(".,-'`~")]
fps = 'FPS: %.1f' % counter.fps()
a[0:1, 0:len(fps)] = [fps]
window.render_to_terminal(a)
counter.frame()
if __name__ == '__main__':
main()