我正在參加「創意開發 投稿大賽」詳情請看:掘金創意開發大賽來了!
上手 python,A lot of people first contact is pygame 包了,Can have fun at the same time,建立起對 python 的基本認識.
提到編程,I think many people's first impression is the hacker's image in the film,Under the background of black keyboard,With flashing green font,Dive into the crack system one by one......
當然,Reality and the imagination, of course, is irrelevant,But given the imagination,Why not to try on their own to achieve?所以,We can use this very simple30Lines of code to achieve the effect of a rain of code,Meet our imagination and curiosity.
We just use thispygame
與random
兩個包,首先,Will they import:
import pygame
import random
復制代碼
之後,我們進行pygameInitialization of the interface:
# 參數
SCREENSIZE=(600,600)
BLACK=(0,0,0,13)
# 初始化
pygame.init()
font = pygame.font.SysFont('宋體', 20)
screen = pygame.display.set_mode(SCREENSIZE)
surface = pygame.Surface(SCREENSIZE, flags=pygame.SRCALPHA)
pygame.Surface.convert(surface)
surface.fill(BLACK)
screen.fill(BLACK)
復制代碼
After setting the content related to our font:
# 內容
lib=[chr(i) for i in range(48,48+10)] + [chr(i) for i in range(97,97+26)] # [0-9 a-z]
texts = [font.render(l, True, (0, 255, 0)) for l in lib]
cols = list(range(40)) # 字體15, 窗口600
復制代碼
The last in a loop,Update the interface and code to map the rain:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.time.delay(33)
screen.blit(surface, (0, 0))
for i in range(n:=len(cols)):
text = random.choice(texts)
# 繪制代碼雨
screen.blit(text, (i * 15, cols[i] * 15))
cols[i] = 0 if cols[i] >80 or random.random() > 0.95 else cols[i]+1
pygame.display.flip()
復制代碼
完整代碼如下:
import pygame
import random
# 參數
SCREENSIZE=(600,600)
BLACK=(0,0,0,13)
# 初始化
pygame.init()
font = pygame.font.SysFont('宋體', 20)
screen = pygame.display.set_mode(SCREENSIZE)
surface = pygame.Surface(SCREENSIZE, flags=pygame.SRCALPHA)
pygame.Surface.convert(surface)
surface.fill(BLACK)
screen.fill(BLACK)
# 內容
lib=[chr(i) for i in range(48,48+10)] + [chr(i) for i in range(97,97+26)] # [0-9 a-z]
texts = [font.render(l, True, (0, 255, 0)) for l in lib]
cols = list(range(40)) # 字體15, 窗口600
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.time.delay(33)
screen.blit(surface, (0, 0))
for i in range(n:=len(cols)):
text = random.choice(texts)
screen.blit(text, (i * 15, cols[i] * 15))
cols[i] = 0 if cols[i] >80 or random.random() > 0.95 else cols[i]+1
pygame.display.flip()
復制代碼