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.

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)tot.pensize(4)for thicker, brighter rays.
What it uses
- functions with parameters
- for loops
- random with a seed
- range with a step
Every turtle command the site supports is listed on Python turtle online.