import sys, time, math from pyglet.gl import * from euclid import * from random import * window = pyglet.window.Window() gravity = Vector3(0, -200, 0) class particle: def __init__(self, pos): self.pos = pos.copy() self.vel = Vector3(uniform(-120,120), 400, 0) self.color = [uniform(.5,1), uniform(0,1), uniform(0,0.5)] self.removeTime = time.time() + uniform(0.5,2) def update(self, dt): self.pos += self.vel * dt self.vel += gravity * dt class particleSystem: def __init__(self): self.particles = [] self.addParticles(4) self.addTime = 0 def addParticles(self, num): for i in range(0,num): p = particle(Vector3(300,0,0)) self.particles.append(p) def draw(self): glColor3f(1,1,1) glBegin(GL_LINES) for p in self.particles: glColor3fv((GLfloat*3)(*p.color)) glVertex3fv((GLfloat*3)(*p.pos)) tail = p.pos + p.vel * 0.1 glVertex3fv((GLfloat*3)(*tail)) glEnd() def update(self, dt): for p in self.particles: p.update(dt) t = time.time() for i in range(len(self.particles)-1, -1, -1): if self.particles[i].removeTime <= t: systems.append(explodeSystem(self.particles[i].pos)) del self.particles[i] elif self.particles[i].pos[1] < 0: del self.particles[i] self.addTime += dt if self.addTime > 0.25: self.addTime = 0 self.addParticles(1) class explodeparticle: def __init__(self, pos): self.pos = pos.copy() dir = uniform(0,math.pi*2) self.vel = Vector3(math.sin(dir), math.cos(dir), 0) * 120 self.color = [uniform(.5,1), uniform(0,1), uniform(0,0.5)] self.removeTime = time.time() + uniform(0.5,2) def update(self, dt): self.pos += self.vel * dt self.vel += gravity * dt class explodeSystem: def __init__(self,pos): self.particles = [] self.addParticles(20,pos) def addParticles(self, num, pos): for i in range(0,num): p = explodeparticle(pos) self.particles.append(p) def draw(self): glColor3f(1,1,1) glLineWidth(3.0) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE) glBegin(GL_LINES) for p in self.particles: glColor4f(p.color[0], p.color[1], p.color[2], 0) glVertex3fv((GLfloat*3)(*p.pos)) glColor4f(p.color[0], p.color[1], p.color[2], 1) glVertex3fv((GLfloat*3)(*p.pos + p.vel*0.15)) glEnd() glLineWidth(1.0) glDisable(GL_BLEND) def update(self, dt): for p in self.particles: p.update(dt) t = time.time() for i in range(len(self.particles)-1, -1, -1): if self.particles[i].removeTime <= t: del self.particles[i] systems = [particleSystem()] @window.event def on_draw(): glClearColor(0,0.2,0.5,0) glClear(GL_COLOR_BUFFER_BIT) for s in systems: s.draw() def update(dt): for s in systems: s.update(dt) for i in range(len(systems)-1, -1, -1): if len(systems[i].particles) == 0: del systems[i] print len(systems), 'particle systems' pyglet.clock.schedule_interval(update,1/60.0) pyglet.app.run()