Last active
August 29, 2015 14:15
-
-
Save jonatasleon/6c9de9603fae9da3c75f to your computer and use it in GitHub Desktop.
Terminal effect in the matrix movie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
#-*- encoding:utf-8 -*- | |
from os import system, popen | |
from time import sleep | |
from random import choice, randint | |
from string import ascii_letters | |
BLANK = ' ' | |
TIME = 0.09 # TIME must be lower than 0.1 for a cool effect | |
def getTerminalSize(): | |
l,c = popen('stty size', 'r').read().split() | |
c = int(c) // 2 | |
return int(l), c | |
def getRandomChar(): | |
char = choice(ascii_letters) | |
return char | |
def randomizeRow(c): | |
colist = [] | |
for i in range(c): | |
value = getRandomChar() | |
if ord(value) % randint(2, 3) == 0: | |
colist.append(BLANK) | |
else: | |
colist.append(value) | |
return colist | |
def generateMatrix(rows, columns): | |
rowsList = [] | |
for i in range(rows): | |
columnsList = [] | |
for j in range(columns): | |
columnsList.append(BLANK) | |
rowsList.append(columnsList) | |
return rowsList | |
def editMatrix(matrix, index, c): | |
if index+1 < len(matrix): | |
editMatrix(matrix, index+1, c) | |
if index == 0: | |
matrix[index] = randomizeRow(c) | |
else: | |
matrix[index] = matrix[index-1] | |
def showMatrix(matrix): | |
for i in range(len(matrix)): | |
print(BLANK.join(matrix[i])) | |
def main(l, c): | |
matrix = generateMatrix(l,c) | |
print('\033[92m') | |
try: | |
while(True): | |
showMatrix(matrix) | |
editMatrix(matrix, 0, c) | |
sleep(TIME) | |
system('clear') | |
except (KeyboardInterrupt): | |
system('clear') | |
print('\033[0m') | |
if __name__ == '__main__': | |
l, c = getTerminalSize() | |
main(l, c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment