use Python Realization *** Digital rain in Empire

writing | a highlander

source :Python technology 「ID: pythonall」

 use Python Realization *** Digital rain in Empire

Speaking of *** The empire , I believe that even if you haven't seen a series of films, you should have heard the name , The latest in the series is 2003 ) , It's been a long time 10 Several years. , If you've seen the film , I should be impressed by the digital rain inside .

It doesn't matter if you haven't seen the movie and don't know what digital rain is like , I'll show you a picture .

 use Python Realization *** Digital rain in Empire

It's like the picture above , In this article we use Python To achieve this effect , Of course, this is not limited to numbers , It can also be letters 、 Graphics, etc .

Digital rain

The implementation of the code is relatively simple , It's basically using pygame Library creation window , Then define the generation of numbers and let them continuously display on the window , The code implementation is as follows :

import random, pygame
FONT_PX = 15
pygame.init()
winSur = pygame.display.set_mode((500, 600))
font = pygame.font.SysFont('fangsong', 20)
bg_suface = pygame.Surface((500, 600), flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 13))
winSur.fill((0, 0, 0))
# Numbers
texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)]
colums = int(500 / FONT_PX)
drops = [0 for i in range(colums)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.time.delay(33)
winSur.blit(bg_suface, (0, 0))
for i in range(len(drops)):
text = random.choice(texts)
winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
drops[i] += 1
if drops[i] * 10 > 600 or random.random() > 0.95:
drops[i] = 0
pygame.display.flip()

Let's look at the implementation effect :

 use Python Realization *** Digital rain in Empire

Is there something inside .

The letter rain

The letter rain we want to achieve is basically the same as the number rain , The main thing is to change numbers into letters , The code implementation is as follows :

import random, pygame
PANEL_width = 400
PANEL_highly = 500
FONT_PX = 15
pygame.init()
# Create a window
winSur = pygame.display.set_mode((PANEL_width, PANEL_highly))
font = pygame.font.SysFont('123.ttf', 22)
bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 28))
winSur.fill((0, 0, 0))
letter = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c',
'v', 'b', 'n', 'm']
texts = [
font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)
]
# According to the width of the window, you can put several columns of coordinates on the drawing board and generate a list
column = int(PANEL_width / FONT_PX)
drops = [0 for i in range(column)]
while True:
# Get events from queue
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
chang = pygame.key.get_pressed()
if (chang[32]):
exit()
# Pause for a given number of milliseconds
pygame.time.delay(30)
# Re edit the image
winSur.blit(bg_suface, (0, 0))
for i in range(len(drops)):
text = random.choice(texts)
# Re edit the image of each coordinate point
winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
drops[i] += 1
if drops[i] * 10 > PANEL_highly or random.random() > 0.95:
drops[i] = 0
pygame.display.flip()

Let's look at the implementation effect :

 use Python Realization *** Digital rain in Empire

Graphic rain

Except for numbers and letters , We can also use pictures , Let's use the word "Fu" for the picture , Above we are using pygame Library to create windows , This time we use tkinter library , Take a look at the implementation code :

from tkinter import *
import random, threading, time, os
# Initial raindrop ordinate
INIT_HEIGHT = 1
# Raindrops create
def rainmake(canvas, imagefile):
rainlist = []
for i in range(5):
# According to the picture , Create a line of "Fu"
rainlist.append(canvas.create_image(100 + 80 * i, INIT_HEIGHT, anchor=NE, image=imagefile))
return rainlist
# Raindrops fall
def raindown(tk, canvas, imagefile, sec):
# Wait between threads
time.sleep(sec)
rainlist = rainmake(canvas, imagefile)
# The ordinate value of each character
height = [INIT_HEIGHT] * 10
while True:
# Wait a moment before each move
time.sleep(0.2)
# 5 The two characters move together
for i in range(5):
# If the word "Fu" goes to the end , It doesn't move on
if not height[i] == 0:
# Set the fall pace
rnd = random.randint(5, 50)
canvas.move(rainlist[i], 0, rnd)
height[i] = height[i] + rnd
tk.update()
for i,h in enumerate(height):
if h > 400:
# When the word "Fu" comes to the bottom , Delete
canvas.delete(rainlist[i])
tk.update()
# Empty the blessed height
height[i] = 0
print(i,h,height)
# To the end , And then jump out of the loop
if height == [0] * 5:
print('break:',threading.current_thread().name)
break
def lookloop(tk, canvas, thread):
aliveflg = False
while True:
# 5s Test once
time.sleep(5)
for th in thread:
if th.is_alive():
aliveflg = True
else:
aliveflg = False
if aliveflg == False:
break
canvas.create_text(100 , 200, text=' The rain has stopped ...', fill='red')
canvas.pack()
time.sleep(5)
tk.destroy()
def main():
# Create window objects
tk = Tk()
tk.title(' Send blessing rain ')
canvas_style = {
'bg':'white',
'height':'500',
'width':'410',
'cursor':'circle'
}
# Create a canvas
canvas = Canvas(tk,canvas_style)
canvas.pack()
# Picture material
if not os.path.exists('pic.gif'):
raise Exception('pic.gif file does not exists.')
imagefile = PhotoImage(file = 'pic.gif')
thread = []
for i in range(100):
thread.append(threading.Thread(target=raindown, args=(tk, canvas, imagefile, i)))
for t in thread:
t.start()
# Start a new thread to monitor running threads
threading.Thread(target=lookloop, args=(tk, canvas, thread)).start()
# Enter the message loop
tk.mainloop()

Let's look at the implementation effect :

 use Python Realization *** Digital rain in Empire

summary

In this paper, we use Python It's Digital 、 The rain effect of letters and pictures , If you are interested, you can realize it yourself .