用户:
FreeWorld查看:0 回复:0 评论:0 创建时间:2024-07-19T17:20:36
高度还原影视剧中黑客桌面代码雨,而且是动态的!
使用的库:random , pygame , sys 。
代码如下:
import random
import pygame
from pygame.locals import *
from sys import exit
PANEL_width = 1920
PANEL_highly = 1080
FONT_PX = 40
pygame.init()
winSur = pygame.display.set_mode((PANEL_width, PANEL_highly), FULLSCREEN, 32)
font = pygame.font.SysFont("SimHei", 35)
bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA)
bg_suface.fill(pygame.Color(0, 0, 0, 20))
winSur.fill((0, 0, 0))
letter = [chr(i) for i in range(97, 123)]
texts = [font.render(char, True, (0, 255, 0)) for char in letter[:20]]
column = PANEL_width // FONT_PX
drops = [0] * column
try:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
exit()
pygame.time.delay(30)
winSur.blit(bg_suface, (0, 10))
for i in range(column):
text = random.choice(texts)
winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
drops[i] += 1
if drops[i] * FONT_PX > PANEL_highly or random.random() > 0.95:
drops[i] = 0
pygame.display.flip()
except Exception as e:
print(f"发生错误: {e}")
pygame.quit()