发布时间:2025-06-24 18:28:56 作者:北方职教升学中心 阅读量:016
如下效果如下a;
#!/bin/python# coding:utf-8import turtleimport random# 设置屏幕screnen = turtle.Screen()screen.bgcolor("black")# 创造烟花的画家firework = turtle.Turtle()firework.hideturtle()firework.speed(0)firework.color("white")# 烟花爆炸效果def explode(x, y, colors): firework.penup() firework.goto(x, y) firework.pendown() for _ in range(36): # 烟花爆炸的射线数 color = random.choice(colors) firework.color(color) firework.forward(80) firework.backward(80) firework.right(10)# 烟花升高效果def firework_up(x, y, color): firework.penup() firework.goto(x, y - 200) firework.pendown() firework.color(color) firework.goto(x, y)# 主函数,画烟花def main(): firework.showturtle() colors = ["red", "yellow", "blue", "green", "orange", "purple", "white", "pink"] for _ in range(15): # 烟花数量 x = random.randint(-150, 150) y = random.randint(-150, 150) up_color = random.choice(colors) firework_up(x, y, up_color) explode(x, y, colors) firework.hideturtle()if __name__ == '__main__': # 运行主函数 main() # 点击屏幕后退出 screen.exitonclick()。