当前位置:网站首页>[fireworks in the sky] it's beautiful to light up the night sky with gorgeous fireworks. A programmer brought a fireworks show to pay New Year's greetings to everyone~

[fireworks in the sky] it's beautiful to light up the night sky with gorgeous fireworks. A programmer brought a fireworks show to pay New Year's greetings to everyone~

2022-06-10 23:58:00 Programmer pear

Preface

  author :“ Programmer pear ”

** The article brief introduction **: This article mainly explains how to make a new year's interface fireworks special effect .

** Article source code access **: In order to thank everyone who pays attention to me, the project source code of each article is distributed free of charge

Enjoy drops

Click here for the blue line font , If you need any source code, remember to say the title and name ! I can also !

Welcome friends give the thumbs-up 、 Collection 、 Leaving a message.

Text

Setting off fireworks is one of our customs to welcome the new year and bid farewell to the old , However , Large amount of discharge behavior , Will greatly reduce the air quality , build

Air pollution is accompanied by fire 、 Personal injury and other potential safety hazards . So fireworks have been banned in most areas since very early ~

It is already the peak period of construction , Many of my friends may have never seen fireworks , Nah, Nah, NAH Xiaobian will use code to release  

The fireworks , Let's have enough fireworks at one time

 

One 、 Effect display

Two 、 Source code display

1) Function implementation uses Python The library includes :tkinter、PIL、time、random、math, If you haven't installed a third before

Fangku's words , Use  pip install + Module name   Just install it .

2) First , We use tkinter To create a canvas

root = tk.Tk()
#  Draw a canvas
cv = tk.Canvas(root, height=457, width=690)
#  Background map
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()

3) Next, we'll implement the effect of fireworks display and display it on the canvas . Let's define a fireworks class first fireworks, The main package in the class

enclosed : Initialization method and update data method .

The main parameters of initialization method include : Fireworks bloom axis 、 Speed 、 Color 、 The number of particles and time, etc , The code implementation is 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
 #  Exorcism  x  Shaft speed
 self.vx = vx
 #  Exorcism  y  Shaft speed
 self.vy = vy
 #  Number of blooming particles
 self.total = total
 #  Has stayed for a long time
 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

4) When the fireworks are set off, they need to be refreshed , Take a look at the update method , The code implementation is as follows :

def update(self, dt):
 self.age += dt
 #  Particles expand
 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

5) Next, let's look at the realization of fireworks display , The main elements include : The number of fireworks 、 The scope and speed of the explosion 、 Dwell time and brush

New time, etc , The code implementation is as follows :

def ignite(cv):
    t = time()
    #  Fireworks list
    explode_points = []
    wait_time = randint(10, 100)
    #  The number of explosions
    numb_explode = randint(6, 10)
    for point in range(numb_explode):
        #  List of explosive particles
        objects = []
        #  The explosion  x  Axis
        x_cordi = randint(50, 550)
        #  The explosion  y  Axis
        y_cordi = randint(50, 150)
        speed = uniform(0.5, 1.5)
        size = uniform(0.5, 3)
        color = choice(colors)
        #  The speed of the explosion
        explosion_speed = uniform(0.2, 1)
        #  The particle number radius of the 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 the particle list to the fireworks list
        explode_points.append(objects)
    total_time = .0
    #  stay  1.8  Keep up to date in seconds
    while total_time < 1.8:
        #  Pause the screen  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 the fireworks
            for item in point:
                #  Update time
                item.update(dt)
        #  Refresh the page
        cv.update()
        total_time += dt
    root.after(wait_time, ignite, cv)

 

summary

So many fireworks .gif Enough to see You can also make your own code , Come what you want ~

Follow Xiaobian for more wonderful content ! Remember to click on the portal

Remember Sanlian ! If you need packaged source code + Free sharing of materials ! Portal

原网站

版权声明
本文为[Programmer pear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102242248433.html