import turtle
import random
class Firework:
def __init__(self):
# 创建烟花的 turtle 对象
self.firework = turtle.Turtle()
self.firework.hideturtle()
self.firework.speed(0) # 设置绘画速度为最快
# 设置屏幕
self.screen = turtle.Screen()
self.screen.bgcolor("black")
self.screen.title("Firework Simulation")
# 烟花发射位置 -> 后面动态设置
# self.firework.penup()
# self.firework.goto(0, -250)
# self.firework.pendown()
# 随机颜色列表
self.colors = ["red", "yellow", "blue", "green", "purple", "orange", "pink", "white"]
# 烟花爆炸效果
def draw_firework(self,x, y):
'''传入随机位置'''
self.firework.penup()
self.firework.goto(x, y)
self.firework.pendown()
'''随机选择颜色'''
self.firework.color(random.choice(self.colors))
for _ in range(36):
self.firework.forward(100)
self.firework.backward(100)
self.firework.right(10)
#烟花上升效果
def launch_firework(self):
'''设置方向'''
self.firework.setheading(90)
for _ in range(30):
'''每次10个单位, 逐渐上升'''
self.firework.forward(10)
self.draw_firework(self.firework.xcor(), self.firework.ycor())
# 主函数
def explode_firework(self):
while True:
x = random.randint(-200, 200)
y = random.randint(-200, 0)
self.firework.penup()
self.firework.goto(x, y)
self.firework.pendown()
self.launch_firework()
#self.firework.clear()
f1 = Firework()
#just a man in the void
#山谷和草原一同吹来亘古的风
f1.explode_firework()
[杨雨贤发射器]python烟花代码
最新推荐文章于 2024-11-13 17:26:18 发布