In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about python fireworks example code how to write, the editor thinks it is very practical, so share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.
The implementation code is as follows:
#-*-coding: utf-8-*-import math, random,timeimport threadingimport tkinter as tkimport re#import uuidFireworks= [] maxFireworks=8height,width=600600class firework (object): def _ _ init__ (self,color,speed,width) Height): # uid=uuid.uuid1 () self.radius=random.randint (2Magne4) # Particle radius is 2mm 4 pixel self.color=color # Particle color self.speed=speed # speed is 1.5-3.5s self.status=0 # without fireworks exploding Status=0 After the explosion, status > = 1 When status > 100, The end of the life cycle of fireworks self.nParticle=random.randint (20jin30) # number of particles self.center= [random.randint (0minWidthMuth1), random.randint (0 Height-1)] # Random central coordinates of fireworks self.oneParticle= [] # original particle coordinates (100% state) self.rotTheta=random.uniform (0memmath.pi) # Elliptic plane rotation angle # Elliptic parameter equation: x=a*cos (theta), y=b*sin (theta) # ellipsePara= [aforb] self.ellipsePara= [random.randint (30Magne40) Random.randint (20meme30)] theta=2*math.pi/self.nParticle for i in range (self.nParticle): t=random.uniform (- 1.0Accord16) # produces a random number of [- 1Charger 16gamma 1px16) which is a random number of [1] * math.cos (theta*i+t), self.ellipsePara [1] * math.sin (theta*i+t) # elliptic parameter equation xx Yy=x*math.cos (self.rotTheta)-y*math.sin (self.rotTheta), y*math.cos (self.rotTheta) + x*math.sin (self.rotTheta) # plane rotation equation self.oneParticle.append ([xx Yy]) self.curParticle=self.oneParticle [0:] # current particle coordinates self.thread=threading.Thread (target=self.extend) # create thread object def extend (self): # particle swarm state change function thread for i in range: self.status+=1 # update status ID self. CurParticle= [[one [0] * self.status/100] One [1] * self.status/100] for one in self.oneParticle] # Update the particle swarm coordinates time.sleep (self.speed/50) def explode (self): self.thread.setDaemon (True) # set the current routine to daemon thread self.thread.start () # start thread 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=self.color,speed=self.speed,np=self.nParticle,cx=self.center [0], cy=self.center [1], p=str (self.oneParticle), ea=self.ellipsePara [0], eb=self.ellipsePara [1]) def colorChange (fire): rgb=re.findall (r'(. {2})' Fire.color [1:]) cs=fire.status f=lambda XMagi c: hex (int (int (xmem16) * (100m c) / 30)) [2:] # when the particle lifespan reaches 70% Color starts linear attenuation 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 (nimble 1): # Recursively generate fireworks objects if n > maxFireworks or len (Fireworks) > maxFireworks: pass elif nimble objects 1: cl='# {0:0 > 6} '.format (hex (int (random.randint (0Ling 16777215) [2:]) # generates a random number of zero 16777215 (0xFFFFFF) As a random color a=firework (cl,random.uniform (1.5 particle':a,'points': 3.5), width,height) Fireworks.append ({'particle':a,'points': []}) # set up a particle display list,' particle' is a fireworks object 'points' is the object variable set a.explode () else: appendFirework () appendFirework (nMel 1) def show (c): for p in Fireworks: # each time the display is refreshed for each particle display First delete all the existing particles for pp in p ['points']: c.delete (pp) for p in Fireworks: # based on each fireworks object, calculate the display object oneP=p [' particle'] if oneP.status==100: # status of each particle is marked as 100 Indicates the end of fireworks life Fireworks.remove (p) # remove the current fireworks appendFirework () # add a fireworks continue else: li= [[int (cp [0] * 2) + oneP.center [0] Int (cp [1] * 2) + oneP.center [1]] for cp in oneP.curParticle] # translate the ellipse centered at the origin to the random center coordinate color=colorChange (oneP) # calculate the current color for pp in li: P ['points'] .append (c.create_oval (pp [0]-oneP.radius, pp [1]-oneP.radius) based on the current state of the fireworks Pp [0] + oneP.radius, pp [1] + oneP.radius, fill=color) # draw fireworks each particle root.after (50, show,c) # callback Per 50ms refresh if _ name__=='__main__': appendFirework (maxFireworks) root = tk.Tk () cv = tk.Canvas (root, height=height, width=width) cv.create_rectangle (0,0, width, height, fill= "black") cv.pack () root.after (50, show,cv) root.mainloop ()
Picture display:
The above is how to write the example code of fireworks in python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.