python代码案例分享,python实现代码雨,动画显示,pygame使用教程

python代码案例分享,python实现代码雨,动画显示

代码实现:

python 复制代码
# 代码雨列表
class CodeRain:
    def __init__(self):
        self.column_width = 20
        self.columns = WIDTH // self.column_width
        self.drops = []
        self.texts = []
        self.colors = []
        self.speeds = []
        self.trail_colors = []  # 轨迹颜色
        
        for i in range(self.columns):
            self.drops.append(random.randint(-100, 0))
            self.texts.append(randomCode())
            self.colors.append(randomColor())
            self.speeds.append(randomSpeed() / 10)
            self.trail_colors.append((0, random.randint(100, 255), 0))  # 绿色轨迹
    
    def update(self):
        for i in range(self.columns):
            self.drops[i] += self.speeds[i]
            
            if self.drops[i] > HEIGHT:
                self.drops[i] = random.randint(-100, 0)
                self.texts[i] = randomCode()
                self.colors[i] = randomColor()
                self.speeds[i] = randomSpeed() / 10
    
    def draw(self, surface):
        for i in range(self.columns):
            if self.drops[i] > 0:
                # 绘制轨迹效果
                for j in range(10):
                    if self.drops[i] - j * 15 > 0:
                        trail_text = randomCode()
                        trail_color = (0, max(0, 255 - j * 25), 0)  # 渐变绿色
                        trail_surface = font.render(trail_text, True, trail_color)
                        surface.blit(trail_surface, (i * self.column_width, self.drops[i] - j * 15))
                
                # 绘制主字符
                text_surface = font.render(self.texts[i], True, self.colors[i])
                surface.blit(text_surface, (i * self.column_width, self.drops[i]))

定义代码精力类:

python 复制代码
class Code(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.font = pygame.font.Font('./font.ttf', randomSize())	# 随机字体大小
		self.speed = randomSpeed()			# 随机速度
		self.code = self.getCode()			# 随机长度
		self.image = self.font.render(self.code, True, randomColor())	# 使用已有的文本创建一个位图image,返回值为一个image  随机颜色
		self.image = pygame.transform.rotate(self.image, random.randint(87, 93))	# 讲图像随机旋转角度
		self.rect = self.image.get_rect()
		self.rect.topleft = randomPos()		# 随机位置

	def getCode(self):
		length = randomLen()
		code = ''
		for i in range(length):
			code += randomCode()
		return code
	def update(self):
		self.rect = self.rect.move(0, self.speed)
		if self.rect.top > HEIGHT:
			self.kill()

初始化pygame

python 复制代码
pygame.init()			# 初始函数,使用pygame的第一步
screen = pygame.display.set_mode((WIDTH, HEIGHT))	#生成主屏幕screen;第一个参数是屏幕大小
pygame.display.set_caption('Code Rain-居然')	# 窗口命名

clock = pygame.time.Clock()					# 初始化一个clock对象
codesGroup = pygame.sprite.Group()			# 精灵组,一个简单的实体容器
while True:
	clock.tick(24)							# 控制游戏绘制的最大帧率为30
	for event in pygame.event.get():
		if event.type == QUIT:
			pygame.quit()
			sys.exit(0)
	# screen.fill((1, 1, 1))					# 填充
	screen.fill((0, 0, 0))						# 填充背景颜色

	codeobject = Code()
	codesGroup.add(codeobject)				# 添加精灵对象
	codesGroup.update()
	codesGroup.draw(screen)
	pygame.display.update()

最终执行demo类

我整理了一些python代码案例源码以及教程,包含基础教程,自动化,算法,实例案例有几百个案例,python学习佳品

「python源码案例」

https://drive.uc.cn/s/f661de583ee34

相关推荐
鹏易灵9 小时前
C++——8.移动语义初探(移动构造、移动赋值)
开发语言·c++·php
2601_962341309 小时前
计算机毕业设计之jsp考研在线复习平台
java·大数据·开发语言·hadoop·python·考研·课程设计
凯瑟琳.奥古斯特9 小时前
力扣1008:前序重建BST
开发语言·c++·算法·leetcode·职场和发展
2zcode10 小时前
项目文档:基于MATLAB图像处理的饮料瓶灌装液位检测系统设计与实现
开发语言·图像处理·matlab
云烟成雨TD10 小时前
LangFlow 1.x 系列【7】工作流创建与部署指南
人工智能·python·agent
月疯10 小时前
np.where()[0]的用法
开发语言·python·numpy
z落落10 小时前
C#五大加密算法(AES、DES、MD5、RSA、SHA)
开发语言·c#
namexingyun10 小时前
Scaling Law bug实战启示:从“虚胖“到“精瘦“的算力效率革命
开发语言·网络·人工智能·bug·ai编程
KaMeidebaby10 小时前
卡梅德生物技术快报|纳米抗体技术全套实操流程:AFB1 全合成文库淘选 + 分子对接定点突变参数手册
人工智能·python·tcp/ip·算法·机器学习
hnxaoli10 小时前
统信小程序(十六)xls转xlsx
开发语言·python·小程序