This repository has been archived by the owner on Jul 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.py
222 lines (193 loc) · 7.19 KB
/
matrix.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import time
import curses
import random
from collections import namedtuple
import sys
import os
import winsound
PYTHON2 = sys.version_info.major < 3
########################################################################
# TUNABLES
t_end = time.time() + 50
DROPPING_CHARS = 50
MIN_SPEED = 1
MAX_SPEED = 10
RANDOM_CLEANUP = 50
WINDOW_CHANCE = 50
WINDOW_SIZE = 25
WINDOW_ANIMATION_SPEED = 10
FPS = 25
SLEEP_MILLIS = 1.0/FPS
USE_COLORS = True
SCREENSAVER_MODE = False
MATRIX_CODE_CHARS = "ɀɁɂŧϢϣϤϥϦϧϨϫϬϭϮϯϰϱϢϣϤϥϦϧϨϩϪϫϬϭϮϯϰ߃߄༣༤༥༦༧༩༪༫༬༭༮༯༰༱༲༳༶"
########################################################################
# CODE
COLOR_CHAR_NORMAL = 1
COLOR_CHAR_HIGHLIGHT = 2
COLOR_WINDOW = 3
class FallingChar(object):
matrixchr = list(MATRIX_CODE_CHARS)
normal_attr = curses.A_NORMAL
highlight_attr = curses.A_REVERSE
def __init__(self, width, MIN_SPEED, MAX_SPEED):
self.x = 0
self.y = 0
self.speed = 1
self.char = ' '
self.reset(width, MIN_SPEED, MAX_SPEED)
def reset(self, width, MIN_SPEED, MAX_SPEED):
self.char = random.choice(FallingChar.matrixchr).encode("utf-8")
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(FallingChar.matrixchr).encode("utf-8")
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():
winsound.PlaySound('Sounds/mtrx_audio.wav', winsound.SND_ASYNC | winsound.SND_LOOP)
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_WINDOW, curses.COLOR_GREEN, curses.COLOR_GREEN)
height, width = scr.getmaxyx()
window_animation = None
lines = []
for i in range(DROPPING_CHARS):
l = FallingChar(width, MIN_SPEED, MAX_SPEED)
l.y = randint(0, height-2)
lines.append(l)
scr.refresh()
while time.time() < t_end:
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(SLEEP_MILLIS)
if SCREENSAVER_MODE:
key_pressed = scr.getch() != -1
if key_pressed:
raise KeyboardInterrupt()
steps += 1
winsound.PlaySound(None, winsound.SND_PURGE)
curses.curs_set(1)