Some areas have banned fireworks, programmers can only use Python code to achieve an environmentally friendly and pollution-free fireworks show


The text and pictures of this article are from the Internet, only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time

This article is from Tencent cloud by Python sophomore
Fireworks, invented by ancient Chinese people, are often used in grand ceremonies or performances. They are also set off on New Year’s Eve and the Lantern Festival to set off the festival atmosphere. In recent years, with the aggravation of environmental pollution, some areas have banned fireworks, so we use Python to achieve a pollution-free fireworks show.

Environmental Science

  • Operating system: Windows
  • Python version: 3.6
  • Modules involved: Tkinter, PIL, time, random, math


Import 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


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


Define fireworks
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
        #X-axis of fireworks blooming
        self.x = x
        #X-axis of fireworks blooming
        self.y = y
        self.initial_speed = explosion_speed
        #X-axis velocity
        self.vx = vx
        #Velocity of y-axis
        self.vy = vy
        #Number of blooming particles = total
        #Length of stay
        self.age = 0
        self.color = color
        #Canvas = cv
        self.cid = - size, y - size, x + size, y + size,
        self.lifespan = lifespan

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

    #Defines the time frame of the bulge effect
    def expand(self):
        return self.age <= 1.5

    #Check that the particles are still in the life cycle
    def alive(self):
        return self.age <= self.lifespan


def ignite(cv):
    t = time()
    #Fireworks list
    explode_points = []
    wait_time = randint(10, 100)
    #Number of explosions
    numb_explode = randint(6, 10)
    for point in range(numb_explode):
        #Explosive particle list
        objects = []
        #Explosion X-axis
        x_cordi = randint(50, 550)
        #Explosive y-axis
        y_cordi = randint(50, 150)
        speed = uniform(0.5, 1.5)
        size = uniform(0.5, 3)
        color = choice(colors)
        #The blooming speed of explosion
        explosion_speed = uniform(0.2, 1)
        #The radius of particle number of explosion
        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
        #Add particle list to fireworks list
    total_time = .0
    #Keep updated in 1.8 second time frame
    while total_time < 1.8:
        #Pause the screen for 0.01s
        #Refresh time
        tnew = time()
        t, dt = tnew, tnew - t
        #Traverse fireworks list
        for point in explode_points:
            #Traversing the list of particles in fireworks
            for item in point:
                #Update time
        #Refresh page
        total_time += dt
    root.after(wait_time, ignite, cv)


if __name__ == "__main__":
    root = tk.Tk()
    #Draw a canvas
    cv = tk.Canvas(root, height=400, width=600)
    #Background image
    image ="bg.jpg")
    photo = ImageTk.PhotoImage(image)
    #Draw a picture on the drawing board
    cv.create_image(0, 0, image=photo, anchor='nw')
    root.after(100, ignite, cv)
    #Generation window


The final effect is shown in the figure

Recommended Today

Practice analysis of rust built-in trait: partialeq and EQ

Abstract:Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. This article is shared from Huawei cloud community《Analysis of rust built-in trait: partialeq and EQ》Author: debugzhang Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. Some traits can be automatically […]