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

Time:2021-2-26

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

realization

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):
        self.id = 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
        self.total = total
        #Length of stay
        self.age = 0
        #Color
        self.color = color
        #Canvas
        self.cv = cv
        self.cid = self.cv.create_oval(x - size, y - size, x + size, y + size,
        fill=self.color)
        self.lifespan = lifespan

    #Update data
    def update(self, dt):
        self.age += dt
        #Particle expansion
        if self.alive() and self.expand():
            move_x = cos(radians(self.id * 360 / self.total)) * self.initial_speed
            move_y = sin(radians(self.id * 360 / self.total)) * self.initial_speed
            self.cv.move(self.cid, move_x, move_y)
            self.vx = move_x / (float(dt) * 1000)
        #Expand to maximum drop
        elif self.alive():
            move_x = cos(radians(self.id * 360 / self.total))
            self.cv.move(self.cid, self.vx + move_x, self.vy + 0.5 * dt)
            self.vy += 0.5 * dt
        #Expired removal
        elif self.cid is not None:
            cv.delete(self.cid)
            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
            objects.append(r)
        #Add particle list to fireworks list
        explode_points.append(objects)
    total_time = .0
    #Keep updated in 1.8 second time frame
    while total_time < 1.8:
        #Pause the screen for 0.01s
        sleep(0.01)
        #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
                item.update(dt)
        #Refresh page
        cv.update()
        total_time += dt
    root.after(wait_time, ignite, cv)

 

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

 

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 […]