Python实现新年烟花秀的代码怎么写

Python实现新年烟花秀的代码怎么写

今天就跟大家聊聊有关Python实现新年烟花秀的代码怎么写,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

先介绍下 Pygame 绘制烟花的基本原理,烟花从发射到绽放一共分为三个阶段:

1,发射阶段:在这一阶段烟花的形状是线性向上,通过设定一组大小不同、颜色不同的点来模拟“向上发射” 的运动运动,运动过程中 5个点被赋予不同大小的加速度,随着时间推移,后面的点会赶上前面的点,最终所有点会汇聚在一起,处于 绽放准备阶段;

2,烟花绽放:烟花绽放这个阶段,是由一个点分散多个点向不同方向发散,并且每个点的移动轨迹可需要被记录,目的是为了追踪整个绽放轨迹。

3,烟花凋零,此阶段负责描绘绽放后烟花的效果,绽放后的烟花,而在每一时刻点的下降速度和亮度(代码中也叫透明度)是不一样的,因此在代码里,将烟花绽放后将每个点赋予两个属性:分别为重力向量和生命周期,来模拟烟花在不同时期时不同的展现效果。

程序运行截图:

完整程序代码:

importpygamefromrandomimportrandint,uniform,choiceimportmathvector=pygame.math.Vector2gravity=vector(0,0.3)DISPLAY_WIDTH=DISPLAY_HEIGHT=800trail_colours=[(45,45,45),(60,60,60),(75,75,75),(125,125,125),(150,150,150)]dynamic_offset=1static_offset=3classFirework:def__init__(self):#随机颜色self.colour=(randint(0,255),randint(0,255),randint(0,255))self.colours=((randint(0,255),randint(0,255),randint(0,255)),(randint(0,255),randint(0,255),randint(0,255)),(randint(0,255),randint(0,255),randint(0,255)))self.firework=Particle(randint(0,DISPLAY_WIDTH),DISPLAY_HEIGHT,True,self.colour)#Createsthefireworkparticleself.exploded=Falseself.particles=[]self.min_max_particles=vector(100,225)defupdate(self,win):#calledeveryframeifnotself.exploded:self.firework.apply_force(gravity)self.firework.move()fortfinself.firework.trails:tf.show(win)self.show(win)ifself.firework.vel.y>=0:self.exploded=Trueself.explode()else:forparticleinself.particles:particle.apply_force(vector(gravity.x+uniform(-1,1)/20,gravity.y/2+(randint(1,8)/100)))particle.move()fortinparticle.trails:t.show(win)particle.show(win)defexplode(self):#amount数量amount=randint(self.min_max_particles.x,self.min_max_particles.y)foriinrange(amount):self.particles.append(Particle(self.firework.pos.x,self.firework.pos.y,False,self.colours))defshow(self,win):pygame.draw.circle(win,self.colour,(int(self.firework.pos.x),int(self.firework.pos.y)),self.firework.size)defremove(self):ifself.exploded:forpinself.particles:ifp.removeisTrue:self.particles.remove(p)iflen(self.particles)==0:returnTrueelse:returnFalseclassParticle:def__init__(self,x,y,firework,colour):self.firework=fireworkself.pos=vector(x,y)self.origin=vector(x,y)self.radius=20self.remove=Falseself.explosion_radius=randint(5,18)self.life=0self.acc=vector(0,0)#trailvariablesself.trails=[]#storestheparticlestrailobjectsself.prev_posx=[-10]*10#storesthe10lastpositionsself.prev_posy=[-10]*10#storesthe10lastpositionsifself.firework:self.vel=vector(0,-randint(17,20))self.size=5self.colour=colourforiinrange(5):self.trails.append(Trail(i,self.size,True))else:self.vel=vector(uniform(-1,1),uniform(-1,1))self.vel.x*=randint(7,self.explosion_radius+2)self.vel.y*=randint(7,self.explosion_radius+2)#向量self.size=randint(2,4)self.colour=choice(colour)#5个tails总计foriinrange(5):self.trails.append(Trail(i,self.size,False))defapply_force(self,force):self.acc+=forcedefmove(self):ifnotself.firework:self.vel.x*=0.8self.vel.y*=0.8self.vel+=self.accself.pos+=self.velself.acc*=0ifself.life==0andnotself.firework:#checkifparticleisoutsideexplosionradiusdistance=math.sqrt((self.pos.x-self.origin.x)**2+(self.pos.y-self.origin.y)**2)ifdistance>self.explosion_radius:self.remove=Trueself.decay()self.trail_update()self.life+=1defshow(self,win):pygame.draw.circle(win,(self.colour[0],self.colour[1],self.colour[2],0),(int(self.pos.x),int(self.pos.y)),self.size)defdecay(self):#randomdecayoftheparticlesif50>self.life>10:#earlystagetheirisasmallchanceofdecayran=randint(0,30)ifran==0:self.remove=Trueelifself.life>50:ran=randint(0,5)ifran==0:self.remove=Truedeftrail_update(self):self.prev_posx.pop()self.prev_posx.insert(0,int(self.pos.x))self.prev_posy.pop()self.prev_posy.insert(0,int(self.pos.y))forn,tinenumerate(self.trails):ift.dynamic:t.get_pos(self.prev_posx[n+dynamic_offset],self.prev_posy[n+dynamic_offset])else:t.get_pos(self.prev_posx[n+static_offset],self.prev_posy[n+static_offset])classTrail:def__init__(self,n,size,dynamic):self.pos_in_line=nself.pos=vector(-10,-10)self.dynamic=dynamicifself.dynamic:self.colour=trail_colours[n]self.size=int(size-n/2)else:self.colour=(255,255,200)self.size=size-2ifself.size<0:self.size=0defget_pos(self,x,y):self.pos=vector(x,y)defshow(self,win):pygame.draw.circle(win,self.colour,(int(self.pos.x),int(self.pos.y)),self.size)defupdate(win,fireworks):forfwinfireworks:fw.update(win)iffw.remove():fireworks.remove(fw)pygame.display.update()defmain():pygame.init()pygame.font.init()pygame.display.set_caption("FireworksinPygame")#标题background=pygame.image.load("img/1.png")#背景myfont=pygame.font.Font("img/simkai.ttf",80)myfont1=pygame.font.Font("img/simkai.ttf",30)testsurface=myfont.render("新年快乐",False,(251,59,85))testsurface1=myfont1.render("By:Python代码大全",False,(251,59,85))#pygame.image.load("")win=pygame.display.set_mode((DISPLAY_WIDTH,DISPLAY_HEIGHT))#win.blit(background)clock=pygame.time.Clock()fireworks=[Firework()foriinrange(2)]#createthefirstfireworksrunning=Truewhilerunning:clock.tick(60)foreventinpygame.event.get():ifevent.type==pygame.QUIT:running=Falseifevent.type==pygame.KEYDOWN:#Changegamespeedwithnumberkeysifevent.key==pygame.K_1:#按下1fireworks.append(Firework())ifevent.key==pygame.K_2:#按下2加入10个烟花foriinrange(10):fireworks.append(Firework())win.fill((20,20,30))#drawbackgroundwin.blit(background,(0,0))win.blit(testsurface,(200,30))win.blit(testsurface1,(520,80))ifrandint(0,20)==1:#createnewfireworkfireworks.append(Firework())update(win,fireworks)#statsforfun#total_particles=0#forfinfireworks:#total_particles+=len(f.particles)#print(f"Fireworks:{len(fireworks)}\nParticles:{total_particles}\n\n")pygame.quit()quit()main()

看完上述内容,你们对Python实现新年烟花秀的代码怎么写有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

发布于 2022-01-06 23:31:10
分享
海报
27
上一篇:Python的特性都有哪些 下一篇:.NET Core怎么实现Cookie和JWT混合认证、授权
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码