圣诞节快来了~用python做一个粒子烟花震撼众人赚个女孩回来吧~

前言

嗨喽,大家好呀~这里是爱看美女的茜茜呐

又到了学Python时刻~

圣诞节快来了~用python做一个粒子烟花震撼众人赚个女孩回来吧~

; 准备

准备一下你运行效果的背景图

圣诞节快来了~用python做一个粒子烟花震撼众人赚个女孩回来吧~

以及一首你喜欢或那你女朋友喜欢的音乐

圣诞节快来了~用python做一个粒子烟花震撼众人赚个女孩回来吧~

效果

圣诞节快来了~用python做一个粒子烟花震撼众人赚个女孩回来吧~

圣诞节快来了~用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: 粒子在画布上停留的时间

”’

PS:本篇完整源码如有需要的小伙伴可以加下方的群去找管理员免费领取

圣诞节快来了~用python做一个粒子烟花震撼众人赚个女孩回来吧~

为每一个烟花绽放出来的粒子单独构建一个类的对象 ,

每个粒子都会有一些重要的属性,决定它的外观(大小、颜色)、移动速度等

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

烟花绽放x轴

        self.x = x

烟花绽放y轴

        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

指定一个限定矩形(Tkinter 会自动在这个矩形内绘制一个椭圆)

        self.cid = self.cv.create_oval(x - size, y - size, x + size,y + size, fill=self.color, outline='white',width=0.01)

粒子在画布上停留的时间

        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():
            columnFont = ('华文行楷',40)

            self.cv.create_text(150, 80, text='圣',tag="write_tag", fill=choice(colors),font = columnFont)
            self.cv.create_text(250, 80,  text='诞',tag="write_tag", fill=choice(colors),font = columnFont)
            self.cv.create_text(350, 80, text='快',tag="write_tag", fill=choice(colors),font = columnFont)
            self.cv.create_text(450, 80,  text='乐',tag="write_tag", fill=choice(colors),font = columnFont)

            move_x = cos(radians(self.id*360/self.total))

            self.cv.move(self.cid, self.vx + move_x, self.vy+GRAVITY*dt)
            self.vy += GRAVITY*dt

        elif self.cid is not None:

            cv.delete(self.cid)

            self.cv.delete("write_tag")

            self.cid = None
    def expand (self):

        return self.age  1.2

    def alive(self):

        return self.age  self.lifespan

'''
Firework simulation loop:
Recursively call to repeatedly emit new fireworks on canvas
a list of list (list of stars, each of which is a list of particles)
is created and drawn on canvas at every call,
via update protocol inside each 'part' object
'''

可以在这一段修改烟花的位置

def simulate(cv):

    t = time()

    explode_points = []

    wait_time = randint(10,100)

    numb_explode = randint(8,20)

    for point in range(numb_explode):

        if point6:
            objects = []

            x_cordi = 150 + point*50

            y_cordi = 80

            speed = uniform (0.5, 5.5)

            size = uniform (0.5,3)

            color = choice(colors)

            explosion_speed = uniform(0.6, 4)

            total_particles = randint(10,50)

            for i in range(2,total_particles):

                r = part(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.7,1.75))

                objects.append(r)

            explode_points.append(objects)

        else:
            objects = []

            x_cordi = randint(50,550)

            y_cordi = randint(50, 550)

            speed = uniform (0.5, 6.5)

            size = uniform (0.4,3)

            color = choice(colors)

            explosion_speed = uniform(0.3, 2)

            total_particles = randint(10,40)

            for i in range(2,total_particles):

                r = part(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

    while total_time < 2:

        sleep(0.03)

        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, simulate, cv)

def close(*ignore):

    """Stops simulation loop and closes the window."""
    global root
    root.quit()

if __name__ == '__main__':
    root = tk.Tk()
    root.title('圣诞快乐')
    cv = tk.Canvas(root, height=600, width=600)

    bgpath = filedialog.askopenfilename(title='请选择背景图片')

    image = Image.open(bgpath)

    image = image.resize((600,600), Image.ANTIALIAS)

    photo = ImageTk.PhotoImage(image)
    cv.create_image(0, 0, image=photo, anchor='nw')

    bgmusic = filedialog.askopenfilename(title='请选择背景音乐')
    py.mixer.init()

    py.mixer.music.load(bgmusic)

    py.mixer.music.play(-1, 0, fade_ms=50)

    py.mixer.music.pause()

    py.mixer.music.unpause()

    cv.pack()

    root.protocol("WM_DELETE_WINDOW", close)
    root.after(200, simulate, cv)

    root.mainloop()

尾语

感谢你观看我的文章呐~本次航班到这里就结束啦 🛬

希望本篇文章有对你带来帮助 🎉,有学习到一点知识~

躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。

最后,博主要一下你们的三连呀(点赞、评论、收藏),不要钱的还是可以搞一搞的嘛~

不知道评论啥的,即使扣个6666也是对博主的鼓舞吖 💞 感谢 💐

圣诞节快来了~用python做一个粒子烟花震撼众人赚个女孩回来吧~

Original: https://blog.csdn.net/m0_72282564/article/details/128303700
Author: 茜茜是帅哥
Title: 圣诞节快来了~用python做一个粒子烟花震撼众人赚个女孩回来吧~

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/723613/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球