用python画烟花代码

用Python制作炫酷的烟花特效是一项有趣的编程项目。借助Python的图形库和数学计算功能,我们可以模拟出绚丽多彩的烟花效果。在本文中,我将为您介绍一种使用Python编写烟花特效的方法。

代码

#Fireworks Display - www.101computing.net/fireworks-display

from random import randint
from processing import *

numberOfParticules = 40;
position=[]
velocity=[]
lifespan=[]
color=1

def setup():
    size(300,300)
    frameRate(24)  
    background(0) 
    stroke(255)
    strokeWeight(2)
    global numberOfParticules
    global position
    global velocity
    global lifespan
    for i in range(0, numberOfParticules):
        position.append([0,0])
        if (i < numberOfParticules/2):
            velocity.append([randint(-2,2), randint(-10,-5)])
            lifespan.append(randint(20,40))
        else: 
            velocity.append([0,0])
            lifespan.append(randint(0,40))

def draw():
    global color
    global numberOfParticules    
    global position
    global velocity
    global lifespan    
  
    background(0)
    for i in range(0, numberOfParticules):
      color=i%5
      if color==0:
        stroke(255,255,255)    
      elif color==1:
        stroke(255,255,0)
      elif color==2:
        stroke(255,0,255)
      elif color==3:
        stroke(0,255,0)
      elif color==4:
        stroke(0,255,255)        
      
      point(position[i][0] + 150, position[i][1] + 300 )

      position[i][0]+=velocity[i][0]
      position[i][1]+=velocity[i][1]
      #Add Gravity
      velocity[i][1]+=0.2
      point(position[i][0] + 150, position[i][1] + 300 )
      
      #When a particules dies, another one appears
      lifespan[i]-=1
      if (lifespan[i] < 0): 
        velocity[i] = [randint(-2,2), randint(-10,-5)]
        position[i] = [0,0]
        lifespan[i] = randint(0,40)
        
        
def keyPressed():
    #print('A key was pressed', keyboard.key)
    #exitp()
    exit()

#The run() procedure below will call the setup() procedure followed by the draw() procedure.
#The draw() procedure will then be called 24 times per second (based on the frame rate set in the setup() procedure)
run()

运行截图

烟花代码编程python可复制天空爆炸的烟花python代码

#Fireworks Display - www.101computing.net/fireworks-display

from random import randint
from processing import *
import math

display=True
numberOfParticules=12
position=[]
velocity=[]
lifespan=randint(30,60)
color=1

def setup():
    size(300,300)
    frameRate(24)  
    background(0) 
    stroke(255)
    strokeWeight(2)
    global numberOfParticules
    global position
    global velocity
    global lifespan
    global display
    x=randint(-100,100)
    y=randint(-100,100)
    
    for i in range(0, numberOfParticules):    
      position.append([x,y])
      velocity.append([1,1])
      
def draw():
    global color
    global numberOfParticules    
    global position
    global velocity
    global lifespan 
    global display
  
    background(0)
    if display==True:
      for i in range(0, numberOfParticules):
        point(position[i][0] + 150, position[i][1] + 150 )
        position[i][0]+=velocity[i][0]*math.cos(math.radians(i*360/numberOfParticules))
        position[i][1]+=velocity[i][1]*math.sin(math.radians(i*360/numberOfParticules))
        point(position[i][0] + 150, position[i][1] + 150 )
      
      lifespan-=1
      
      if (lifespan < 0): 
          display = False  

def keyPressed():
    #print('A key was pressed', keyboard.key)
    #exitp()
    global display
    global color
    global position
    global velocity
    global lifespan 
    display=True
    color=randint(1,5)
    if color==0:
      stroke(255,255,255)     
    elif color==1:
      stroke(255,255,0)
    elif color==2:
      stroke(255,0,255)
    elif color==3:
      stroke(0,255,0)
    elif color==4:
      stroke(0,255,255)   
    
    x=randint(-100,100)
    y=randint(-100,100)  
    for i in range(0, numberOfParticules):
      velocity[i] = [1,1]
      position[i] = [x,y]
      lifespan = randint(30,60)      
    
    exit()

#The run() procedure below will call the setup() procedure followed by the draw() procedure.
#The draw() procedure will then be called 24 times per second (based on the frame rate set in the setup() procedure)
run()

通过运行以上代码,您将看到一个简单的烟花特效在窗口中展示。每次运行程序,可能会看到不同的烟花效果,这取决于粒子的随机属性。

希望以上代码示例能帮助您理解如何使用Python制作烟花特效。您可以根据自己的需求和创意进一步扩展和优化代码,实现更加炫酷的效果。祝您编程愉快!