SoFunction
Updated on 2024-12-16

Writing a New Year's Fireworks Show with Python

The Python modules we use include:tkinterPILtimerandommath, if the third-party module is not installedpip install A quick look will do, see the code implementation below.

1. Introduction to the library

import tkinter as tk
from PIL import Image, ImageTk
from time import time, sleep
from random import choice, uniform, randint
from math import sin, cos, radians

2. Fireworks color

colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']

3. Define the fireworks class

class fireworks:
    def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx=0., vy=0., size=2., color='red', lifespan=2, **kwargs):
         = idx
        # Fireworks in bloom x-axis
         = x
        # Fireworks in bloom x-axis
         = y
        self.initial_speed = explosion_speed
        # Outgoing x-axis velocity
         = vx
        # Outgoing y-axis velocity
         = vy
        # Number of particles in bloom
         = total
        # of time spent
         = 0
        # Color
         = color
        # Canvas
         = cv
         = .create_oval(x - size, y - size, x + size, y + size,
        fill=)
         = lifespan

    # Update data
    def update(self, dt):
         += dt
        # Particle expansion
        if () and ():
            move_x = cos(radians( * 360 / )) * self.initial_speed
            move_y = sin(radians( * 360 / )) * self.initial_speed
            (, move_x, move_y)
             = move_x / (float(dt) * 1000)
        # Swell to maximum drop
        elif ():
            move_x = cos(radians( * 360 / ))
            (,  + move_x,  + 0.5 * dt)
             += 0.5 * dt
        # Expired removal
        elif  is not None:
            ()
             = None

    # Define the time frame for the swell effect
    def expand(self):
        return  <= 1.5

    # Check if the particle is still in its life cycle
    def alive(self):
        return  <= 

4. Fireworks

def ignite(cv):
    t = time()
    # Fireworks list
    explode_points = []
    wait_time = randint(10, 100)
    # of explosions
    numb_explode = randint(6, 10)
    for point in range(numb_explode):
        # Exploding particle list
        objects = []
        # Explosion x-axis
        x_cordi = randint(50, 550)
        # Explosion y-axis
        y_cordi = randint(50, 150)
        speed = uniform(0.5, 1.5)
        size = uniform(0.5, 3)
        color = choice(colors)
        # Explosive bloom speed
        explosion_speed = uniform(0.2, 1)
        # of particles exploded radius
        total_particles = randint(10, 50)
        for i in range(1, total_particles):
            r = fireworks(cv, idx=i, total=total_particles, explosion_speed=explosion_speed, x=x_cordi, y=y_cordi,
                     vx=speed, vy=speed, color=color, size=size,
                     lifespan=uniform(0.6, 1.75))
            # Add to the particle list
            (r)
        # Add the particle list to the fireworks list
        explode_points.append(objects)
    total_time = .0
    # Updates are maintained in 1.8 second time frames
    while total_time < 1.8:
        # Pause the screen 0.01s
        sleep(0.01)
        # Refresh time
        tnew = time()
        t, dt = tnew, tnew - t
        # Traversing the list of fireworks
        for point in explode_points:
            # Iterate over the list of particles in the fireworks
            for item in point:
                # Update Time
                (dt)
        # Refresh the page
        ()
        total_time += dt
    (wait_time, ignite, cv)

5. Activation

if __name__ == "__main__":
    root = ()
    # Draw a canvas
    cv = (root, height=400, width=600)
    # Background image
    image = ("")
    photo = (image)
    # Draw a picture on the drawing board
    cv.create_image(0, 0, image=photo, anchor='nw')
    ()
    (close)
    (100, ignite, cv)
    # Generate windows
    ()

Look at the effect: