Here is my silly particle system code:
class Sparks:
def __init__(self, pos):
self.pos = pos
self.particles = tuple({
'life': 1.0,
'decay': random.uniform(0.5, 0.8),
'r': 1.0, 'g': 1.0, 'b': 1.0,
'x': 0.0, 'y': 0.0,
'vect': utils.normalizeVector2((random.uniform(-1.0,
1.0), random.uniform(-1.0, 1.0))),
'speed': random.uniform(0.0, 64.0)
} for i in xrange(64))
def draw(self):
glDisable(GL_DEPTH_TEST)
glPushMatrix()
glTranslatef(*self.pos)
glPointSize(5.0)
glEnable(GL_POINT_SMOOTH)
glBegin(GL_POINTS)
for p in self.particles:
if p['life'] > 0.0:
glColor4f(p['r'], p['g'], p['b'], p['life'])
glVertex2f(p['x'], p['y'])
glEnd()
glDisable(GL_POINT_SMOOTH)
glPopMatrix()
def update_task(self):
done = False
while not done:
time = yield
done = True
for p in self.particles:
if p['life'] > 0.0:
done = False
p['life'] -= p['decay'] * time
p['x'] += p['vect'][0] * p['speed'] * time
p['y'] += p['vect'][1] * p['speed'] * time
`update_task` is a generator, and meant to work with simple task
queue:
tasks = list()
reset_time = False
def append(task):
global reset_time
if not tasks:
pyglet.clock.schedule(update)
reset_time = False
task.next() # init generator
tasks.append(task)
def update(frame_time):
global reset_time
if not reset_time:
reset_time = True
frame_time = 0.0
for task in tasks[:]:
try:
task.send(frame_time)
except StopIteration:
tasks.remove(task)
if not tasks:
pyglet.clock.unschedule(update)
I have simple controller which put everything together like this:
sparks = Sparks((x, y, z))
scene.models.append(sparks) # to draw particles
tasks.append(sparks.update_task()) # to update particles
If someone interested I can post complete code, but don't know
convenient way to publish 10 files (that is the way i organize my
project, sorry).
-- serg
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---