<> use Python and Pygame Implementation of code rain

* Implementation effect
* Programming
* initialization , window creation
* Digital version , Alphabetic version
* Set coordinates , Code rain generation
* General procedure
<> Implementation effect

adopt Python Of IDLE Compiler and Pygame Programming . Implementation generates code ( Green letters with gradients ) Of tk window .

( Program flow chart )
Imitate the effect of code rain seen on the Internet , use Python and Pygame Write a small program , To achieve this effect :

( Implementation effect )

<> Programming

This program uses IDLE Compiler writing . First of all cmd download Pygame plug-in unit ( If it's red, brush it again )

<> initialization , window creation

First initialize the window size (width,highly,PX), Create another visual window .
import random import pygame PANEL_width = 600 PANEL_highly = 500 FONT_PX = 15
pygame.init() winSur = pygame.display.set_mode((PANEL_width, PANEL_highly)) font
= pygame.font.SysFont("123.ttf", 25) 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))
This creates a normal window ( The width and height and the number of character lines cannot be changed ).

<> Digital version , Alphabetic version

Create digital version texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)] And alphabetic version
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']
. The default is alphabetic , If you want a digital version, please add a prefix before the letter code # number , Number code front handle # No .
Letters can be replaced with what you want , Pay attention to change range The number of .
#texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)] 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) ] column = int(PANEL_width /
FONT_PX) drops = [0 for i in range(column)]
<> Set coordinates , Code rain generation

Set each letter ( number ) coordinate , The gradient disappears after a certain millisecond pause , At the same time let the letter of the next coordinate ( number ) display , So back and forth , Achieve the effect of the code .
while True: 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() pygame.time.delay(30) 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 > PANEL_highly or random.random()
> 0.95: drops[i] = 0 pygame.display.flip()
Refresh rate can be changed , As long as the pygame.time.delay(30) Change the number of milliseconds in brackets to what you want .

<> General procedure

Total program Uploaded
import random import pygame PANEL_width = 600 PANEL_highly = 500 FONT_PX = 15
pygame.init() winSur = pygame.display.set_mode((PANEL_width, PANEL_highly)) font
= pygame.font.SysFont("123.ttf", 25) 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)) #texts =
[font.render(str(i), True, (0, 255, 0)) for i in range(10)] 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) ] column = int(PANEL_width / FONT_PX)
drops= [0 for i in range(column)] while True: 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() pygame.time.delay(30) 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 > PANEL_highly or random.random() > 0.95: drops[i] = 0 pygame.display.flip()
<> This project can be changed

* You can choose the alphabetic version or the digital version
* You can change the contents of the letters in the code
* Adjustable code refresh rate
There may be articles on higher modifiable programs later ~ The sixth grade cooled me , Please support ~
* Part of the program from the tutorial

Technology