网络知识 娱乐 Python表白代码:“ 星光月夜烟花皆归你,我也归你”(满天烟花盛开、附番外玫瑰)

Python表白代码:“ 星光月夜烟花皆归你,我也归你”(满天烟花盛开、附番外玫瑰)

 导语

"慢品人间烟火色 闲观人间岁月长"

                            ———致自己​图片​​

🌙 遇见我以后,我们的故事就开始了,愿你历经山河,仍觉得人间值得🌙。

 星光月夜烟花皆归你,我也归你。关于烟花🎇大家都​知道多少?有多少表白故事情节都发生在烟

花下,想必木木子👸不用说大家也知道叭~​​

今天这则小短文就是关于烟花的故事你准备好跟我一起进入烟花的世界了嘛?

图片


​正文

“每一句文案,都有一个故事,你仔细听”

图片

1)环境安装🎊

准备好:Python3、Pycharm、Tkinter、Pygame、Pillow以及一些自带模块。

安装命令统一:

 pip install -i https://pypi.douban.com/simple/ +模块名

2)素材展示🎨

2.1音乐背景(可自选)

2.2图片素材(可修改)

3)正式敲代码🎊

3.1导入库

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

3.2界面窗口设置

if __name__ == '__main__': 
    root = tk.Tk() 
    root.title('漫天烟花——祝大家—有情人终成眷属')  # 设置窗体的标题栏
    cv = tk.Canvas(root, height=600, width=600)
    #绘制一个高600,宽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)
    # 播放  第一个是播放值 -1代表循环播放, 第二个参数代表开始播放的时间
    py.mixer.music.pause() 
    #暂停
    py.mixer.music.unpause()
    #取消暂停
    cv.pack()
    #把cv添加进去
    root.protocol("WM_DELETE_WINDOW", close)
    root.after(200, simulate, cv)
    #在0.1秒后再调用stimulate函数,生成一轮烟花绽放效果
    root.mainloop()
    #执行root,生成窗口

3.3颜色设置随机

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']
#颜色列表
 

3.4主程序烟花

class part:
#为每一个烟花绽放出来的粒子单独构建一个类的对象 ,每个粒子都会有一些重要的属性,决定它的外观(大小、颜色)、移动速度等
    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
        #每个烟花的特定标识符
        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
        #粒子在画布上停留的时间
 
 
    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
            #粒子x轴继续膨胀
            move_y = sin(radians(self.id*360/self.total))*self.initial_speed
            #粒子y轴继续膨胀
            self.cv.move(self.cid, move_x, move_y)
            #根据id把画布上的粒子移动x和y个距离
            self.vx = move_x/(float(dt)*1000)
            #粒子x轴的速度
       
        
        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轴
 

        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
        #判断膨胀时间是否小于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()
    #返回自1970年后经过的浮点秒数,精确到小数点后7位
    explode_points = []
    #爆炸点列表,烟花列表
    wait_time = randint(10,100)
    #等待时间为10到100之间整数
    numb_explode = randint(8,20)
    #爆炸烟花个数时6到10之间的随机整数
    # create list of list of all particles in all simultaneous explosion
    for point in range(numb_explode):
        #为所有模拟烟花绽放的全部粒子创建一列列表
        if point<=4:
            objects = []
            #每个点的爆炸粒子列表粒子列表
            x_cordi = 250 + point*50
            #每个爆炸点的x轴
            y_cordi = 100
            #每个爆炸点的y轴
            speed = uniform (0.5, 1.5)          
            #每个爆炸点的速度
            size = uniform (0.5,3)
            #每个爆炸点的大小
            color = choice(colors)
            #每个爆炸点的颜色
            explosion_speed = uniform(0.6, 3)
            #爆炸的绽放速度
            total_particles = randint(10,60)
            #烟花的总粒子数
            for i in range(1,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))
                #把上述参数代入part函数,但是每个粒子的生存时间是自己独立的
                objects.append(r)
                #把r添加进粒子列表
            explode_points.append(objects)
            #把粒子列表添加进烟花列表
        else: 
            objects = []
            #每个点的爆炸粒子列表粒子列表
            x_cordi = randint(50,550)
            #每个爆炸点的x轴
            y_cordi = randint(50, 150)
            #每个爆炸点的y轴
            speed = uniform (0.5, 1.5)          
            #每个爆炸点的速度
            size = uniform (0.5,3)
            #每个爆炸点的大小
            color = choice(colors)
            #每个爆炸点的颜色
            explosion_speed = uniform(0.3, 2)
            #爆炸的绽放速度
            total_particles = randint(10,50)
            #烟花的总粒子数
            for i in range(1,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))
                #把上述参数代入part函数,但是每个粒子的生存时间是自己独立的
                objects.append(r)
                #把r添加进粒子列表
            explode_points.append(objects)
            #把粒子列表添加进烟花列表

 
    total_time = .0
    #初始化总时间
    # keeps undate within a timeframe of 1.8 second 
    while total_time < 2:
    #当总时间小于1.8秒时运行该循环
        sleep(0.03)
        #让画面暂停0.01秒
        tnew = time()
        #刷新时间
        t, dt = tnew, tnew - t
        #时间等于新时间,和上次时间间隔为tnew-t
        for point in explode_points:
        #遍历烟花列表
            for item in point:
            #遍历烟花里的粒子列表
                item.update(dt)
                #粒子更新时间
        cv.update()
        #刷新画布
        total_time += dt
        #为while循环增加时间
 
    root.after(wait_time, simulate, cv)
    #将组件置于其他组件之后,放在最顶层,覆盖下面的,递归调用自己,形成新一轮的爆炸

def close(*ignore):
    #打开模拟循环并关闭窗口
    """Stops simulation loop and closes the window."""
    global root
    root.quit()

​4)效果展示💐

展示四组烟花效果哈——其余的自己拿代码试试哦~🎇其实都是动态放烟花滴~

一、​PART 01🌻

1.0 随机作品展示效果

1.2 烟花文案——满天星辰🌙

✨ 凑不够满天星辰 那就放烟花给你看

✨ 烟花很美 你看着烟花微笑时的微笑更美

✨ 你带我看了一场烟花 我梦里都是你的微笑

✨ 若时光是醉世烟花,你能否与我共看璀璨风华

✨ 心上人有他的烟花之火 心上人是你的眼光有火

I will luve thee still, my dear.

While the sands o" life shall run.​

二、PART 02🌻

1.0随机作品展示效果❤

1.2 烟花文案——三生烟火🌙

✨ 一根仙女棒可以燃烧9秒 瞬间释放180个火焰比银河系的星星还多 所以我喊你出来放烟花是想给你满天繁星

✨ 独看烟花绽放,独守烟花渐凉
 

✨ 烟花转瞬即逝,灰烬倒是永恒
 

✨ 我用三生烟火,还你一世迷离
 

✨ 你,曾如此美丽的烟花盛放在我的天空,当烟火散尽后,我于残垣断壁处,只寻得一地的支离破碎

I will luve thee still, my dear.

While the sands o" life shall run.

​三、PART 03🌻

1.0 随机作品展示效果❤

1.2 烟花文案——十里长街🌙

✨ 从此烟花,不止刹那,如我的祝福,不会随寂寞瞬间落下

✨ 你的出现 就像深夜里绽放的烟花 虽然只有一瞬间 却照亮了我的整个人生

✨ 十里寒潭路,烟花一半醒

✨ 烟花绽放,徐徐仰望,寂寥无眠,思卿断肠

✨ 长街长,烟花繁,你挑灯回看, 短亭短,红尘辗,我把萧再叹

I will luve thee still, my dear.

While the sands o" life shall run.

四、PART 04🌻

1.0 随机作品展示效果❤

1.2 烟花文案——一眼万年🌙

✨ 烟花是永远的,因为它在我心里刻在了永恒的美丽。烟花是幸福的,因为它自己对留下的瞬间肯定了自己的美丽

✨ 喜欢你时,我的心里全都是绽放的小小烟花。那,在今年烟花盛开的时候,就让我扑到你怀里好不好?

✨ 一定要和你看一场烟火大会,感受一场浪漫与心动

✨ 烟花绚丽绽放,一瞬即是一世。守护之心坚定,千年亦在咫尺

✨ 刹那芳华,犹如指尖流沙。灿烂烟花,终究剪不下

I will luve thee still, my dear.

While the sands o" life shall run.

5)番外表白篇——玫瑰文末附代码🍁

“谁都逃不过玫瑰 而玫瑰送谁都浪漫”

5.1 玫瑰Turtle作品一

5.2 玫瑰Turtle作品二

"我终将落俗 但浪漫不死"

总结

"哪有女孩子不喜欢鲜花不爱浪漫"! 好啦——烟花是你——玫瑰也是你!

文章就到这里就写完啦~代码拿去表白或者给对象叭!

​​​

🎯完整的源码免费分享滴!需要的滴滴我!

你们的支持是我最大的动力!!记得三连哦~mua 欢迎大家阅读往期的文章哦~