python烟花代码
时间: 2023-07-02 20:23:18 浏览: 102
python程序烟花代码
5星 · 资源好评率100%
以下是基于Python的烟花代码,你可以在终端中运行它:
```python
import random
import math
import turtle
# 创建烟花
def create_firework(color):
firework = turtle.Turtle()
firework.hideturtle()
firework.speed(0)
firework.penup()
firework.color(color)
firework.goto(random.randint(-300, 300), random.randint(-200, 200))
firework.pendown()
return firework
# 创建烟花爆炸
def create_explosion(color):
explosion = turtle.Turtle()
explosion.hideturtle()
explosion.speed(0)
explosion.penup()
explosion.color(color)
explosion.pensize(5)
explosion.goto(random.randint(-300, 300), random.randint(-200, 200))
explosion.pendown()
return explosion
# 计算烟花半径
def calculate_radius(distance):
return distance / 10
# 计算烟花速度
def calculate_speed(distance):
return math.ceil(distance / 10)
# 发射烟花
def launch_firework(color):
firework = create_firework(color)
distance = random.randint(100, 300)
firework.setheading(90)
firework.pensize(3)
firework.speed(calculate_speed(distance))
firework.pendown()
firework.forward(distance)
explosion = create_explosion(color)
for i in range(30):
explosion.pensize(i / 5)
explosion.circle(calculate_radius(i))
explosion.clear()
# 主程序
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
turtle.bgcolor("black")
turtle.hideturtle()
turtle.speed(0)
for i in range(50):
color = random.choice(colors)
launch_firework(color)
turtle.done()
```
这段代码利用了Python中的turtle库,通过循环语句和一些简单的计算,生成了一系列不同颜色的烟花。运行代码后,你可以在窗口中看到一些烟花不断发射和爆炸的场景。
阅读全文