快到春节了,用 Python 实现一场烟花秀

快到春节了,用 Python 实现一场烟花秀,第1张

概述文|野客来源:Python技术「ID:pythonall」春节的脚步越来越近了,每逢春节很多地方都会燃放烟花来增添节日的气氛,然而因环境污染的加剧,近年来不少地方已经禁止燃放烟花了,为了弥补这个遗憾,本文我们来看一下如何使用Python来实现一场烟花秀。实现功能实现用到的Python库包

文 | 野客

来源:Python 技术「ID: pythonall」

春节的脚步越来越近了,每逢春节很多地方都会燃放烟花来增添节日的气氛,然而因环境污染的加剧,近年来不少地方已经禁止燃放烟花了,为了弥补这个遗憾,本文我们来看一下如何使用 Python 来实现一场烟花秀。

实现

功能实现用到的 Python 库包括:tkinter、PIL、time、random、math,如果之前没有装过第三库的话,使用 pip install pillow 装一下即可。

首先,我们使用 tkinter 来创建一个画布,可以根据自己的喜好选一张应景的图片作为背景,代码实现如下:

root = tk.Tk()# 绘制一个画布cv = tk.Canvas(root, height=457, wIDth=690)# 背景图image = Image.open("bg.jpeg")photo = ImageTk.PhotoImage(image)# 在画板上绘制一张图片cv.create_image(0, 0, image=photo, anchor='nw')cv.pack()

看一下效果:

接着我们来实现烟花燃放的效果并在画布上显示。先来定义一个烟花类 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 轴 self.x = x # 烟花绽放 x 轴 self.y = y self.initial_speed = explosion_speed # 外放 x 轴速度 self.vx = vx # 外放 y 轴速度 self.vy = vy # 绽放的粒子数 self.total = total # 已停留时间 self.age = 0 # 颜色 self.color = color # 画布 self.cv = cv self.cID = self.cv.create_oval(x - size, y - size, x + size, y + size, fill=self.color) self.lifespan = lifespan

当烟花燃放过后需要进行刷新,看一下更新方法,代码实现如下:

def update(self, dt): self.age += dt # 粒子膨胀 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) # 膨胀到最大下落 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 # 过期移除 elif self.cID is not None:  cv.delete(self.cID)  self.cID = None

再接着来看烟花燃放的实现,主要元素包括:烟花的个数、爆炸的范围和速度、停留时间和刷新时间等,代码实现如下:

def ignite(cv):    t = time()    # 烟花列表    explode_points = []    wait_time = randint(10, 100)    # 爆炸的个数    numb_explode = randint(6, 10)    for point in range(numb_explode):        # 爆炸粒子列表        objects = []        # 爆炸 x 轴        x_cordi = randint(50, 550)        # 爆炸 y 轴        y_cordi = randint(50, 150)        speed = uniform(0.5, 1.5)        size = uniform(0.5, 3)        color = choice(colors)        # 爆炸的绽放速度        explosion_speed = uniform(0.2, 1)        # 爆炸的粒子数半径        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))            # 添加进粒子列表里            objects.append(r)        # 把粒子列表添加到烟花列表        explode_points.append(objects)    total_time = .0    # 在 1.8 秒时间帧内保持更新    while total_time < 1.8:        # 让画面暂停 0.01s        sleep(0.01)        # 刷新时间        tnew = time()        t, dt = tnew, tnew - t        # 遍历烟花列表        for point in explode_points:            # 遍历烟花里的粒子列表            for item in point:                # 更新时间                item.update(dt)        # 刷新页面        cv.update()        total_time += dt    root.after(wait_time, ignite, cv)

最后,我们来看一下效果:

总结

本文我们使用 Python 实现了烟花燃放的特效,如果感兴趣的话,可以自动动手试试。

总结

以上是内存溢出为你收集整理的快到春节了,用 Python 实现一场烟花秀全部内容,希望文章能够帮你解决快到春节了,用 Python 实现一场烟花秀所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1188002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存