python烟花代码
时间: 2023-07-02 11:05:24 浏览: 1037
以下是一个简单的Python烟花代码:
```python
import random
import time
import turtle
# 设置画布
canvas = turtle.Screen()
canvas.bgcolor("black")
# 定义烟花颜色列表
colors = ["red", "yellow", "blue", "green", "purple", "white", "orange"]
# 定义烟花
class Firework:
def __init__(self):
self.x = random.randint(-200, 200)
self.y = random.randint(-200, 200)
self.color = random.choice(colors)
# 绘制烟花
def explode(self):
for i in range(30):
particle = turtle.Turtle()
particle.speed(0)
particle.shape("circle")
particle.color(self.color)
particle.penup()
particle.goto(self.x, self.y)
particle.pendown()
# 粒子爆炸效果
explosion_speed = random.randint(1, 10)
particle.speed(explosion_speed)
# 粒子移动
angle = random.uniform(0, 2 * math.pi)
distance = random.randint(10, 200)
x = self.x + math.sin(angle) * distance
y = self.y + math.cos(angle) * distance
particle.goto(x, y)
# 清除粒子
particle.hideturtle()
# 绘制多个烟花
for i in range(25):
firework = Firework()
firework.explode()
# 隐藏画笔
turtle.hideturtle()
# 显示画面
canvas.mainloop()
```
这个代码使用Python的turtle库绘制烟花,每次绘制一个随机位置和颜色的烟花,然后在一定时间内爆炸,发射出一些随机颜色和位置的粒子。可以根据需要自行修改和调整。
阅读全文