python创意小作品代码_python浪漫表白源码

python创意小作品代码_python浪漫表白源码这篇文章主要为大家详细介绍了python实现浪漫的烟花秀,具有一定的参考价值,感兴趣的小伙伴们可以参考一下无意中看到一段用Tkinter库写的放烟花的程序,就跟着跑了一遍。设计理念:通过让画面上一个粒子分裂为X数量的粒子来模拟爆炸效果。粒子会发生“膨胀”,意思是它们会以恒速移动且相互之间的角度相等。这样就能让我们以一个向外膨胀的圆圈形式模拟出烟花绽放的画面。经过一定时间后,粒子会进入“自由落体”阶…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

这篇文章主要为大家详细介绍了python实现浪漫的烟花秀,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

无意中看到一段用Tkinter库写的放烟花的程序,就跟着跑了一遍。

设计理念:通过让画面上一个粒子分裂为X数量的粒子来模拟爆炸效果。粒子会发生“膨胀”,意思是它们会以恒速移动且相互之间的角度相等。这样就能让我们以一个向外膨胀的圆圈形式模拟出烟花绽放的画面。经过一定时间后,粒子会进入“自由落体”阶段,也就是由于重力因素它们开始坠落到地面,仿若绽放后熄灭的烟花。

python创意小作品代码_python浪漫表白源码

首先我们写一个粒子类,表示烟花事件中的每个粒子,包含大小,颜色,位置,速度等属性以及粒子经历的三个阶段的函数,即:膨胀、坠落、消失。

”’

particles 类

粒子在空中随机生成随机,变成一个圈、下坠、消失

属性:

– id: 粒子的id

– x, y: 粒子的坐标

– vx, vy: 在坐标的变化速度

– total: 总数

– age: 粒子存在的时长

– color: 颜色

– cv: 画布

– lifespan: 最高存在时长

”’

class Particle:

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

self.y = y

self.initial_speed = explosion_speed

self.vx = vx

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))

# 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

# 移除超过最高时长的粒子

elif self.cid is not None:

cv.delete(self.cid)

self.cid = None

# 扩大的时间

def expand (self):

return self.age <= 1.2

# 粒子是否在最高存在时长内

def alive(self):

return self.age <= self.lifespan

接下来我们需要创建一列列表,每个子列表是一个烟花,其包含一列粒子,每个列表中的粒子有相同的x,y坐标、大小、颜色、初始速度。

源码如下:

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

# 模拟重力

GRAVITY = 0.05

# 颜色选项(随机或者按顺序)

colors = [‘red’, ‘blue’, ‘yellow’, ‘white’, ‘green’, ‘orange’, ‘purple’, ‘seagreen’, ‘indigo’, ‘cornflowerblue’]

”’

particles 类

粒子在空中随机生成随机,变成一个圈、下坠、消失

属性:

– id: 粒子的id

– x, y: 粒子的坐标

– vx, vy: 在坐标的变化速度

– total: 总数

– age: 粒子存在的时长

– color: 颜色

– cv: 画布

– lifespan: 最高存在时长

”’

class Particle:

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

self.y = y

self.initial_speed = explosion_speed

self.vx = vx

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))

# 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

# 移除超过最高时长的粒子

elif self.cid is not None:

cv.delete(self.cid)

self.cid = None

# 扩大的时间

def expand (self):

return self.age <= 1.2

# 粒子是否在最高存在时长内

def alive(self):

return self.age <= self.lifespan

”’

循环调用保持不停

”’

def simulate(cv):

t = time()

explode_points = []

wait_time = randint(10, 100)

numb_explode = randint(6, 10)

# 创建一个所有粒子同时扩大的二维列表

for point in range(numb_explode):

objects = []

x_cordi = randint(50, 550)

y_cordi = randint(50, 150)

speed = uniform(0.5, 1.5)

size = uniform(0.5, 3)

color = choice(colors)

explosion_speed = uniform(0.2, 1)

total_particles = randint(10, 50)

for i in range(1, total_particles):

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

# 1.8s内一直扩大

while total_time < 1.8:

sleep(0.01)

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):

“””退出程序、关闭窗口”””

global root

root.quit()

if __name__ == ‘__main__’:

root = tk.Tk()

cv = tk.Canvas(root, height=360, width=480)

# 选一个好看的背景会让效果更惊艳!

image = Image.open(“./image.jpg”)

photo = ImageTk.PhotoImage(image)

cv.create_image(0, 0, image=photo, anchor=’nw’)

cv.pack()

root.protocol(“WM_DELETE_WINDOW”, close)

root.after(100, simulate, cv)

root.mainloop()

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

# 模拟重力

GRAVITY = 0.05

# 颜色选项(随机或者按顺序)

colors = [‘red’, ‘blue’, ‘yellow’, ‘white’, ‘green’, ‘orange’, ‘purple’, ‘seagreen’, ‘indigo’, ‘cornflowerblue’]

”’

particles 类

粒子在空中随机生成随机,变成一个圈、下坠、消失

属性:

– id: 粒子的id

– x, y: 粒子的坐标

– vx, vy: 在坐标的变化速度

– total: 总数

– age: 粒子存在的时长

– color: 颜色

– cv: 画布

– lifespan: 最高存在时长

”’

class Particle:

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

self.y = y

self.initial_speed = explosion_speed

self.vx = vx

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))

# 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

# 移除超过最高时长的粒子

elif self.cid is not None:

cv.delete(self.cid)

self.cid = None

# 扩大的时间

def expand (self):

return self.age <= 1.2

# 粒子是否在最高存在时长内

def alive(self):

return self.age <= self.lifespan

”’

循环调用保持不停

”’

def simulate(cv):

t = time()

explode_points = []

wait_time = randint(10, 100)

numb_explode = randint(6, 10)

# 创建一个所有粒子同时扩大的二维列表

for point in range(numb_explode):

objects = []

x_cordi = randint(50, 550)

y_cordi = randint(50, 150)

speed = uniform(0.5, 1.5)

size = uniform(0.5, 3)

color = choice(colors)

explosion_speed = uniform(0.2, 1)

total_particles = randint(10, 50)

for i in range(1, total_particles):

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

# 1.8s内一直扩大

while total_time < 1.8:

sleep(0.01)

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):

“””退出程序、关闭窗口”””

global root

root.quit()

if __name__ == ‘__main__’:

root = tk.Tk()

cv = tk.Canvas(root, height=360, width=480)

# 选一个好看的背景会让效果更惊艳!

image = Image.open(“./image.jpg”)

photo = ImageTk.PhotoImage(image)

cv.create_image(0, 0, image=photo, anchor=’nw’)

cv.pack()

root.protocol(“WM_DELETE_WINDOW”, close)

root.after(100, simulate, cv)

root.mainloop()

效果图(背景请忽略哈哈):

python创意小作品代码_python浪漫表白源码

喜欢自己去跑一下

内容就以上怎么多,最后给大家推荐一个口碑不错的公众号【程序员学府】,这里有很多的老前辈学习技巧,学习心得,面试技巧,职场经历等分享,更为大家精心准备了零基础入门资料,实战项目资料,每天都有程序员定时讲解Python技术,分享一些学习的方法和需要留意的小细节

python创意小作品代码_python浪漫表白源码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190466.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 中国银行笔试题目回忆录_中国银行好考吗

    中国银行笔试题目回忆录_中国银行好考吗亲,戳上面的蓝字关注我们哦!中国银行笔试回忆题目昨天刚考完的中国银行笔试题目,趁着头脑还比较清醒先在这里记录一下吧,我选的是中国银行的信息技术岗位。我是提前就进了考场,中国银行考场还是比较好的,还

    2022年8月3日
    12
  • 字符串匹配算法_多字符串匹配

    字符串匹配算法_多字符串匹配文章目录1.BM(Boyer-Moore)算法1.BM(Boyer-Moore)算法思想:有模式串中不存在的字符,那么肯定不匹配,往后多移动几位,提高效率BM原理:坏字符规则,好后缀规则…

    2022年8月21日
    5
  • 数据结构与算法学习笔记

    本文是王争老师的《算法与数据结构之美》的学习笔记,详细内容请看王争的专栏。有不懂的地方指出来,我做修改。数据结构与算法思维导图数据结构指的是“一组数据的存储结构”,算法指的是“操作数据的一组方法”。数据结构是为算法服务的,算法是要作用再特定的数据结构上的。最常用的数据结构预算法:数据结构:数组、链表、栈、队列、散列表、二叉树‘、堆、跳表、图、Tire树 算法:递归…

    2022年4月7日
    210
  • LOAM, ALOAM, LegoLOAM, hdl graph slam比较

    LOAM, ALOAM, LegoLOAM, hdl graph slam比较A-LOAMLOAM:•LOAMuseanewdefinedfeaturesystem(cornerandflatpoint),forthedetailseeitsarticle.•LOAMsupposelinearmotionwithinthescanswap(VLOAMfurtherusesvisualodometrytoe…

    2025年6月15日
    0
  • 位图(bmp)文件格式分析

    from:https://blog.csdn.net/qingchuwudi/article/details/25785307位图(bmp)文件格式分析作者:深蓝(由博主分享)一、什么是位图计算机能以位图和矢量图格式显示图像。1、位图(Bitmap):图像又称点阵图或光栅图,它使用我们称为像素(象素,Pixel)的一格一格的小点来描述图像。计算机屏幕其实就是一张包含大量像素…

    2022年4月3日
    209
  • http协议与tcp协议区别[通俗易懂]

    http协议与tcp协议区别[通俗易懂]http协议与tcp协议区别1、性质不同:http是一个简单的请求-响应协议。TCP是一种面向连接的、可靠的、基于字节流的传输层通信协议。2、连接不同:TCP连接到不同但互连的计算机通信网络的主计算机中的成对进程之间依靠TCP提供可靠的通信服务。http通常运行在TCP之上。指定了客户端可能发送给服务器什么样的消息以及得到什么样的响应。3、功能不同:当应用层向TCP层发送用于网间传输的、用8位字节表示的数据流,TCP则把数据流分割成适当长度的报文段,最大传输段大小(MSS)通常受该计算机连接的网

    2022年9月20日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号