【Python】情人节表白烟花(带声音和文字)

您所在的位置:网站首页 好看浪漫的背景图片 【Python】情人节表白烟花(带声音和文字)

【Python】情人节表白烟花(带声音和文字)

2024-07-12 02:37| 来源: 网络整理| 查看: 265

​今天就是情人节了,有想好送什么给亲爱的他/她吗?    去年5月20日的时候整理了Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白),今天向大家介绍表白烟花,祝单身的朋友顺利脱单,有男/女朋友的朋友约会甜蜜。

在这里插入图片描述

你是年少的欢喜,反过来也是

2.两个人的烟花(音乐背景)

在整个宇宙,你是我唯一的星

     

二、绘制代码

   实现本文效果的整体思路是:加载库—选择背景图片和音乐—在屏幕上添加想说的话—在屏幕上实现烟花效果。

  

1.导入库

  

要想实现本文的效果,首先要先在python中加载以下库。   

# -*- coding: UTF-8 -*- ''' 代码用途 :情人节表白 作者 :阿黎逸阳 公众号 : 阿黎逸阳的代码 ''' 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

  

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(100, simulate, cv) #在0.2秒后再调用stimulate函数,生成一轮烟花绽放效果 root.mainloop() #执行root,生成窗口

  

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

  

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(): #如果粒子既存活又处于扩张阶段 columnFont = ('华文行楷',18) 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='520',tag="write_tag", fill=choice(colors),font = columnFont) move_x = cos(radians(self.id*360/self.total))*self.initial_speed*3 #粒子x轴继续膨胀 move_y = sin(radians(self.id*360/self.total))*self.initial_speed*3 #粒子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 = ('华文行楷',18) columnFont1 = ('华文行楷',18) #如果粒子仅存活不扩张(只是停留时间足够,说明膨胀到最大了),则自由坠落 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='520',tag="write_tag", fill=choice(colors),font = columnFont) #添加烟花中的文字 self.cv.create_text(440, 500, text="You're the only star belongs me,",tag="eternal", fill='white',font = columnFont1) self.cv.create_text(470, 550, text="In the whole universe!",tag="eternal", fill='white',font = columnFont1) 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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3