【Python烟花】不到一个月就过年啦,和胖虎一起放烟花喽

在这里插入图片描述
嗨嗨,朋友们,离新年是越来越近了
胖虎在这给大家放烟花提前拜年啦

咱年前也得整一场烟花不是?
用电脑开整

虽然不是很好看哈哈,但是也能将就一下咯
背景图片、音乐、文字都是可以自己修改的哦~

这是效果,大家请看

请添加图片描述

首先咱得导入库

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

再来实现代码

源码可领取

随机生成颜色


def randomcolor():
    #生成随机颜色
    colArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
    color = ""
    for i in range(6):
        color += colArr[random.randint(0,14)]
    return "#"+color

GRAVITY = 0.06
#重力变量
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen','indigo', 'cornflowerblue', 'pink']
#颜色列表

编辑它的属性

Generic class for particles
particles are emitted almost randomly on the sky, forming a round of circle (a star) before falling and getting removed
from canvas
Attributes(属性):
    - id: 粒子的id
    - x, y: 粒子的坐标
    - vx, vy: 粒子在对应坐标的变化速度
    - total:一颗烟花里的粒子总数
    - age: 粒子在画布上停留的时间
    - color: 自我移植
    - cv: 画布
    - lifespan: 粒子在画布上停留的时间

控制粒子运动速度

咱可以自己更改“新年快乐”哦~


class part:
#为每一个烟花绽放出来的粒子单独构建一个类的对象 ,每个粒子都会有一些重要的属性,决定它的外观(大小、颜色)、移动速度等
    def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx = 0., vy = 0., size=3., color = 'red', lifespan = 2, **kwargs):
        self.id = idx
        #每个烟花的特定标识符
        self.x = x
        #烟花绽放x轴
        self.y = y
        #烟花绽放y轴
        self.initial_speed = explosion_speed
        #粒子初始速度
        self.vx = vx
        #粒子运动x轴速度
        self.vy = vy
        #粒子运动y轴速度
        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, outline='white',width=0.01)
        #指定一个限定矩形(Tkinter 会自动在这个矩形内绘制一个椭圆)
        self.lifespan = lifespan
        #粒子在画布上停留的时间

控制粒子的xy轴移动位移

如果粒子仅炸开而不扩张(只是停留时间足够,说明膨胀到最大了),则自由坠落

        elif self.alive():
            columnFont = ('华文行楷',14)
            self.cv.create_text(250, 100, text='新',tag="write_tag", fill=choice(colors),font = columnFont) #字体
            self.cv.create_text(300, 100,  text='年',tag="write_tag", fill=choice(colors),font = columnFont)
            self.cv.create_text(350, 100, text='快',tag="write_tag", fill=choice(colors),font = columnFont)
            self.cv.create_text(400, 100,  text='乐',tag="write_tag", fill=choice(colors),font = columnFont)
            #删除文字标签
            move_x = cos(radians(self.id*360/self.total))
            #x轴的移动位移
            # we technically don't need to update x, y because move will do the job
            self.cv.move(self.cid, self.vx + move_x, self.vy+GRAVITY*dt)
            self.vy += GRAVITY*dt
            #更新y轴

膨胀效果时间

判断膨胀时间是否小于1.2秒

    def expand (self):
        #定义膨胀效果时间帧
        return self.age <= 1.2

判断粒子是否仍在生命周期内

判断已停留时间是否小于应该停留时间

    def alive(self):
        #判断粒子是否仍在生命周期内
        return self.age <= self.lifespan

剩下代码

剩下的代码就留着下次炸

代码后面都有注释哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值