一、准备工作
下载Python解释器和PyCharm,可以参考以下文章:
Python基础 笔记(一) Python的介绍_木易巷的博客-CSDN博客
二、话不多说,直接上代码
有四种效果,可自行查看效果图选择。
效果图为截图,实际上是为动图,是文字从上到下流动的样式。
效果一:
import random
import pygame
from pygame.locals import *
from sys import exit
PANEL_width = 1920
PANEL_highly = 1080
FONT_PX = 40
pygame.init()
# 创建一个可视窗口, 如果不想全屏运行,可以把 FULLSCREEN 参数去掉
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)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 20))
winSur.fill((0, 0, 0))
# 汉字版
letter = ['我', '爱', '你', '我', '爱你', '我爱你', '我非常爱你', '我爱你', '我爱', '我', '爱', '你',
'我爱你', '爱', '我', '爱你', '我', '我爱', '爱你', '你']
texts = [
font.render(str(letter[i]), True, (0, 255, 0)) for i in range(20)
]
# 按屏幕的宽带计算可以在画板上放几列坐标并生成一个列表
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()
文字版效果图(文字可在代码中自行更改):
效果二:
import random
import pygame
from pygame.locals import *
from sys import exit
PANEL_width = 1920
PANEL_highly = 1080
FONT_PX = 40
pygame.init()
# 创建一个可视窗口, 如果不想全屏运行,可以把 FULLSCREEN 参数去掉
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)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 20))
winSur.fill((0, 0, 0))
# 二进制版
letter = ['1', '0', '1', '1', '1', '0', '0', '0', '1', '0', '1', '0', '1', '0', '0', '1', '1', '0', '0', '0', '1', '1','1', '0', '1', '0', '0', '1', '0', '1']
texts = [
font.render(str(letter[i]), True, (0, 255, 0)) for i in range(20)
]
# 按屏幕的宽带计算可以在画板上放几列坐标并生成一个列表
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()
二进制版效果图:
效果三:
import random
import pygame
from pygame.locals import *
from sys import exit
PANEL_width = 1920
PANEL_highly = 1080
FONT_PX = 40
pygame.init()
# 创建一个可视窗口, 如果不想全屏运行,可以把 FULLSCREEN 参数去掉
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)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 20))
winSur.fill((0, 0, 0))
# # 字母版
letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v','w', 'x', 'y', 'z']
texts = [
font.render(str(letter[i]), True, (0, 255, 0)) for i in range(20)
]
# 按屏幕的宽带计算可以在画板上放几列坐标并生成一个列表
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()
字母版效果图:
效果四:
import random
import pygame
from pygame.locals import *
from sys import exit
PANEL_width = 1920
PANEL_highly = 1080
FONT_PX = 40
pygame.init()
# 创建一个可视窗口, 如果不想全屏运行,可以把 FULLSCREEN 参数去掉
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)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0, 0, 0, 20))
winSur.fill((0, 0, 0))
# 数字版
texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)]
# 按屏幕的宽带计算可以在画板上放几列坐标并生成一个列表
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()
数字版效果图:
三、复制代码到Pycharm
直接复制粘贴到Pycharm中,运行即可。
如果提示缺少包,win+R键打开Run窗口后,则可去cmd命令行下输入:pip install 包名。
例: pip install random pip install pygame
快去试试吧!
有问题可以评论私信哦~
<end>