python实现烟花小程序
作者:Dachao1013 发布时间:2022-10-02 09:45:23
标签:python,烟花,小程序
本文实例为大家分享了python实现烟花小程序的具体代码,供大家参考,具体内容如下
'''
FIREWORKS SIMULATION WITH TKINTER
*self-containing code
*to run: simply type python simple.py in your console
*compatible with both Python 2 and Python 3
*Dependencies: tkinter, Pillow (only for background image)
*The design is based on high school physics, with some small twists only for aesthetics purpose
import tkinter as tk
#from tkinter import messagebox
#from tkinter import PhotoImage
from PIL import Image, ImageTk
from time import time, sleep
from random import choice, uniform, randint
from math import sin, cos, radians
# gravity, act as our constant g, you can experiment by changing it
GRAVITY = 0.05
# list of color, can choose randomly or use as a queue (FIFO)
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen','indigo', 'cornflowerblue']
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: identifier of a particular particle in a star
- x, y: x,y-coordinate of a star (point of explosion)
- vx, vy: speed of particle in x, y coordinate
- total: total number of particle in a star
- age: how long has the particle last on canvas
- color: self-explantory
- cv: canvas
- lifespan: how long a particle will last on canvas
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
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
# particle expansions
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)
# falling down in projectile motion
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
# remove article if it is over the lifespan
elif self.cid is not None:
cv.delete(self.cid)
self.cid = None
# define time frame for expansion
def expand (self):
return self.age <= 1.2
# check if particle is still alive in lifespan
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(6,10)
# create list of list of all particles in all simultaneous explosion
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 = 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
# keeps undate within a timeframe of 1.8 second
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
# recursive call to continue adding new explosion on canvas
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()
cv = tk.Canvas(root, height=600, width=600)
# use a nice background image
image = Image.open("./image1.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()
注意:这里需要安装tkinter,安装过程:
step1:
>>> import _tkinter # with underscore, and lowercase 't'
step2:
>>> import Tkinter # no underscore, uppercase 'T' for versions prior to V3.0
>>> import tkinter # no underscore, lowercase 't' for V3.0 and later
step3:
>>> Tkinter._test() # note underscore in _test and uppercase 'T' for versions prior to V3.0
>>> tkinter._test() # note underscore in _test and lowercase 'T' for V3.0 and later
然后就可以运行了,在代码中有一个背景照片部分,路径可自行选择!我这里就不修改了。
来源:https://blog.csdn.net/Dachao0707/article/details/82955710


猜你喜欢
- python openvc 裁剪图片下面是4个坐标代码:import cv2#裁剪图片路径input_path,四个裁剪坐标为:y1,y2,
- 简介OpenCV中使用VideoCapture类写的视频是没有音频的,如果要进一步处理音频则需要用到一个库——MoviePy,这个库是Pyt
- Django的QuerySets酷毙了!在本文中我将解释一下QuerySets是什么,它是如何工作的(如果你对它已经熟悉了,你可
- 一、最长回文子串问题描述🪐大家已经熟悉了AABCC、AABBCC这种类型的字符串是回文串。也就是说,排除掉字符串中的各种字符,字母不区分大小
- 需求在某应用中,需要根据一定的规则生成随机的IP地址,规则类似于192.168.11.0/24这样的CIDR形式给出。实现经过艰苦卓绝的调试
- 1 事务的使用1.1 事务概念事务就是一组DML语句组成,这些语句在逻辑上存在相关性,这一组DML语句要么全部成功,要么全部失败,是一个整体
- mysql优化是我们日常工作经常遇到的问题,今天给大家说下MySQL常见的几种优化方案。注:原始资料来自享学课堂,自己加上整理和思考思考sq
- 在网上看过很多版本的PHP异步请求方法,这里简单总结几个常用方法分享给大家 1、用CURL实现一步请求 CURL扩展是我们在开发过程中最常用
- 1、什么是mixin这不是Vue的专属的,可以说是一种思想,在很多开发框架中都实现了Mixin。官方解释:mixin提供了一种非常灵活的方式
- 本文实例讲述了Python使用Dijkstra算法实现求解图中最短路径距离问题。分享给大家供大家参考,具体如下:这里继续前面一篇《Pytho
- 刚开始使用webpack时,可能很多人都会有过这样的想法,在require文件时,能不能不写静态的字符串路径,而是使用一个更灵活的方式,比如
- SMTPSMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Python对SMTP
- PyTorch创建自己的数据集图片文件在同一的文件夹下思路是继承 torch.utils.data.Dataset,并重点重写其 __get
- 之前写了Python实现登录接口的示例代码,最近需要回顾,就顺便发到随笔上了要求:1.输入用户名和密码2.认证成功,显示欢迎信息3.用户名3
- 本文实例讲述了Python实现从订阅源下载图片的方法。分享给大家供大家参考。具体如下:这段代码是基于python 3.4实现的,和pytho
- 实例如下:from win32com.client import Dispatch import win32com.client
- 前言之前看到一个有意思的开源项目,主要是可以将一张照片变成卡通漫画的风格。下面给大家放几张官方给出的部分效果图。看到这个效果图,还是非常经验
- 本文实例讲述了php计算两个整数的最大公约数常用算法。分享给大家供大家参考。具体如下:<?php//计时,返回秒function&nb
- 查询所有数据库的总大小方法如下:mysql> use information_schema;mysql> select conc
- PHP mysqli_stat() 函数创建 SSL 连接:<?php// 假定数据库用户名:root,密码:123456,数据库:c