首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

2025年一段代码使用python完成过年放烟花写祝福

  • 25-02-16 03:40
  • 4724
  • 11820
blog.csdn.net

运行效果如下,也可以自己自定义要祝福的人,虽然做的不太好但是也可以先用用
 

  1. import pygame
  2. import random
  3. import math
  4. # 初始化Pygame
  5. pygame.init()
  6. # 设置窗口大小
  7. width, height = 800, 600
  8. screen = pygame.display.set_mode((width, height))
  9. pygame.display.set_caption("天总,新年快乐!")
  10. # 颜色定义
  11. WHITE = (255, 255, 255)
  12. BLACK = (0, 0, 0)
  13. RED = (255, 0, 0)
  14. GREEN = (0, 255, 0)
  15. BLUE = (0, 0, 255)
  16. YELLOW = (255, 255, 0)
  17. # 文字设置
  18. font_path = "C:/Windows/Fonts/simhei.ttf" # 替换为你的中文字体路径
  19. font = pygame.font.Font(font_path, 74)
  20. text = font.render("天总,新年快乐呦!", True, WHITE)
  21. text_rect = text.get_rect(center=(width // 2, height // 2))
  22. # 烟花粒子类
  23. class Firework:
  24. def __init__(self, x, y, color):
  25. self.x = x
  26. self.y = y
  27. self.color = color
  28. self.particles = []
  29. self.exploded = False
  30. def add_particle(self):
  31. angle = random.uniform(0, 2 * math.pi)
  32. speed = random.uniform(5, 10)
  33. particle = {
  34. 'x': self.x,
  35. 'y': self.y,
  36. 'vx': speed * math.cos(angle),
  37. 'vy': -speed * math.sin(angle),
  38. 'life': 50
  39. }
  40. self.particles.append(particle)
  41. def update(self):
  42. if not self.exploded:
  43. self.y -= 5
  44. if self.y < height // 2:
  45. self.exploded = True
  46. for _ in range(100):
  47. self.add_particle()
  48. else:
  49. for particle in self.particles:
  50. particle['x'] += particle['vx']
  51. particle['y'] += particle['vy']
  52. particle['vy'] += 0.1 # 加速度
  53. particle['life'] -= 1
  54. self.particles = [p for p in self.particles if p['life'] > 0]
  55. def draw(self, screen):
  56. if not self.exploded:
  57. pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 3)
  58. else:
  59. for particle in self.particles:
  60. pygame.draw.circle(screen, self.color, (int(particle['x']), int(particle['y'])), 2)
  61. # 创建烟花列表
  62. fireworks = []
  63. # 主循环
  64. running = True
  65. show_text = True
  66. text_duration = 3000 # 显示文字的时间(毫秒)
  67. start_time = pygame.time.get_ticks()
  68. while running:
  69. for event in pygame.event.get():
  70. if event.type == pygame.QUIT:
  71. running = False
  72. current_time = pygame.time.get_ticks()
  73. elapsed_time = current_time - start_time
  74. if show_text and elapsed_time >= text_duration:
  75. show_text = False
  76. screen.fill(BLACK)
  77. if show_text:
  78. screen.blit(text, text_rect)
  79. else:
  80. if random.randint(1, 20) == 1:
  81. fireworks.append(Firework(random.randint(100, width - 100), height, random.choice([RED, GREEN, BLUE, YELLOW])))
  82. for firework in fireworks:
  83. firework.update()
  84. firework.draw(screen)
  85. fireworks = [fw for fw in fireworks if not (fw.exploded and len(fw.particles) == 0)]
  86. pygame.display.flip()
  87. pygame.time.delay(30)
  88. pygame.quit()

注:本文转载自blog.csdn.net的文章永久免费只为良心的文章"https://blog.csdn.net/weixin_53693367/article/details/144842287"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

134
游戏
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top