用Python记录一场2023的烟花

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

弹指间2023已经到来新的一年祝大家新年快乐阖家幸福呀~~~

好吧进入正题2023的到来肯定少不了烟花吧外面不让放炮那咱们就用python放炮【DOGE】

首先需要的外置库pygamepymunk

导入

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空间然后设置空间的重力为0300然后设置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形状为circlebody对应的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传参的数值又因为我们遍历的是度数degreesin和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))

这样就大功告成啦

手动放烟花和自动放烟花的完整代码作者都已经放到GitHub啦

https://github.com/LeaderLeLe/Firework2023

https://github.com/LeaderLeLe/Firework2023.git

喜欢的话就来个3连吧~~ 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: python