Madras Academy
Home / Blog / html / Create a Fireworks Animation with Python Turtle
html

Create a Fireworks Animation with Python Turtle

March 1, 2025 admin 3 min read

Want to create a stunning fireworks display using Python? In this tutorial, we’ll build a randomized fireworks animation using the Turtle graphics module. πŸŽ‡

This project is perfect for beginners looking to explore animation, randomness, and color effects while creating something fun and visually appealing.


What You’ll Learn

βœ… Setting up the Turtle graphics window for animations πŸ–₯️
βœ… Drawing random fireworks bursts with dynamic particle sizes πŸŽ‡
βœ… Using loops to create multiple firework effects 🌟
βœ… Adding random colors and angles for a realistic look 🎯
βœ… Customizing speed, background, and visuals for an eye-catching display πŸŽ†


Step 1: Import Required Modules

We’ll need Turtle for drawing and Random to create dynamic fireworks bursts.

import turtle
import random

Step 2: Set Up the Screen

We create a black background to mimic the night sky.

# Setup the screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Fireworks Animation")

Step 3: Create the Fireworks Turtle

We’ll create a turtle to draw the fireworks bursts and hide it for a cleaner effect.

# Create the turtle
firework = turtle.Turtle()
firework.hideturtle()
firework.speed(0) # Fastest speed

Step 4: Function to Draw a Single Firework Burst

A firework burst consists of random particles moving outward in different directions.

def draw_firework(x, y):
firework.penup()
firework.goto(x, y)
firework.pendown()

# Random burst size and number of particles
num_particles = random.randint(20, 50)
burst_size = random.randint(50, 150)

# Random color for each burst
color = (random.random(), random.random(), random.random())
firework.color(color)

for _ in range(num_particles):
angle = random.uniform(0, 360) # Random direction
distance = random.uniform(20, burst_size) # Random spread

firework.penup()
firework.goto(x, y)
firework.setheading(angle)
firework.pendown()
firework.forward(distance)
firework.penup()

βœ” Generates random-sized fireworks bursts πŸŽ†
βœ” Creates colorful explosions with random angles 🌈
βœ” Uses loops to draw multiple particles per firework ✨


Step 5: Simulating Multiple Fireworks

We’ll create multiple fireworks bursts at random positions.

def fireworks_show(num_fireworks):
for _ in range(num_fireworks):
# Random position for each firework burst
x = random.randint(-300, 300)
y = random.randint(-300, 300)
draw_firework(x, y)

βœ” Generates multiple fireworks at different locations
βœ” Creates a dynamic and lively display
βœ” Each firework burst has random colors, angles, and sizes


Step 6: Final Setup and Running the Animation

# Set up color mode for RGB values
turtle.colormode(1.0) # Use 0-1 range for RGB colors

# Run the fireworks show with 10 bursts
fireworks_show(10)

# Keep the window open
turtle.done()

βœ” Sets up RGB mode for vibrant colors 🌈
βœ” Runs the fireworks animation with 10 bursts πŸŽ‡
βœ” Keeps the window open to view the display


How It Works πŸŽ₯

πŸŽ† Generates random firework bursts at different locations
🎨 Uses RGB colors for vibrant effects
πŸ”„ Each burst consists of multiple outward-moving particles
🌟 Creates a lively, realistic fireworks display


Final Output: A Stunning Fireworks Show!

When you run the script, you’ll see multiple colorful fireworks bursts that appear randomly on the screen. πŸŽ†βœ¨

πŸ’‘ Want to customize it? Try these:
βœ” Change burst size and number of particles for bigger fireworks πŸ”₯
βœ” Adjust colors, angles, and animation speed for a unique effect 🌈
βœ” Add sound effects for a realistic experience πŸ”Š


Watch the Full Video Tutorial πŸŽ₯

See the step-by-step process and live demonstration of the fireworks animation!


Download the Full Code

Get the complete source code on GitHub:

πŸ”— GitHub – Fireworks Animation


Conclusion

In this tutorial, we created a beautiful fireworks animation using Python Turtle graphics and randomization techniques.

πŸ’‘ Next Steps:
βœ” Try different burst effects and colors 🎨
βœ” Experiment with fading animations for a realistic touch πŸ”₯
βœ” Save the animation as a GIF or video for digital art πŸŽ₯


Did you enjoy this tutorial?

πŸ’‘ Like this post
πŸ’¬ Comment your thoughts
πŸ”” Subscribe for more Python animation projects!

πŸš€ Happy Coding! πŸŽ†πŸβœ¨

RECOMMENDED COURSES

Continue learning with Madras Academy.

Here are a few courses selected from our current catalog.

Browse All Courses
W WhatsApp