帮我写一个放烟花的python代码
时间: 2023-03-24 11:04:05 浏览: 127
python烟花效果的代码实例
5星 · 资源好评率100%
以下是一个简单的放烟花的 Python 代码:
```python
import turtle
import random
# 创建画布
canvas = turtle.Screen()
canvas.bgcolor("black")
# 创建烟花
def create_firework(x, y):
firework = turtle.Turtle()
firework.hideturtle()
firework.speed()
firework.penup()
firework.goto(x, y)
firework.pendown()
firework.pensize(3)
firework.color("white")
return firework
# 创建烟花爆炸效果
def create_explosion(x, y):
explosion = turtle.Turtle()
explosion.hideturtle()
explosion.speed()
explosion.penup()
explosion.goto(x, y)
explosion.pendown()
explosion.pensize(3)
explosion.color("white")
return explosion
# 放烟花
def launch_firework():
x = random.randint(-300, 300)
y = random.randint(-200, 200)
firework = create_firework(x, y)
for i in range(25):
firework.forward(i * 10)
firework.right(144)
explosion = create_explosion(x, y)
for i in range(25):
explosion.circle(i * 10)
firework.clear()
explosion.clear()
# 循环放烟花
while True:
launch_firework()
```
希望对你有帮助!
阅读全文