forked from GADify/AristaMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AristaMatrix.py
executable file
·271 lines (239 loc) · 9.03 KB
/
AristaMatrix.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Matrix-Curses
# See how deep the rabbit hole goes.
# Copyright (c) 2012 Tom Wallroth
#
# Sources on github:
# http://github.com/devsnd/matrix-curses/
#
# licensed under GNU GPL version 3 (or later)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#
from __future__ import unicode_literals
import argparse
import locale
import time
import curses
import random
import sys
PYTHON2 = sys.version_info.major < 3
locale.setlocale(locale.LC_ALL, '')
encoding = locale.getpreferredencoding()
########################################################################
# TUNABLES
DROPPING_CHARS = 50
MIN_SPEED = 1
MAX_SPEED = 8
RANDOM_CLEANUP = 100
#WINDOW_CHANCE = 50
WINDOW_CHANCE = 1
WINDOW_SIZE = 25
WINDOW_ANIMATION_SPEED = 3
FPS = 55
#USE_COLORS = False
USE_COLORS = True
SCREENSAVER_MODE = True
# Character set
#MATRIX_CODE_CHARS = "ɀɁɂŧϢϣϤϥϦϧϨϫϬϭϮϯϰϱϢϣϤϥϦϧϨϩϪϫϬϭϮϯϰ߃߄༣༤༥༦༧༩༪༫༬༭༮༯༰༱༲༳༶"
getchars = lambda start, end: [chr(i) for i in range(start, end)]
CODE_CHARS_BINARY = (0, 1)
CODE_CHARS_NUMBERS = getchars(0x30, 0x3A)
CODE_CHARS_LATIN = getchars(0x41, 0x5B) + getchars(0x61, 0x7B)
CODE_CHARS_GREEK = getchars(0x370, 0x3FF)
CODE_CHARS_CYRILLIC = getchars(0x400, 0x50)
ALL_CODE_CHARS = CODE_CHARS_NUMBERS + CODE_CHARS_LATIN + CODE_CHARS_GREEK + CODE_CHARS_CYRILLIC
########################################################################
# CODE
COLOR_CHAR_NORMAL = 1
COLOR_CHAR_HIGHLIGHT = 2
COLOR_WINDOW = 3
class FallingChar(object):
normal_attr = curses.A_NORMAL
highlight_attr = curses.A_REVERSE
def __init__(self, width, MIN_SPEED, MAX_SPEED, matrixchr):
self.x = 0
self.y = 0
self.speed = 1
self.char = ' '
self.matrixchr = matrixchr
self.reset(width, MIN_SPEED, MAX_SPEED)
def reset(self, width, MIN_SPEED, MAX_SPEED):
self.char = random.choice(self.matrixchr).encode(encoding)
self.x = randint(1, width - 1)
self.y = 0
self.speed = randint(MIN_SPEED, MAX_SPEED)
# offset makes sure that chars with same speed don't move all in same frame
self.offset = randint(0, self.speed)
def tick(self, scr, steps):
height, width = scr.getmaxyx()
if self.advances(steps):
# if window was resized and char is out of bounds, reset
self.out_of_bounds_reset(width, height)
# make previous char curses.A_NORMAL
if USE_COLORS:
scr.addstr(self.y, self.x, self.char, curses.color_pair(COLOR_CHAR_NORMAL))
else:
scr.addstr(self.y, self.x, self.char, curses.A_NORMAL)
# choose new char and draw it A_REVERSE if not out of bounds
self.char = random.choice(self.matrixchr).encode(encoding)
self.y += 1
if not self.out_of_bounds_reset(width, height):
if USE_COLORS:
scr.addstr(self.y, self.x, self.char, curses.color_pair(COLOR_CHAR_HIGHLIGHT))
else:
scr.addstr(self.y, self.x, self.char, curses.A_REVERSE)
def out_of_bounds_reset(self, width, height):
if self.x > width-2:
self.reset(width, MIN_SPEED, MAX_SPEED)
return True
if self.y > height-2:
self.reset(width, MIN_SPEED, MAX_SPEED)
return True
return False
def advances(self, steps):
if steps % (self.speed + self.offset) == 0:
return True
return False
def step(self, steps, scr):
return -1, -1, None
class WindowAnimation(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.step = 0
def tick(self, scr, steps):
if self.step > WINDOW_SIZE:
#stop window animation after some steps
self.draw_frame(scr, self.x - self.step, self.y - self.step,
self.x + self.step, self.y + self.step,
curses.A_NORMAL)
return False
# clear all characters covered by the window frame
for i in range(WINDOW_ANIMATION_SPEED):
anistep = self.step + i
self.draw_frame(scr, self.x - anistep, self.y - anistep,
self.x + anistep, self.y + anistep,
curses.A_NORMAL, ' ')
#cancel last animation
self.draw_frame(scr, self.x - self.step, self.y - self.step,
self.x + self.step, self.y + self.step,
curses.A_NORMAL)
#next step
self.step += WINDOW_ANIMATION_SPEED
#draw outer frame
self.draw_frame(scr, self.x - self.step, self.y - self.step,
self.x + self.step, self.y + self.step,
curses.A_REVERSE)
return True
def draw_frame(self, scr, x1, y1, x2, y2, attrs, clear_char=None):
if USE_COLORS:
if attrs == curses.A_REVERSE:
attrs = curses.color_pair(COLOR_WINDOW)
h, w = scr.getmaxyx()
for y in (y1, y2):
for x in range(x1, x2+1):
if x < 0 or x > w-1 or y < 0 or y > h-2:
continue
if clear_char is None:
scr.chgat(y, x, 1, attrs)
else:
scr.addstr(y, x, clear_char, attrs)
for x in (x1, x2):
for y in range(y1, y2+1):
if x < 0 or x > w-1 or y < 0 or y > h-2:
continue
if clear_char is None:
scr.chgat(y, x, 1, attrs)
else:
scr.addstr(y, x, clear_char, attrs)
# we don't need a good PRNG, just something that looks a bit random.
def rand():
# ~ 2 x as fast as random.randint
a = 9328475634
while True:
a ^= (a << 21) & 0xffffffffffffffff;
a ^= (a >> 35);
a ^= (a << 4) & 0xffffffffffffffff;
yield a
r = rand()
def randint(_min, _max):
if PYTHON2:
n = r.next()
else:
n = r.__next__()
return (n % (_max - _min)) + _min
def main(params):
steps = 0
scr = curses.initscr()
scr.nodelay(1)
curses.curs_set(0)
curses.noecho()
if USE_COLORS:
curses.start_color()
curses.use_default_colors()
curses.init_pair(COLOR_CHAR_NORMAL, curses.COLOR_GREEN, curses.COLOR_BLACK)
#curses.init_pair(COLOR_CHAR_HIGHLIGHT, curses.COLOR_WHITE, curses.COLOR_GREEN)
curses.init_pair(COLOR_CHAR_HIGHLIGHT, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(COLOR_WINDOW, curses.COLOR_GREEN, curses.COLOR_GREEN)
height, width = scr.getmaxyx()
window_animation = None
lines = []
for i in range(params.dropping_chars):
l = FallingChar(width, MIN_SPEED, MAX_SPEED, params.matrix_chars)
l.y = randint(0, height-2)
lines.append(l)
scr.refresh()
while True:
height, width = scr.getmaxyx()
for line in lines:
line.tick(scr, steps)
for i in range(RANDOM_CLEANUP):
x = randint(0, width-1)
y = randint(0, height-1)
scr.addstr(y, x, ' ')
if randint(0, WINDOW_CHANCE) == 1:
if window_animation is None:
#start window animation
line = random.choice(lines)
window_animation = WindowAnimation(line.x, line.y)
if not window_animation is None:
still_active = window_animation.tick(scr, steps)
if not still_active:
window_animation = None
scr.refresh()
time.sleep(1.0 / params.fps)
if SCREENSAVER_MODE:
key_pressed = scr.getch() != -1
if key_pressed:
raise KeyboardInterrupt()
steps += 1
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-c', '--dropping-chars', type=int, default=DROPPING_CHARS, help="Number of simultaneous dropping chars")
parser.add_argument(
'-f', '--fps', type=int, default=FPS, help="Frames per second used in the animation")
parser.add_argument(
'-m', '--matrix-chars', type=str, default=ALL_CODE_CHARS, help="Set of characters used in the animation")
args = parser.parse_args()
try:
main(args)
except KeyboardInterrupt:
curses.endwin()
curses.curs_set(1)
curses.reset_shell_mode()
curses.echo()