-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfalling_code.py
More file actions
68 lines (56 loc) · 2.06 KB
/
falling_code.py
File metadata and controls
68 lines (56 loc) · 2.06 KB
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
import os
import sys
from sys import stdout
import random
from time import sleep
from string import ascii_letters, digits, punctuation
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-he', '--height', type=int, default=18, help='height of falling string')
parser.add_argument('-s', '--speed', type=float, default=.035, help='time between lines in seconds')
args = parser.parse_args()
# Does not work for linux
#os.system('cls')
STRING = ascii_letters + digits + punctuation
class Ansi:
green = '32'
reset = '0'
bright = '1'
dim = '2'
normal = '22'
lightgreen = '92'
def __init__(self):
for name in dir(self):
if not name.startswith('_'):
setattr(self, name, f'\033[{getattr(self, name)}m')
def falling_code(length=20, speed=.045):
style = Ansi()
cols = os.get_terminal_size().columns
line = [0 for x in range(cols)]
while True:
cols = os.get_terminal_size().columns
if len(line) != cols:
os.system('cls')
line = [0 for _ in range(cols)]
new = random.choice([x for x in range(len(line)) if line[x] == 0])
line[new] += 1
for i in line:
stdout.write(style.green)
if i == length:
stdout.write(style.reset + random.choice(STRING))
elif i == 0:
stdout.write(' ')
elif 1 <= i <= length//4:
stdout.write(style.dim + random.choice(STRING))
elif length//4 < i <= round((length/4)*2):
stdout.write(style.normal + random.choice(STRING))
elif round((length/4)*2) < i < round((length/4)*3):
stdout.write(style.bright + random.choice(STRING))
else:
stdout.write(style.reset + style.lightgreen + style.bright + random.choice(STRING))
stdout.write(style.reset)
line = [x+1 if x < length and x != 0 else 0 for x in line]
sys.stdout.flush()
sleep(speed)
if __name__ == '__main__':
falling_code(length=args.height, speed=args.speed)