Advertisement
here2share

# Tk_MATRIX_Digital_Rain.py

Apr 2nd, 2022 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. # Tk_MATRIX_Digital_Rain.py
  2.  
  3. import tkinter
  4. import time
  5. import random
  6.    
  7. katakana = '''あ い う え お が ぎ ぐ げ ご ざ じ ず ぜ ぞ だ ぢ づ で ど な に ぬ ね の は ぷ
  8. ぺ ま み む め も や ゆ よ ら り る れ わ ゐ ゑ を ん ア イ ウ オ カ ガ キ グ ゲ コ サ シ ス ゼ
  9. ソ タ チ テ ト ナ ニ ヌ ネ ノ ハ ヒ フ ペ ホ マ ミ ム メ モ ヤ ユ ラ リ ル レ ワ ヰ ヱ ヲ ン ヴ'''.split(' ')
  10.  
  11. chars = katakana + list('23457'*2)
  12.  
  13. ww = 800
  14. hh = 600
  15.  
  16. root=tkinter.Tk()
  17. root.title("Tk_MATRIX_Digital_Rain")
  18. root.geometry(str(ww)+"x"+str(hh)+"+-10+0")
  19. canvas=tkinter.Canvas(root, width=ww, height=hh, bg='#000000')
  20. canvas.pack()
  21.  
  22. HEIGHTOFTEXT=18
  23.  
  24. def rgb2hex(r,g,b):
  25.     return '#%02X%02X%02X'%(r,g,b)
  26.  
  27. def find_speed():
  28.     return random.uniform(HEIGHTOFTEXT*3.0, HEIGHTOFTEXT*0.3)
  29.  
  30. white = rgb2hex(255,255,255)
  31. DIMMER = [white]+[rgb2hex(0,g,max(0,160-g*15)) for g in range(10,255,10)][::-1]
  32. Ldim = len(DIMMER)
  33.  
  34. across = [x for x in range(15, ww, 15)]
  35. random.shuffle(across)
  36.  
  37. x = {}
  38. y = {}
  39. speed = {}
  40. streak = {}
  41.  
  42. while 1:
  43.     canvas.delete('all')
  44.     if across:
  45.         L = across.pop(0)
  46.         x[L] = L
  47.         y[L] = 0
  48.         speed[L] = find_speed()
  49.         streak[L] = [random.choice(chars) for dim in DIMMER]
  50.        
  51.     rain = list(x)
  52.     for L in rain:
  53.         x2,y0 = L,int(y[L]/HEIGHTOFTEXT*HEIGHTOFTEXT)
  54.         for index in range(Ldim):
  55.             if not random.randint(0,4):
  56.                 streak[L][index] = random.choice(chars)
  57.             t = streak[L][index]
  58.             y2 = int(y0-(index*HEIGHTOFTEXT))
  59.             canvas.create_text(x2, y2, text=t, font='bold', fill=DIMMER[index])
  60.         streak[L].insert(0, random.choice(chars))
  61.         streak[L].pop()
  62.         y[L] += speed[L]
  63.         if y[L]-HEIGHTOFTEXT*50 > hh:
  64.             y[L] = 0
  65.             speed[L] = find_speed()
  66.                
  67.     '''
  68.     wait=time.time()+0.8 ### moderate speed
  69.     while time.time() < wait:
  70.         canvas.update()
  71.     '''
  72.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement