Programming can be an exciting and creative endeavor, especially when it involves simulating visually appealing phenomena like fireworks. In this article, we will delve into a complete Python code that simulates a fireworks display, exploring its components, functionality, and how you can run it to add a bit of sparkle to your coding projects.
The Basics of Our Fireworks Simulation
Our fireworks simulation will leverage Python’s turtle
graphics library, which is part of the standard Python library and thus does not require any additional installations. The turtle
module provides a simple way to create graphics and animations, making it an ideal tool for our project.
The Complete Code
Below is the complete Python code for our fireworks simulation. This code initializes a screen, creates multiple fireworks that explode into a random pattern of colors, and then exits when all fireworks have finished their display.
pythonCopy Codeimport turtle
import random
import time
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Python Fireworks")
fireworks = []
class Firework:
def __init__(self, x, y, color):
self.particle_list = []
self.firework = turtle.Turtle()
self.firework.shape("circle")
self.firework.color(color)
self.firework.penup()
self.firework.goto(x, y)
self.firework.speed(0)
fireworks.append(self)
def explode(self):
particles = 50
for _ in range(particles):
t = turtle.Turtle()
t.shape("circle")
t.color(random.choice(["red", "orange", "yellow", "white", "blue", "green", "purple"]))
t.penup()
t.goto(self.firework.position())
t.speed(0)
angle = random.randint(0, 360)
distance = random.randint(50, 150)
dx = distance * turtle.cos(angle)
dy = distance * turtle.sin(angle)
t.setheading(angle)
self.particle_list.append([t, dx, dy])
def move_particles(self):
for particle in self.particle_list:
t = particle
dx = particle
dy = particle
t.goto(t.xcor() + dx/20, t.ycor() + dy/20)
particle = particle * 0.99
particle = particle * 0.99
def main():
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
for _ in range(10):
x = random.randint(-300, 300)
y = random.randint(-300, 300)
color = random.choice(colors)
f = Firework(x, y, color)
time.sleep(0.5)
f.explode()
screen.update()
time.sleep(2)
running = True
while running:
screen.update()
for firework in fireworks:
firework.move_particles()
time.sleep(0.02)
turtle.done()
if __name__ == "__main__":
main()
How It Works
1.Initialization: The code starts by setting up the screen and defining a Firework
class to manage individual fireworks.
2.Firework Creation: It then creates several fireworks at random positions with random colors.
3.Explosion: Each firework explodes into particles that move away from the explosion center in random directions.
4.Animation: The particles move in their respective directions, simulating the dispersing effect of a firework explosion.
5.Cleanup: The simulation ends after a short delay, allowing all particles to disperse before closing.
Running the Code
To run this code, ensure you have Python installed on your machine. Copy the code into a Python file (e.g., fireworks.py
), open a terminal or command prompt, navigate to the file’s directory, and run it using the command python fireworks.py
.