Matrix con Python

Dato il tema ispirato al mio film preferito “MATRIX” condivido questo piccolo script Python trovato anni fa per trasformare il vostro desktop nella “inquietante” cascata di caratteri alfanumerici di Matrix.


[code language=”python”]
import os, time, random, sys

class message(str):
def __new__(cls, text, speed):
self = super(message, cls).__new__(cls, text)
self.speed = speed
self.y = -1*len(text)
self.x = random.randint(0, display().width)
self.skip = 0
return self

def move(self):
if self.speed > self.skip:
self.skip += 1
else:
self.skip = 0
self.y += 1

class display(list):
def __init__(self):
self.height, self.width = [int(x) for x in os.popen(‘stty size’, ‘r’).read().split()]
self[:] = [‘ ‘ for y in xrange(self.height) for x in xrange(self.width)]

def set_vertical(self, x, y, string):
string = string[::-1]
if x < 0: x = 80 + x if x >= self.width:
x = self.width-1
if y < 0: string = string[abs(y):] y = 0 if y + len(string) > self.height:
string = string[0:self.height – y]
if y >= self.height:
return
start = y*self.width+x
length = self.width*(y+len(string))
step = self.width

self[start:length:step] = string

def __str__(self):
return ”.join(self)

i_message = raw_input("Input a message: ")
messages = [message(i_message, random.randint(1, 5))]
for t in xrange(1000):
messages.append(message(i_message, random.randint(1, 5)))
d = display()
for text in messages:
d.set_vertical(text.x, text.y, text)
text.move()
sys.stdout.write(str(d))
sys.stdout.flush()
del d
time.sleep(0.1)
[/code]

Sia che usiate Winzoz, oppure Mac o Linux, quello che dovete fare è semplicemente aprire una finestra terminale (cmd.exe per utenti Winzoz) allargarla a schermo intero e quindi lanciare il comando:

[code]python ./matrix.py[/code]

Al prompt inserite il testo che volete e …
…Buon divertimento.

AD MAIORA.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *