-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathanime.py
executable file
·144 lines (107 loc) · 3.03 KB
/
anime.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
import curses
import time
import math
stdscr = curses.initscr()
# Allow color
curses.start_color()
# Disable echoing of input keys
curses.noecho()
# Make getch() not block
stdscr.nodelay(1)
# React to keys without Enter key needed
curses.cbreak()
# Allow special keys
stdscr.keypad(1)
# Disable cursor
# curses.curs_set(0)
# Create a color pair
curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLACK)
class Smootherizer(object):
def common(x):
return (x * x * (3.0 - 2.0 * x))
def slow(x):
return x * x;
def strange(x):
return x * x;
def sin(x):
return math.sin(x * math.pi / 2)
def linear(x):
return x
common = staticmethod(common)
slow = staticmethod(slow)
strange = staticmethod(strange)
sin = staticmethod(sin)
linear = staticmethod(linear)
class Anime:
def __init__(self, from_x, to_x):
self.from_x = from_x
self.to_x = to_x
self.x = 0.0
self.length_x = to_x - from_x
self.direction = 1
self.y = 10
self.prev_x = 0
self.pprev_x = 0
self.interpolation = Smootherizer.common
self.primary_char = "-"
self.secondary_char = "."
self.height = 1
def smooth_x(self):
x = self.x
v = x / self.length_x
step = self.interpolation(v)
return int((self.from_x * step) + (self.to_x * (1 - step)))
def draw(self):
vertex_a = self.smooth_x()
vertex_b = self.y
for h in range(0, self.height):
stdscr.addstr(vertex_b + h, vertex_a, self.primary_char, curses.A_BOLD)
if (vertex_a != self.prev_x):
for h in range(0, self.height):
stdscr.addstr(vertex_b + h, self.prev_x, self.secondary_char);
if (self.prev_x != self.pprev_x):
for h in range(0, self.height):
stdscr.addstr(vertex_b + h, self.pprev_x, " ");
self.pprev_x = self.prev_x
self.prev_x = vertex_a
def update(self):
self.draw()
stdscr.refresh()
self.x = self.x + self.direction
if self.x >= self.length_x:
self.direction = -1
if self.x <= 0:
self.direction = 1
def update_status(interpolation):
stdscr.addstr(0, 0, "Interpolation: " + interpolation + " ",
curses.color_pair(1) | curses.A_BOLD)
stdscr.addstr(1, 0, "(c)ommon (s)low s(t)range si(n) (l)inear (q)uit",
curses.color_pair(1) | curses.A_BOLD)
stdscr.refresh()
anime = Anime(10, 70)
c = ""
update_status("common")
while c != ord('q'):
c = stdscr.getch()
interpolation = "common"
if c != -1:
if c == ord("s"):
anime.interpolation = Smootherizer.slow
interpolation = "slow"
elif c == ord("t"):
anime.interpolation = Smootherizer.strange
interpolation = "strange"
elif c == ord("c"):
anime.interpolation = Smootherizer.common
interpolation = "common"
elif c == ord("n"):
anime.interpolation = Smootherizer.sin
interpolation = "sin"
elif c == ord("l"):
anime.interpolation = Smootherizer.linear
interpolation = "linear"
update_status(interpolation)
anime.update()
time.sleep(0.02)
curses.endwin()