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

相关推荐
花酒锄作田13 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪16 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽17 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战17 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋1 天前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者2 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者2 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh2 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅2 天前
Python函数入门详解(定义+调用+参数)
python
曲幽2 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama