-
Notifications
You must be signed in to change notification settings - Fork 1
/
space144.py
120 lines (102 loc) · 4.04 KB
/
space144.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
import sys,time,random,math
SHIP = [
[ 0x00, 0x0a, 0x74, 0x88, 0xb5, 0xc8, 0xc3, 0xcd, 0xe7, 0xf7, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xcb, 0x34, 0x00, 0x00, 0x00, 0x00, 0x19, 0x60, 0x82, 0x7c, 0x7c, 0x82, 0x60, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00 ],
[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x7a, 0xd2, 0xff, 0xff, 0xcc, 0x34, 0x00, 0x00, 0x00, 0x00, 0x26, 0x98, 0xde, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xde, 0x98, 0x26, 0x00, 0x00, 0x00 ],
[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x7a, 0xd2, 0xff, 0xff, 0xc9, 0x2c, 0x04, 0x51, 0x9d, 0xe8, 0xff, 0xf7, 0xf0, 0xf1, 0xf1, 0xf0, 0xf7, 0xff, 0xe8, 0x9d, 0x57, 0x15, 0x00 ],
[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x7e, 0xde, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xf3, 0xf3, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xf7, 0xeb, 0xdb, 0x99, 0x27, 0x00 ],
[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x7e, 0xde, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xf3, 0xf3, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xf7, 0xeb, 0xdb, 0x99, 0x27, 0x00 ],
[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x7a, 0xd2, 0xff, 0xff, 0xc9, 0x2c, 0x04, 0x51, 0x9d, 0xe8, 0xff, 0xf7, 0xf0, 0xf1, 0xf1, 0xf0, 0xf7, 0xff, 0xe8, 0x9d, 0x57, 0x15, 0x00 ],
[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x7a, 0xd2, 0xff, 0xff, 0xcc, 0x34, 0x00, 0x00, 0x00, 0x00, 0x26, 0x98, 0xde, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xde, 0x98, 0x26, 0x00, 0x00, 0x00 ],
[ 0x00, 0x00, 0x74, 0x88, 0xb5, 0xc8, 0xc3, 0xcd, 0xe7, 0xf7, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xcb, 0x34, 0x00, 0x00, 0x00, 0x00, 0x19, 0x60, 0x82, 0x7c, 0x7c, 0x82, 0x60, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00 ],
]
class SpaceAni(object):
def move_ship(self):
self.ship_x += self.dx/16
if self.ship_x > self.w + 400:
self.ship_x = -random.randint(30, 80)
self.new_dx = 1.
def paint_ship(self, x):
ship_x = x - .75
grid_x = int(math.floor(ship_x))+1
diff = grid_x-ship_x
off = min(1.999999, max(0, 2*diff)) # on 2x grid
ratio = off-int(math.floor(off)) # on 2x grid
off = int(math.floor(off)) # on 2x grid
for y in range(8):
base = self.w*y
for dx in range(16):
x = grid_x+dx
if 0 <= x < self.w:
arr_x = off+2*dx
c0 = SHIP[y][arr_x]
c1 = SHIP[y][arr_x+1]
r0, r1 = 1-ratio, ratio
if c0 != 0 or c1 != 0:
if c0 == 0:
c0 = self.frame[base+x]
elif c1 == 0:
c1 = self.frame[base+x]
self.frame[base+x] = int(r0*c0 + r1*c1)
def add_star(self):
self.stars.append( [ self.w, random.randint(0,self.h-1), random.randint(15, 80)/10. ] )
self.next_star = random.randint(3, 64)
def move_stars(self):
for i in range(len(self.stars)-1, -1, -1):
x, y, v = self.stars[i]
v *= self.dx
x -= v*self.dt
if x + v < 0:
self.stars[i:i+1] = []
else:
self.stars[i][0] = x
self.next_star -= self.dx
if self.next_star <= 0:
self.add_star()
def paint_stars(self):
for x, y, v in self.stars:
v += self.dx
x = int(x)
v = int(v)
base = self.w*y
l = int(v*v/8)+1
for i in range(l):
ix = x+i
if 0 <= ix < self.w:
self.frame[base+ix] += int(255-255*i/l)
def change_dx(self):
self.next_dx_change -= 1
self.dx = self.dx*.97 + self.new_dx*.03
if self.next_dx_change <= 0:
if -8 < self.ship_x < self.w:
self.new_dx += random.randint(4, 8)/3.
self.next_dx_change = random.randint(300, 1000)
def clear(self):
for i in range(len(self.frame)):
self.frame[i]=0
def __init__(self, w, h):
self.w, self.h = w, h
self.frame = [0]*(w*h)
self.stars = []
self.add_star()
self.ship_x = -30
self.dx = 0.
self.new_dx = 1.
self.next_dx_change = 1
self.dt = 1/16.
def next_frame(self, i, t):
self.clear()
self.change_dx()
self.move_stars()
self.paint_stars()
self.move_ship()
self.paint_ship(self.ship_x)
return [ min(255, x) for x in self.frame ]
ani = SpaceAni(432, 8)
i=0
t0 = time.time()
while True:
i+=1
i%=432
sys.stdout.buffer.write(bytes( ((x//2)&0x7f) for x in ani.next_frame(i, time.time()-t0))+b"\x80")
sys.stdout.flush()
time.sleep(.005)