基于Python记录一场2023的烟花
作者:Leleprogrammer 发布时间:2022-01-08 19:57:07
弹指间,2023已经到来,新的一年,祝大家新年快乐,阖家幸福呀~~~
好吧,进入正题,2023的到来,肯定少不了烟花吧(外面不让放炮,那咱们就用python放炮【DOGE】)
首先,需要的外置库:pygame,pymunk
导入
import pygame
from pygame.locals import *
import pymunk
from pymunk import pygame_util
import sys
import random as rd
import time
import math
然后写一个主程序类,对pygame进行初始化,设置屏幕宽高,设置标题,创建pymunk空间,然后设置空间的重力为(0,300),然后设置collision_persistence参数为0,表示没有碰撞,毕竟俩烟花也不会撞一起......然后设置烟花半径(可以自行修改),创建两个列表,用于存放烟花 * 形成的火球和发射到天空中还没 * 的烟花,创建一个colors列表,存放烟花的颜色
class Firework:
def __init__(self):
pygame.init()
self.W,self.H=800,1000
self.screen=pygame.display.set_mode((self.W,self.H))
self.draw_options=pygame_util.DrawOptions(self.screen)
pygame.display.set_caption("2023元旦烟花")
self.space=pymunk.Space()
self.space.gravity=(0,300)
self.space.collision_persistence=0
self.fireball_radius=2
self.fire_radius=2
self.fireballs=[]
self.colors=[
(255,0,0),(255,127,80),(255,140,0),(255,160,122),(240,128,128),(255,99,71),(255,69,0),
(255,105,180),(255,20,147),(208,32,144),(176,48,96),(153,50,204),(255,48,48),
(238,44,44),(205,38,38),(255,255,0),(255,215,0),(255,185,15),(238,201,0),
(34,139,34),(46,139,87),(60,179,113),(0,255,127)
]
self.fires=[]
接下来,进行事件监听,按下鼠标就可以创建火球
def listen(self):
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
if event.type==MOUSEBUTTONDOWN:
self.create_firework(x=pygame.mouse.get_pos()[0])
然后写个创建烟花的方法,首先要有个body,设置body_type为DYNAMIC,因为烟花是动态的。然后设置body的位置,x坐标为传参的x坐标,y坐标为屏幕最底部,接下来创建一个shape,形状为circle,body对应的fireball_body传进去就好了,然后设置radius(半径),设置shape的弹性(这个不设置也可以,没多大影响),将body和shape添加到空间中,用add添加,然后将烟花对应的shape对象、颜色、创建时间、 * 前持续时间这四个参数归在一个列表,将这个列表添加到fireballs中,最后就是要赋予body冲击力了,使用apply_impulse_at_local_point方法
def create_firework(self,x):
fireball_body=pymunk.Body(mass=1,moment=100,body_type=pymunk.Body.DYNAMIC)
fireball_body.position=x,self.H
fireball_shape=pymunk.Circle(fireball_body,self.fireball_radius)
fireball_shape.elasticity=0.5
self.space.add(fireball_body,fireball_shape)
self.fireballs.append([fireball_shape,rd.choice(self.colors),time.time(),rd.uniform(1,2.2)]) # shape,color,startTime,lastTime
fireball_body.apply_impulse_at_local_point((0,rd.randint(-730,-500)),(0,0))
然后是draw的代码(比较多),先是填充背景为黑色,然后使用while循环遍历fireballs,将烟花绘制出来,检查是否到达了 * 时间,如果已经到达 * 时间,那么将这个火球从烟花的列表中删掉。接下来就要创建炸开来的火花,火花是向不同方向散开的,所以用for循环遍历一圈的度数,每隔10°就有一个,length是斜边长度,然后定义bias偏移量,因为火花散发力量和距离并不是固定的,所以每一次length都会浮动一点,但始终控制在25~100之间(maximum和minimum),因为apply_impulse_at_local_point发射出去时传参是x轴的力量和y轴的力量,所以要使用三角函数计算临边和对边的长度从而得到apply_impulse_at_local_point传参的数值,又因为我们遍历的是度数(degree),sin和cos计算的是弧度(radians),所以要先把度数通过math.radians转化为弧度,再传参到sin和cos中。计算出来之后,还是创建body和shape并设置一些基本的参数,添加到空间中,并添加到fires列表中,最后删除已 * 的烟花,别忘了变量i需要减1。
上面是对未 * 的烟花进行遍历,下面我们还需要对 * 后形成的火花进行遍历,如果超出范围或已到达删除时间就进行删除的操作,逻辑差不多
draw的代码如下
def draw(self):
self.screen.fill((0,0,0))
i=0
while i<len(self.fireballs):
fireball,color,startTime,lastTime=self.fireballs[i]
pygame.draw.circle(self.screen,color,fireball.body.position,self.fireball_radius)
nowTime=time.time()
boomTime=startTime+lastTime
if nowTime>boomTime:
popball=self.fireballs.pop(i)
length=50
for degree in range(90,450,10):
bias=1
length+=rd.randint(-bias,bias)
maximum,minimum=100,25
if length>maximum:
length=maximum
elif length<minimum:
length=minimum
radians=math.radians(degree)
x_force=math.sin(radians)*length
y_force=math.cos(radians)*length
body=pymunk.Body(mass=1,moment=100,body_type=pymunk.Body.DYNAMIC)
body.position=popball[0].body.position
shape=pymunk.Circle(body,self.fire_radius)
self.space.add(body,shape)
self.fires.append([shape,popball[1],time.time(),rd.uniform(0.5,1.5)]) # shape,color,startTime,lastTime
body.apply_impulse_at_local_point((x_force,y_force),(0,0))
self.space.remove(popball[0])
i-=1
i+=1
i=0
while i<len(self.fires):
fire,color,startTime,lastTime=self.fires[i]
pos=fire.body.position
pygame.draw.circle(self.screen,color,pos,self.fire_radius)
nowTime=time.time()
deleteTime=startTime+lastTime
if nowTime>deleteTime:
self.fires.pop(i)
self.space.remove(fire)
i-=1
elif pos[0]<0 or pos[0]>self.W or pos[1]>self.H:
self.fires.pop(i)
self.space.remove(fire)
i-=1
i+=1
写到这儿,我们的烟花就差不多完成了,最后写个run
def run(self):
clock=pygame.time.Clock()
FPS=60
while True:
clock.tick(FPS)
self.listen()
self.draw()
self.space.step(1/FPS)
pygame.display.update()
运行
if __name__=="__main__":
firework=Firework()
firework.run()
这样就好啦!
最终代码
import pygame
from pygame.locals import *
import pymunk
from pymunk import pygame_util
import sys
import random as rd
import time
import math
class Firework:
def __init__(self):
pygame.init()
self.W,self.H=800,1000
self.screen=pygame.display.set_mode((self.W,self.H))
self.draw_options=pygame_util.DrawOptions(self.screen)
pygame.display.set_caption("2023元旦烟花")
self.space=pymunk.Space()
self.space.gravity=(0,300)
self.space.collision_persistence=0
self.fireball_radius=2
self.fire_radius=2
self.fireballs=[]
self.colors=[
(255,0,0),(255,127,80),(255,140,0),(255,160,122),(240,128,128),(255,99,71),(255,69,0),
(255,105,180),(255,20,147),(208,32,144),(176,48,96),(153,50,204),(255,48,48),
(238,44,44),(205,38,38),(255,255,0),(255,215,0),(255,185,15),(238,201,0),
(34,139,34),(46,139,87),(60,179,113),(0,255,127)
]
self.fires=[]
def listen(self):
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
if event.type==MOUSEBUTTONDOWN:
self.create_firework(x=pygame.mouse.get_pos()[0])
def create_firework(self,x):
fireball_body=pymunk.Body(mass=1,moment=100,body_type=pymunk.Body.DYNAMIC)
fireball_body.position=x,self.H
fireball_shape=pymunk.Circle(fireball_body,self.fireball_radius)
fireball_shape.elasticity=0.5
self.space.add(fireball_body,fireball_shape)
self.fireballs.append([fireball_shape,rd.choice(self.colors),time.time(),rd.uniform(1,2.2)]) # shape,color,startTime,lastTime
fireball_body.apply_impulse_at_local_point((0,rd.randint(-730,-500)),(0,0))
def draw(self):
self.screen.fill((0,0,0))
i=0
while i<len(self.fireballs):
fireball,color,startTime,lastTime=self.fireballs[i]
pygame.draw.circle(self.screen,color,fireball.body.position,self.fireball_radius)
nowTime=time.time()
boomTime=startTime+lastTime
if nowTime>boomTime:
popball=self.fireballs.pop(i)
length=50
for degree in range(90,450,10):
bias=1
length+=rd.randint(-bias,bias)
maximum,minimum=100,25
if length>maximum:
length=maximum
elif length<minimum:
length=minimum
radians=math.radians(degree)
x_force=math.sin(radians)*length
y_force=math.cos(radians)*length
body=pymunk.Body(mass=1,moment=100,body_type=pymunk.Body.DYNAMIC)
body.position=popball[0].body.position
shape=pymunk.Circle(body,self.fire_radius)
self.space.add(body,shape)
self.fires.append([shape,popball[1],time.time(),rd.uniform(0.5,1.5)]) # shape,color,startTime,lastTime
body.apply_impulse_at_local_point((x_force,y_force),(0,0))
self.space.remove(popball[0])
i-=1
i+=1
i=0
while i<len(self.fires):
fire,color,startTime,lastTime=self.fires[i]
pos=fire.body.position
pygame.draw.circle(self.screen,color,pos,self.fire_radius)
nowTime=time.time()
deleteTime=startTime+lastTime
if nowTime>deleteTime:
self.fires.pop(i)
self.space.remove(fire)
i-=1
elif pos[0]<0 or pos[0]>self.W or pos[1]>self.H:
self.fires.pop(i)
self.space.remove(fire)
i-=1
i+=1
def run(self):
clock=pygame.time.Clock()
FPS=60
while True:
clock.tick(FPS)
self.listen()
self.draw()
self.space.step(1/FPS)
pygame.display.update()
if __name__=="__main__":
firework=Firework()
firework.run()
此时我仿佛听见你说:一直按鼠标手好酸啊!
那没事,我们改成自动就好啦!
只需要修改一部分内容就好
首先需要一个自定义事件(全局变量)
CREATE_FIREBALL=USEREVENT+1
run中while循环前添加这一行,用来启动事件循环,第二个参数是间隔时间,单位是毫秒,可以自己修改
pygame.time.set_timer(CREATE_FIREBALL,120)
将listen改成这样
def listen(self):
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
if event.type==CREATE_FIREBALL:
self.create_firework(x=rd.randint(0,self.W))
这样就大功告成啦!
来源:https://blog.csdn.net/leleprogrammer/article/details/128510460


猜你喜欢
- 写在前面:这篇文章也是借鉴了一些前辈的代码和思路写的,代码有些也是复用了别人的。先说下思路:1.首先利用Opencv检测出人脸的区域&nbs
- 最近接触了一些selenium模块的相关知识,觉得还挺有意思的,于是决定亲自尝试写一些爬虫程序来强化selenium模块(一定要多尝试、多动
- 前言接着上一篇的故事工厂模式继续,手机要出厂,显然光一个手机肯定是不行的,还需要包装盒、充电器等等东西。我们按照上一篇提到的工厂模式,去建立
- 在Goland中,如果 import 了包,但在代码中没有使用,会自动帮你移除这个包的 引用有可能是习惯问题,每次写代码都习惯 先impor
- golang的defer优雅又简洁, 是golang的亮点之一。defer在声明时不会立即执行,而是在函数return后,再按照先进后出的原
- 这篇文章主要介绍了如何使用Python发送HTML格式的邮件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 以下是作者在学习Python中django框架时的学习笔记,并把测试的代码做了详细分析,最后还附上了学习心得,值得大家学习。URL配置(UR
- 近几年来,nosql大行其道,json更是火的一塌糊涂,作为数据库的元老,mysql在5.7版本中添加了对json数据的支持。这片博客主要介
- 单来说,vue-resource就像jQuery里的$.ajax,用来和后端交互数据的。可以放在created或者ready里面运行来获取或
- 在使用tensorflow时常常会使用到tf.reduce_*这类的函数,在此对一些常见的函数进行汇总1.tf.reduce_sumtf.r
- 问:假如我的一个表里含有(a,b,c,d)和(a,b)形成组合键。我能在列值中写这个查询吗?例如: select a,c,d from my
- 存储过程是用户定义的一系列sql语句的集合,涉及特定表或其它对象的任务,用户可以调用存储过程,而函数通常是数据库已定义的方法,它接收参数并返
- 最近为了做Hyperledger Fabric国密改造,涉及到了golang源码的改动。特将操作过程整理如下,以供参考:golang的源码安
- 1. Python 的参数传递Python的参数传递,无法控制引用传递还是值传递。对于不可变对象(数字、字符、元组等)的参数,更类似值传递;
- 解决方案:1、选择Edit Configurations, 删除相关单元测试2、右击__name__ == "__main__&q
- 先给大家介绍下python制作定时发送信息脚本,内容如下所示:文章中提到的菜单是右下角这个需求我们需要做到打开微信获取输入框焦点及输入思路1
- 从python2到python3,这两个版本可以说是从语法、编码等多个方面上都有很大的差别。为了不带入过多的累赘,Python 3.0在设计
- 在网页中放iframe,如果frameborder=0;就没有边框显示了;但动态创建时,在IE7中就不行了,从网上找到解决的办法,写出来记录
- 亲测十分靠谱下面是解决该问题的方法第一步:关闭SIP系统保护1.重启系统时按住Command+R进入恢复模式(记住是你在重新启动时,不是启动
- 前言综合前述的类、函数、matplotlib等,完成一个随机移动的过程(注意要确定移动的次数,比如10万次),每次行走都完全是随机的,没有明