<>用Python和Pygame实现代码雨
 * 实现效果
 * 程序编写 
 * 初始化,窗口创建
 * 数字版,字母版
 * 设置坐标,代码雨生成
 * 总程序 
 <>实现效果
通过Python的IDLE编译器和Pygame进行程序编写。实现生成有代码雨(有渐变的绿色的字母)的tk窗口。
 (程序流程图)
 仿照在网上看到的代码雨的效果,用Python和Pygame编写了一个小程序,实现这种效果:
 (实现效果)
 <>程序编写
本程序用IDLE编译器编写。首先通过cmd下载Pygame插件(如果报红就重新再刷一遍)
 <>初始化,窗口创建
首先初始化窗口各大小(width,highly,PX),再创建一个可视窗口。
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']
。默认是字母版的,如想要数字版请在字母代码前加#号,数字代码前把#号去掉。
 字母可以替换成自己想要的,注意改变range的数。
#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() 
刷新率可更改,只要将pygame.time.delay(30)括号里的毫秒数改为自己想要的就行了。
 <>总程序
总程序已上传
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() 
 <>本项目可改点
 *  可选择字母版还是数字版
 *  可改变代码雨中字母的内容
 *  可调节代码雨的刷新率 
后可能会有更高可改性程序的文章~ 六年级凉了本人,请支持~
 *项目部分程序来自教程