Editor

Python Turtle Fireworks

Intermediate · Festivals · 76 lines · draws in about 9 seconds

This program fires five rockets into a night sky, each one a function call that draws a rising trail and a ring of coloured rays ending in sparks.

Python Turtle Fireworks: the finished drawing
import random
import turtle
random.seed(2026)
screen = turtle.Screen()
screen.title("Fireworks")
screen.bgcolor("#0d0d36")
t = turtle.Turtle()
t.speed(10)
t.pensize(2)
def burst(x, y, size, rays, color1, color2):
"""A rocket's trail up from the ground, then rays of sparks from (x, y)."""
# The trail the rocket leaves on its way up
t.penup()
t.goto(x, -170)
t.pendown()
t.color("#3a3a5c")
t.goto(x, y)
# Rays in two colours, each ending in a spark
for ray in range(rays):
t.penup()
t.goto(x, y)
t.setheading(ray * 360 / rays)
t.forward(size * 0.25)
t.pendown()
if ray % 2 == 0:
t.color(color1)
else:
t.color(color2)
t.forward(size * 0.75)
t.penup()
t.forward(size * 0.12)
t.dot(6)
# A bright flash in the middle
t.goto(x, y)
t.dot(14, "white")
# Faint stars. Speed 0 puts them all down at once, instead of
# making the turtle walk across the sky to each one.
t.speed(0)
t.penup()
for star in range(50):
t.goto(random.randint(-300, 300), random.randint(-100, 215))
t.dot(2, "lightgray")
t.speed(10)
# Five fireworks: position, size, number of rays and two colours
burst(-150, 95, 105, 24, "deeppink", "gold")
burst(95, 115, 90, 20, "deepskyblue", "white")
burst(215, -5, 70, 16, "lawngreen", "yellow")
burst(-20, -20, 65, 16, "orange", "red")
burst(-235, -60, 50, 12, "violet", "white")
# A dark city skyline in front, one building every 40 steps
t.color("#03030f")
for left in range(-300, 300, 40):
height = random.randint(35, 100)
t.penup()
t.goto(left, -215)
t.pendown()
t.begin_fill()
t.goto(left + 40, -215)
t.goto(left + 40, -215 + height)
t.goto(left, -215 + height)
t.goto(left, -215)
t.end_fill()
t.hideturtle()
turtle.done()

How the code works

A burst of rays

First a dim line rises from the ground to (x, y): the rocket's trail. Then each ray starts a quarter of the way out, so the middle stays dark, and ends in a spark made with dot().

def burst(x, y, size, rays, color1, color2):
    """A rocket's trail up from the ground, then rays of sparks from (x, y)."""
    # The trail the rocket leaves on its way up
    t.penup()
    t.goto(x, -170)
    t.pendown()
    t.color("#3a3a5c")
    t.goto(x, y)

    # Rays in two colours, each ending in a spark
    for ray in range(rays):
        t.penup()
        t.goto(x, y)
        t.setheading(ray * 360 / rays)
        t.forward(size * 0.25)
        t.pendown()
        if ray % 2 == 0:
            t.color(color1)
        else:
            t.color(color2)
        t.forward(size * 0.75)
        t.penup()
        t.forward(size * 0.12)
        t.dot(6)

    # A bright flash in the middle
    t.goto(x, y)
    t.dot(14, "white")

Five fireworks

Each call gives a position, a size, how many rays and two colours. Bigger bursts get more rays so their sparks stay about the same distance apart.

burst(-150, 95, 105, 24, "deeppink", "gold")
burst(95, 115, 90, 20, "deepskyblue", "white")
burst(215, -5, 70, 16, "lawngreen", "yellow")
burst(-20, -20, 65, 16, "orange", "red")
burst(-235, -60, 50, 12, "violet", "white")

A city skyline

range(-300, 300, 40) gives the left edge of each building and random.randint(35, 100) picks its height. Drawing the skyline last puts it in front of the trails.

for left in range(-300, 300, 40):
    height = random.randint(35, 100)
    t.penup()
    t.goto(left, -215)
    t.pendown()
    t.begin_fill()
    t.goto(left + 40, -215)
    t.goto(left + 40, -215 + height)
    t.goto(left, -215 + height)
    t.goto(left, -215)
    t.end_fill()

Make it yours

Change the code above and press Run. Some ideas:

  • Add a sixth firework with burst(0, 170, 45, 12, "cyan", "white") before the skyline.
  • Change random.seed(2026) to another number for a different skyline and different stars.
  • Change t.pensize(2) to t.pensize(4) for thicker, brighter rays.

What it uses

Every turtle command the site supports is listed on Python turtle online.