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! ππβ¨
