import random
import pygame
import sys
# 初始化参数设计
win_width=1000
win_height=800
font_px=15
# 创建窗口及文本设计
pygame.init()
winsur=pygame.display.set_mode((win_width,win_height))
font=pygame.font.SysFont('',23)
bg_surface=pygame.Surface((win_width,win_height),flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_surface)
bg_surface.fill(pygame.Color(0,0,0,28))
winsur.fill((0,0,0))
# 文本内容
letter='1234567890!@#$%^&*qwertyuiopasdfghjklzxcvbnm'
texts=[font.render(letter[i],True,(0,255,0)) for i in range(44)]
# 显示设计
column=int(win_width/font_px)
drops=[0 for i in range(column)]
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
change=pygame.key.get_pressed()
if change[32]:
sys.exit()
# 延时30秒
pygame.time.delay(30)
winsur.blit(bg_surface,(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>win_height or random.random()>0.95:
drops[i]=0
pygame.display.flip()
效果:
打包成exe
pyinstaller -F -w -i coderain.ico coderain.py
鼠标点住上方蓝色的代码雨会停止流动
coderain