The realization code is as follows:
# -*- coding: utf-8 -*- import math, random,time import threading import tkinter as tk import re #import uuid Fireworks=[] maxFireworks=8 height,width=600,600 class firework(object): def __init__(self,color,speed,width,height): #uid=uuid.uuid1() =(2,4) #Particle radius of 2~4 pixels =color #Particle color =speed #speed is 1.5-3.5 seconds =0 # In case of non-exploding fireworks, status=0; after exploding, status>=1; when status>100, the life of the fireworks is terminated =(20,30) # of particles =[(0,width-1),(0,height-1)] #Fireworks random center coordinates =[] # Raw particle coordinates (at 100% state) =(0,2*) #Angle of rotation of the elliptic plane #Ellipse parametric equation: x=a*cos(theta),y=b*sin(theta) #ellipsePara=[a,b] =[(30,40),(20,30)] theta=2*/ for i in range(): t=(-1.0/16,1.0/16) # Generate a random number [-1/16,1/16) x,y=[0]*(theta*i+t), [1]*(theta*i+t) #Elliptic parametric equations xx,yy=x*()-y*(), y*()+x*() # Plane rotation equations ([xx,yy]) =[0:] #Current particle coordinates =(target=) #Creating thread objects def extend(self): #Particle swarm state change function threads for i in range(100): +=1 #Update the status logo =[[one[0]*/100, one[1]*/100] for one in ] # Update particle swarm coordinates (/50) def explode(self): (True) # Make this thread a daemon thread () #Starting threads def __repr__(self): return ('color:{color}\n' 'speed:{speed}\n' 'number of particle: {np}\n' 'center:[{cx} , {cy}]\n' 'ellipse:a={ea} , b={eb}\n' 'particle:\n{p}\n' ).format(color=,speed=,np=,cx=[0],cy=[1],p=str(),ea=[0],eb=[1]) def colorChange(fire): rgb=(r'(.{2})',[1:]) cs= f=lambda x,c: hex(int(int(x,16)*(100-c)/30))[2:] #Color begins to decay linearly when particle lifetime reaches 70% if cs>70: ccr,ccg,ccb=f(rgb[0],cs),f(rgb[1],cs),f(rgb[2],cs) else: ccr,ccg,ccb=rgb[0],rgb[1],rgb[2] return '#{0:0>2}{1:0>2}{2:0>2}'.format(ccr,ccg,ccb) def appendFirework(n=1): # Recursively generate fireworks objects if n>maxFireworks or len(Fireworks)>maxFireworks: pass elif n==1: cl='#{0:0>6}'.format(hex(int((0,16777215)))[2:]) # Generate a random number from 0 to 16777215 (0xFFFFFF) as the random color a=firework(cl,(1.5,3.5),width,height) ( {'particle':a,'points':[]} ) # Create a list of particle displays, with 'particle' as a fireworks object and 'points' as the set of object variables for each particle displayed () else: appendFirework() appendFirework(n-1) def show(c): for p in Fireworks: # Every time you refresh the display, delete all the existing particles first. for pp in p['points']: (pp) for p in Fireworks: # Calculate the display object for each particle in each firework object oneP=p['particle'] if ==100: # A status mark of 100 indicates the end of firework life (p) #Move out of current fireworks appendFirework() # Add a new firework continue else: li=[[int(cp[0]*2)+[0],int(cp[1]*2)+[1]] for cp in ] # Translate the ellipse with the center at the origin to the coordinates of the center of a random circle color=colorChange(oneP) # Calculate the current color based on the current state of the firework for pp in li: p['points'].append(c.create_oval(pp[0]-, pp[1]-, pp[0]+, pp[1]+, fill=color)) # Drawing each particle of a firework (50, show,c) #callbacks, refreshed every 50ms if __name__=='__main__': appendFirework(maxFireworks) root = () cv = (root, height=height, width=width) cv.create_rectangle(0, 0, width, height, fill="black") () (50, show,cv) ()
Photo Gallery:
to this article on python fireworks detailed code is introduced to this article, more related python fireworks code content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!