The pace of the Spring Festival is getting closer and closer. During the Spring Festival, many places will set off fireworks to increase the festival atmosphere. However, due to the intensification of environmental pollution, fireworks have been banned in many places in recent years. In order to make up for this regret, in this article, we will take a look at how to use Python to realize a fireworks show.
realization
Python libraries used for function implementation include tkinter, PIL, time, random and math. If the third library has not been installed before, use PIP install pilot to install it.
First, we use tkinter to create a canvas. We can choose a suitable picture as the background according to our preferences. The code is as follows:
root = tk.Tk() # Draw a canvas CV = TK Canvas(root, height=457, width=690) # Background image = image open("bg.jpeg") photo = ImageTk.PhotoImage(image) # Draw a picture on the drawing board cv.create_image(0, 0, image=photo, anchor='nw') cv.pack()
Look at the effect
Then we will realize the effect of fireworks and display it on the canvas. First, define a fireworks class fireworks, which mainly includes initialization methods and update data methods. Finally, if your time is not very tight and you want to improve python quickly, the most important thing is not afraid of hardship. I suggest you can fight ♥ Xin (homonym): 276 3177 065, that's really good. Many people make rapid progress. You need to be not afraid of hardship! You can add it and have a look~
The main parameters of the initialization method include: fireworks blooming coordinate axis, speed, color, particle number and time, etc. the code is implemented as follows:
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 # Fireworks bloom x-axis self.x = x # Fireworks bloom x-axis self.y = y self.initial_speed = explosion_speed # External x-axis speed self.vx = vx # External y-axis speed self.vy = vy # Number of particles in bloom self.total = total # Dwell time self.age = 0 # colour 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
After the fireworks are set off, you need to refresh. Take a look at the update method. The code implementation is as follows:
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
Next, let's look at the implementation of fireworks discharge. The main elements include: the number of fireworks, the range and speed of explosion, residence time and refresh time. The code implementation is as follows:
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) # Explosion y-axis y_cordi = randint(50, 150) speed = uniform(0.5, 1.5) size = uniform(0.5, 3) color = choice(colors) # Explosion blooming speed explosion_speed = uniform(0.2, 1) # Number of particles and radius 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 particle list objects.append(r) # Add particle list to fireworks list explode_points.append(objects) total_time = .0 # Keep updated within 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 the fireworks list for point in explode_points: # Traverse 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)
Finally, let's look at the effect:
summary
In this article, we use Python to realize the special effects of fireworks. If you are interested, you can try it automatically
How to receive python benefits:
1. Like + comment (check "forward at the same time")
2. Pay attention to Xiaobian. And reply to keyword [19] by private letter
(be sure to send a private message ~ click my avatar to see the private message button)