python烟花程序代码
时间: 2023-11-06 15:02:31 浏览: 307
Python烟花秀代码
5星 · 资源好评率100%
以下是一个简单的Python烟花程序的代码:
```python
import turtle
import random
# 设置画布
turtle.bgcolor('black')
window = turtle.Screen()
window.title("Python Fireworks")
window.setup(width=800, height=600)
# 创建烟花函数
def create_firework():
colors = ["red", "blue", "green", "yellow", "orange", "purple", "white"]
x = random.randint(-350, 350)
y = random.randint(-250, 250)
size = random.randint(10, 40)
color = random.choice(colors)
fireworks = turtle.Turtle()
fireworks.speed(0)
fireworks.shape("circle")
fireworks.color(color)
fireworks.shapesize(size/20)
fireworks.up()
fireworks.goto(x, y)
fireworks.down()
fireworks.stamp()
fireworks.hideturtle()
return fireworks
# 控制烟花爆炸
def explode_firework(firework):
firework.clear()
firework.color("gray")
for _ in range(20):
firework.shapesize(random.randint(1, 4))
firework.stamp()
firework.hideturtle()
# 主循环
while True:
firework = create_firework()
window.ontimer(lambda: explode_firework(firework), random.randint(500, 2000))
turtle.done()
```
阅读全文