python实现烟花表演

创建一个简单的烟花表演动画需要用到Python的图形库,比如pygame或者matplotlib的动画功能。这里,我会使用pygame来演示如何创建一个简单的烟花表演。

首先,你需要安装pygame库。你可以使用pip来安装:

复制代码

bash复制代码

|---|----------------------|
| | pip install pygame |

接下来是一个简单的烟花表演示例代码:

复制代码

python复制代码

|---|--------------------------------------------------------------------------------------------------------------------------------------------------|
| | import pygame |
| | import random |
| | import math |
| | |
| | # 初始化pygame |
| | pygame.init() |
| | |
| | # 设置屏幕大小 |
| | screen_width, screen_height = 800, 600 |
| | screen = pygame.display.set_mode((screen_width, screen_height)) |
| | pygame.display.set_caption("烟花表演") |
| | |
| | # 颜色定义 |
| | BLACK = (0, 0, 0) |
| | WHITE = (255, 255, 255) |
| | |
| | # 烟花类 |
| | class Firework: |
| | def __init__(self): |
| | # 初始化烟花位置 |
| | self.x = random.randint(0, screen_width) |
| | self.y = screen_height |
| | # 初始化烟花速度 |
| | self.vx = random.randint(-5, 5) |
| | self.vy = -10 - random.randint(0, 10) |
| | # 初始化烟花颜色 |
| | self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) |
| | # 初始化烟花粒子 |
| | self.particles = [] |
| | for _ in range(random.randint(50, 100)): |
| | self.particles.append([random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), random.randint(-5, 5), random.randint(5, 15)]) |
| | |
| | def update(self): |
| | # 更新烟花位置 |
| | self.x += self.vx |
| | self.y += self.vy |
| | # 烟花爆炸效果 |
| | if self.y < 0: |
| | for particle in self.particles: |
| | particle[3] += 0.5 |
| | particle[4] += 0.1 |
| | pygame.draw.circle(screen, particle[:3], (int(self.x + particle[3]), int(self.y + particle[4])), 1) |
| | self.particles = [] # 爆炸后清空粒子 |
| | else: |
| | pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 2) |
| | |
| | def is_done(self): |
| | return self.y < -screen_height |
| | |
| | # 游戏主循环 |
| | running = True |
| | fireworks = [] |
| | clock = pygame.time.Clock() |
| | |
| | while running: |
| | for event in pygame.event.get(): |
| | if event.type == pygame.QUIT: |
| | running = False |
| | elif event.type == pygame.KEYDOWN: |
| | if event.key == pygame.K_SPACE: |
| | fireworks.append(Firework()) |
| | |
| | screen.fill(BLACK) |
| | |
| | for fw in fireworks: |
| | fw.update() |
| | if fw.is_done(): |
| | fireworks.remove(fw) |
| | |
| | pygame.display.flip() |
| | clock.tick(60) |
| | |
| | pygame.quit() |

这个示例代码创建了一个Firework类来表示烟花,每个烟花有自己的位置和速度,当烟花到达屏幕底部时,它会爆炸并生成一些粒子,每个粒子也有自己的颜色和速度。

主循环中,我们监听键盘事件,当按下空格键时,会生成一个新的烟花。我们还限制了帧率为60帧每秒,以确保动画的流畅性。

这只是一个非常基础的示例,你可以根据需要增加更多的功能,比如烟花的不同颜色、形状、爆炸效果,或者添加背景音乐等。

相关推荐
·云扬·7 分钟前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
liulilittle37 分钟前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
巴里巴气1 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19891 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm
JavaEdge在掘金1 小时前
Redis 数据倾斜?别慌!从成因到解决方案,一文帮你搞定
python
ansurfen1 小时前
我的第一个AI项目:从零搭建RAG知识库的踩坑之旅
python·llm
Thomas_YXQ1 小时前
Unity URP法线贴图实现教程
开发语言·unity·性能优化·游戏引擎·unity3d·贴图·单一职责原则
前端付豪1 小时前
20、用 Python + API 打造终端天气预报工具(支持城市查询、天气图标、美化输出🧊
后端·python
前端付豪1 小时前
19、用 Python + OpenAI 构建一个命令行 AI 问答助手
后端·python
Zz_waiting.1 小时前
Javaweb - 10.4 ServletConfig 和 ServletContext
java·开发语言·前端·servlet·servletconfig·servletcontext·域对象