下面是python语言绘制从下向上的烟花的代码:

import turtle
from random import randint
# 设置画布大小
turtle.setup(800, 600)
# 设置画笔
turtle.pensize(3)
# 设置画面颜色和速度
turtle.bgcolor('black')
turtle.speed(10)
# 设置颜色列表
colors = ['red', 'yellow', 'blue', 'green', 'purple', 'orange']
# 定义绘制烟花的函数
def draw_firework():
# 设置画笔随机颜色
color = colors[randint(0, len(colors)-1)]
turtle.color(color)
# 绘制烟花的中心点
turtle.penup()
turtle.goto(0, -300)
turtle.pendown()
turtle.dot(20)
# 绘制烟花的线条
turtle.penup()
turtle.goto(-200, -300)
turtle.pendown()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
# 绘制烟花的花朵
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(10)
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(10)
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(10)
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(10)
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(10)
# 清除烟花的轨迹
turtle.penup()
turtle.goto(-200, -300)
turtle.pendown()
turtle.color('black')
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.goto(randint(-200, 200), randint(100, 500))
# 绘制烟花的爆炸效果
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(100, color)
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(100, color)
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(100, color)
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(100, color)
turtle.penup()
turtle.goto(randint(-200, 200), randint(100, 500))
turtle.pendown()
turtle.dot(100, color)
# 绘制多个烟花效果
for i in range(10):
draw_firework()
# 隐藏画笔
turtle.hideturtle()
# 停在画面上
turtle.done()
运行上面的代码,可以绘制出从下向上的烟花。每次运行程序,都会有不同的烟花效果。

查看详情

查看详情