Rendering particles in realtime with py-lepton
By: Jens in game development | pyglet | python
Last time i made a small demo using prerendered particle effects. Today i'll show another way of doing particle effects with python and pyglet. Instead of animating prerendered graphics i am going to use a particle system to create effects in realtime. The one i am using is py-lepton:
Lepton: A high-performance, pluggable particle engine and API for Python
Lepton is designed to make complex and beautiful particle effects possible, and even easy from Python programs.
Here is the demo of a simple explosion effect without using any premade texture. I made comments in the code that should explain everything.
The video:
Controls:
Left Mouse click : spawn new effect at center of the window
The Code:
from lepton import Particle, ParticleGroup, default_system from lepton.renderer import BillboardRenderer, PointRenderer from lepton.texturizer import SpriteTexturizer, create_point_texture from lepton.emitter import StaticEmitter from lepton.controller import Gravity, Lifetime, Movement, Fader, ColorBlender, Growth import pyglet from pyglet.gl import * #create pyglet window window = pyglet.window.Window(visible=False) window.clear() #enable alpha blending glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA,GL_ONE) def get_color(r,g,b,a): """ converts rgba values of 0 - 255 to the equivalent in 0 - 1""" return (r/255.0,g/255.0,b/255.0, a/255.0) # Setting up particle group for all explosion particles with some controllers # ColorBlending is from white to yellow to red/orange # For visuals us a Pointtexture generated by py-lepton explosion = ParticleGroup( controllers=[ Lifetime(4), Movement(damping=0.93), Growth(30), Fader( fade_in_start=0, start_alpha=0, fade_in_end=0.5 ,max_alpha=0.4, fade_out_start=1, fade_out_end=4.0), ColorBlender([(0, get_color(255, 255, 255,255)), (0.75, get_color(255,255,0,255)), (1.5, get_color(255,0,0,255)) ]) ], renderer=PointRenderer(64, SpriteTexturizer(create_point_texture(64,5)))) # The explosion emitter, the thing actually creating the particles # each particle starts at window center and add some variation (start position, size and # life time) explosion_emitter = StaticEmitter( template=Particle( position=(window.width/2,window.height/2,0), size=(20,20,0), ), deviation=Particle( position=(1,1,0), size=(10,10,0), velocity=(100,100,0), age=2 ), ) #if left mouse button is clicked a new explosion will be started @window.event def on_mouse_press(x, y, button, modifiers): if(pyglet.window.mouse.LEFT == button): explosion_emitter.emit(400, explosion) # clear screen and draw particles @window.event def on_draw(): window.clear() default_system.draw() # setup scheduler for particle updates and start pyglet if __name__ == '__main__': window.set_visible(True) pyglet.clock.schedule_interval(default_system.update, (1.0/30.0)) pyglet.clock.set_fps_limit(None) pyglet.app.run()
Now spice up the explosion without using any premade texture and share it.
Happy hacking :-)
Update 1: Fixed get_color method



on 26 September 2009 at 08:25 Lee Harr said …
I think this line:
return (r/255,g/255,b/255, a/255)
should be:
return (r/255.0,g/255.0,b/255.0, a/255.0)
on 26 September 2009 at 13:17 Kerim Mansour said …
Nice effect.
Why not set up a small challenge and see what the community can come up with as particle effects?