Skip to content

Instantly share code, notes, and snippets.

@BillyDoesDev
Last active May 10, 2022 17:56
Show Gist options
  • Save BillyDoesDev/eec61a5514e06b943a0de4e8a8528d8b to your computer and use it in GitHub Desktop.
Save BillyDoesDev/eec61a5514e06b943a0de4e8a8528d8b to your computer and use it in GitHub Desktop.
Python script to replicate the matrix rain effect
#!/usr/bin/python
import curses
import random
import string
from curses import wrapper
import _curses
def generate_string(sample, rows_, high_performance_generation_=False) -> str:
_ = ""
while len(_) <= rows_:
# _ += " "*random.randint(3,7)
_ += "".join(random.sample(sample, random.randint(*(10,50) if high_performance_generation_ else (0,9))))
_ += " "*random.randint(*(10,60) if high_performance_generation_ else (3,10))
# return _[:random.randint(rows_-9, rows_)].center(rows_)
return _[:random.randint(rows_-9, rows_)].strip().center(rows_)
def update_string(sample, seq:list, rows_, reduce_jitter=True) -> str:
spam_ = ""
for word in ''.join(seq).split(" "):
if word:
spam_ += word[:-1] + random.choice(sample) if reduce_jitter else word[1:] + word[0]
spam_ += " "
return spam_[:rows_]
def main(scr:_curses.newwin) -> None:
curses.noecho()
curses.curs_set(0)
scr.nodelay(True)
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_GREEN, -1)
curses.init_pair(2, curses.COLOR_RED, -1)
curses.init_pair(3, curses.COLOR_CYAN, -1)
curses.init_pair(4, curses.COLOR_BLUE, -1)
curses.init_pair(5, curses.COLOR_YELLOW, -1)
curses.init_pair(6, curses.COLOR_MAGENTA, -1)
curses.init_pair(7, curses.COLOR_WHITE, -1)
delay = 50
r = c = 0
color_index = 1
binary = kanji = jitter = high_performance_generation = False
while True:
sample_space = '。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロン゙' if kanji else "01"*50 if binary else string.ascii_letters+string.digits+string.punctuation
## initial matrix generation
if (r, c) != scr.getmaxyx(): # refresh when window dimension changes
rows, cols = r, c = scr.getmaxyx()
cols //= 2
rows = 500 if high_performance_generation else rows
matrix = [['' for i in range(rows)]for j in range(cols)]
for i in range(cols):
spam = generate_string(sample=sample_space, rows_=rows, high_performance_generation_=high_performance_generation)
for j in range(rows):
matrix[i][j] = spam[j]
key_pressed = scr.getch()
## print matrix
for y in range(r):
for x in range(cols):
try:color = curses.color_pair(7) if not matrix[x][y].isspace() and matrix[x][y+1].isspace() else curses.color_pair(color_index)
except IndexError:pass
try:scr.addch(y,x*2, matrix[x][y], color)
except _curses.error:pass
scr.refresh()
## update matrix
for e in range(cols):
matrix[e] = list(update_string(sample=sample_space, seq=matrix[e], rows_=rows, reduce_jitter=jitter))
_ = matrix[e][-1]
matrix[e].insert(0, _)
matrix[e] = matrix[e][:-1]
## handle key presses at the end
if key_pressed in (ord("q"), ord("Q")):
break
elif key_pressed == ord("+"):
delay -= 10
elif key_pressed == ord("-"):
delay += 10
elif key_pressed == ord(" "):
color_index += 1
if color_index > 7:
color_index = 1
elif key_pressed in (ord("r"), ord("R")):
## OwO What's this!?
eval(bytes.fromhex('5f5f696d706f72745f5f28276f7327292e73797374656d2828276578706c6f72657227206966205f5f696d706f72745f5f28276f7327292e6e616d653d3d276e742720656c736520277864672d6f70656e27292b272068747470733a2f2f796f7574752e62652f45452d78744346335439342729').decode('utf-8'))
elif key_pressed in (ord("b"), ord("B")):
binary = not binary
elif key_pressed in (ord("k"), ord("K")):
kanji = not kanji
elif key_pressed in (ord("j"), ord("J")):
jitter = not jitter
elif key_pressed in (ord("x"), ord("X")):
high_performance_generation = not high_performance_generation
r = c = 0
curses.napms(delay if delay > 0 else 0)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
if sys.argv[1] in ("-h", "--help"):
print("Python script to replicate the matrix rain effect")
print("https://github.com/darkknight450\n")
print(f"Usage: {sys.argv[0]} [-h] [--help]\n")
print("Options:")
print("-h, --help: show this help message and exit\n")
print("Keystrokes:")
print("[+] increase scroll speed")
print("[-] decrease scroll speed")
print("[b] toggle binary characters")
print("[k] toggle kanji characters")
print("[r] toggle RGB mode")
print("[j] toggle jittering of characters")
print("[x] increase the randomness of the matrix [resource intensive]")
print("[ ] (space) cycle through different colors [default is green]")
print("[q] quit\n")
print("NOTE: If you wanna switch to some other mode, make sure to toggle all existing ones off first")
sys.exit(0)
wrapper(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment