matrix.py

Created by fime

Created on April 27, 2021

2.64 KB

Bug report or review here

Matrix rain script on numworks, object-oriented. Press the power button to switch between the color themes.

  • MatrixRain() is the main class.
  • The Theme() class contain all the theme values, like the colors and the characters to display.

Feel free to ask me question about it on my Discord : Fime#9701


from random import randint, choice
from kandinsky import fill_rect, draw_string
from ion import keydown as key
SCREEN_W=320
SCREEN_H=222
CHAR_SIZE=[10,15]

class MatrixRain():
  def __init__(self):
    
    self.size_w=SCREEN_W//CHAR_SIZE[0]
    self.size_h=SCREEN_H//CHAR_SIZE[1]
    
    self.theme=Theme()
    self.matrix=[]
    self.fillMatrix()
    
    self.drawMatrix()
    self.animation()
    
  def animation(self):
    while True:
      if key(8):
        self.theme.nextTheme()
        self.drawMatrix()
      self.fallColumn(randint(0,self.size_w-1))
    
  def fillMatrix(self):
    for x in range(self.size_w):
      self.matrix.append([])
      for y in range(self.size_h):
        self.matrix[x].append(self.randomChar())
        
  def randomChar(self):
    if randint(0,4)==1:return choice(self.theme.chars)
    else:return " "
  
  def drawMatrix(self):
    fill_rect(0,0,SCREEN_W,SCREEN_H,self.colorTheme("bg"))
    for x in range(self.size_w):
      self.drawColumn(x)
      
  def colorTheme(self,col):
    return self.theme.color[col]
  
  def drawColumn(self,column):
    for y in range(self.size_h):
         if self.matrix[column][max(y-1,0)]!=" ":
           if self.matrix[column][min(y+1,self.size_h-1)]==" ":color="glow1"
           else:color="glow2"
         else:
           color="char"
         draw_string(self.matrix[column][y],column*CHAR_SIZE[0],y*CHAR_SIZE[1],self.colorTheme(color),self.colorTheme("bg"))
  
  def shuffleColumn(self,column):
    for y in range(self.size_h):
      if self.matrix[column][y]!=" ":
        self.matrix[column][y]=choice(self.theme.chars)
  
  def fallColumn(self,column):
    self.matrix[column].insert(0,self.randomChar())
    del self.matrix[column][-1]
    self.shuffleColumn(column)
    self.drawColumn(column)
  
class Theme():
  theme_names=["classic","red","fime"]
  themes={
  "classic":{"char":(50,200,10),"bg":(10,20,5),"glow1":(220,255,200),"glow2":(200,255,100)},
  "red":{"char":(200,10,50),"bg":(10,20,50),"glow1":(255,0,0),"glow2":(200,0,0)},
  "fime":{"char":(255,200,10),"bg":(20,10,5),"glow1":(255,255,100),"glow2":(255,220,100)}}
  
  chars={
  "bin":"01",
  "letters":"abcdefghijklmnopqrstuvwxyzΩ0123456789",
  "omega":"A→Ω"}
  
  def __init__(self):
    self.setTheme("classic")
    self.setChars("letters")
    
  def nextTheme(self):
    next=Theme.theme_names[0] if Theme.theme_names.index(self.current)+1==len(Theme.theme_names) else Theme.theme_names[Theme.theme_names.index(self.current)+1]
    self.setTheme(next)
  
  def setChars(self,name):
    self.chars=Theme.chars[name]
  
  def setTheme(self,name):
    self.current=name
    self.color=Theme.themes[name]
    
rain=MatrixRain()