Python 实现超炫生日贺卡,带烟花动画效果及开发避坑指南!
# Python生日贺卡代码(带烟花动画) ```python import pygame import random import math import time # 初始化pygame pygame.init() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("生日贺卡") # 颜色定义 WHITE = (255, 255, 255) BLACK = (0, 0, 0) COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (255, 165, 0)] # 烟花粒子类 class Particle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = random.randint(1, 3) angle = random.uniform(0, math.pi * 2) speed = random.uniform(2, 6) self.vx = math.cos(angle) * speed self.vy = math.sin(angle) * speed self.lifetime = random.randint(30, 60) def update(self): self.x += self.vx self.y += self.vy self.vy += 0.1 # 重力 self.lifetime -= 1 def draw(self, surface): alpha = min(255, self.lifetime * 4) color_with_alpha = (*self.color, alpha) pygame.draw.circle(surface, color_with_alpha, (int(self.x), int(self.y)), self.radius) # 烟花类 class Firework: def __init__(self, x, y): self.x = x self.y = y self.color = random.choice(COLORS) self.particles = [] self.exploded = False self.vy = -random.uniform(8, 12) def update(self): if not self.exploded: self.y += self.vy self.vy += 0.1 if self.vy >= 0: self.explode() else: for particle in self.particles[:]: particle.update() if particle.lifetime <= 0: self.particles.remove(particle) def explode(self): self.exploded = True for _ in range(100): self.particles.append(Particle(self.x, self.y, self.color)) def draw(self, surface): if not self.exploded: pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), 3) else: for particle in self.particles: particle.draw(surface) # 主函数 def main(): clock = pygame.time.Clock() font = pygame.font.SysFont('simhei', 48) small_font = pygame.font.SysFont('simhei', 24) fireworks = [] last_firework = 0 running = True while running: current_time = pygame.time.get_ticks() # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: fireworks.append(Firework(event.pos[0], event.pos[1])) # 自动生成烟花 if current_time - last_firework > 500: fireworks.append(Firework(random.randint(100, width-100), height)) last_firework = current_time # 更新烟花 for firework in fireworks[:]: firework.update() if firework.exploded and len(firework.particles) == 0: fireworks.remove(firework) # 绘制 screen.fill(BLACK) # 绘制祝福文字 text = font.render("生日快乐!", True, WHITE) screen.blit(text, (width//2 - text.get_width()//2, 50)) sub_text = small_font.render("点击屏幕生成更多烟花", True, WHITE) screen.blit(sub_text, (width//2 - sub_text.get_width()//2, 120)) # 绘制烟花 for firework in fireworks: firework.draw(screen) pygame.display.flip() clock.tick(60) pygame.quit() if __name__ == "__main__": main() ``` 运行说明: 1. 需要安装pygame库:`pip install pygame` 2. 运行后会显示生日祝福文字和自动生成的烟花 3. 点击屏幕可以在点击位置生成额外烟花 4. 按关闭按钮退出程序 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: - 粒子系统的性能优化,避免创建过多粒子导致性能下降 - 颜色和透明度的处理需要精确控制,确保视觉效果流畅 - 事件处理循环要正确管理,防止程序无响应或内存泄漏 ######[AI写代码神器 | 1132点数解答 | 2025-09-13 13:53:04]
- C语言实现通讯录按年龄从大到小排序:代码详解与复杂度分析(字节豆包 | 604点数解答 | 2025-12-11 11:24:48)167
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)471
- Java 实现学生类:属性封装、静态方法调用与对象信息输出(字节豆包 | 236点数解答 | 2024-09-18 16:15:05)234
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)352
- 生日祝福 HTML 代码优化建议与逻辑要点大揭秘(字节豆包 | 164点数解答 | 2025-03-19 22:07:03)222
- Python 实战:学生信息列表转字典、遍历、深拷贝及数据修改全流程(GPT | 422点数解答 | 2024-10-29 15:43:54)427
- Python实战:学生成绩列表转字典,深拷贝与数据修改操作全解析(字节豆包 | 254点数解答 | 2024-10-29 16:01:39)418
- Android开发必备:多种动画API解析及使用注意事项(阿里通义 | 355点数解答 | 2023-11-07 22:46:27)234
- 2 秒动画搞定!“好句子”如何稳停结束位置( | 456点数解答 | 2024-05-19 16:46:15)277
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)323
- Java调用Python接口中文乱码?设置UTF - 8编码一招解决!(讯飞星火 | 263点数解答 | 2024-06-06 17:07:59)400
- 解决Java调用Python接口中文乱码问题:设置UTF - 8编码全攻略(讯飞星火 | 160点数解答 | 2024-06-06 17:18:39)423