2024跨年烟花代码,用Python送自己一场浪漫烟花秀 您所在的位置:网站首页 返老还童电影港片 2024跨年烟花代码,用Python送自己一场浪漫烟花秀

2024跨年烟花代码,用Python送自己一场浪漫烟花秀

2024-01-09 03:49| 来源: 网络整理| 查看: 265

前言

2023 已经接近尾声了,2024 即将到来,本文我们用 Python 送你一场跨年烟花秀。 img

我们用到的 Python 模块包括:tkinter、PIL、time、random、math,如果第三方模块没有装的话,pip install 一下即可,下面看一下代码实现。

导库

import tkinter as tk from PIL import Image, ImageTk from time import time, sleep from random import choice, uniform, randint from math import sin, cos, radians

烟花颜色

colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue'] 定义烟花类 class 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 expand(self): return self.age


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有