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! 🎆🐍✨