导入所需要的模块
import pygame
import random
import string
width = 1200
hight = 600
FONT_PX = 18
pygame.init()
创建窗口
winSur = pygame.display.set_mode((width, hight))
设置字体
fout = pygame.font.SysFont(“宋体”, 25)
bg_surface = pygame.Surface((width, hight), flags=pygame.SRCALPHA)
使用纯色填充,画板添加颜色
这里再举个例子,如何自动生成自己所需要的数字、字母和其他特殊字符
import string # 导入string这个模块
print(string.digits) # 输出包含数字0~9的字符串
print(string.ascii_letters) # 包含所有字母(大写或小写)的字符串
print(string.ascii_lowercase) # 包含所有小写字母的字符串
print(string.ascii_uppercase) # 包含所有大写字母的字符串
print(string.punctuation) # 包含所有标点的字符串
acs = string.punctuation
for i in acs:
print(i)
bg_surface.fill(pygame.Color(0, 0, 0, 30))
acs = string.punctuation + string.digits + string.ascii_letters
texts = [fout.render(str(i), True, (0, 255, 0)) for i in acs]
column = int(width / FONT_PX)
print(acs)
drops = [0 for i in range(column)]
while True: # 死循环
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
exit()
# 编辑图片的坐标点
pygame.time.delay(45)
winSur.blit(bg_surface, (0, 0))
for i in range(len(drops)):
text = random.choice(texts)
# print(text)
winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
drops[i] += 1
if drops[i] * 10 > hight or random.random() > 0.95:
drops[i] = 0
pygame.display.flip()
![最终效果](https://img-blog.csdnimg.cn/20200912094147371.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80OTg2ODg2NA==,size_16,color_FFFFFF,t_70#pic_center)