class Kaboom: lifetime = 0.6 color = (0.9, 0.6, 0.2, 0.3) spark_tex = pyglet.resource.texture('bullet.png') spark_texturizer = SpriteTexturizer(spark_tex.id) sparks = ParticleGroup(controllers=[ Lifetime(lifetime), Movement(damping=0.93), ColorBlender([(0, (1, 1, 1, 1)), (lifetime * 0.8, color), (lifetime, color)]), Fader(fade_out_start=0.3, fade_out_end=lifetime), ], renderer=BillboardRenderer(spark_texturizer)) trails = ParticleGroup(controllers=[ Lifetime(lifetime * 1.5), Movement(damping=0.83), ColorBlender([(0, (1, 1, 1, 1)), (1, color), (lifetime, color)]), Fader(max_alpha=0.75, fade_out_start=0, fade_out_end=lifetime), ], renderer=BillboardRenderer(spark_texturizer)) splosions = set() def __init__(self, position): x, y = position spark_emitter = StaticEmitter( template=Particle(position=(uniform(x - 5, x + 5), uniform(y - 5, y + 5), 0), size=(10, ) * 3, color=self.color), deviation=Particle(velocity=(gauss(0, 5), gauss(0, 5), 0), age=1.5), velocity=domain.Sphere((0, gauss(40, 20), 0), 60, 60)) spark_emitter.emit(int(gauss(60, 40)) + 50, self.sparks) spread = abs(gauss(0.4, 1.0)) self.trail_emitter = PerParticleEmitter( self.sparks, rate=uniform(5, 30), template=Particle(size=(6, ) * 3, color=self.color), deviation=Particle(velocity=(spread, spread, spread), age=self.lifetime * 0.75)) self.trails.bind_controller(self.trail_emitter) self.splosions.add(self) pyglet.clock.schedule_once(self.die, self.lifetime) def die(self, dt=None): self.trails.unbind_controller(self.trail_emitter) self.splosions.remove(self)
class Debris: lifetime = 0.5 color = (1.0, 1.0, 1.0, 1.0) fragment_tex = pyglet.resource.texture('fragment.png') fragment_texturizer = SpriteTexturizer(fragment_tex.id) fragments = ParticleGroup(controllers=[ Lifetime(lifetime), Movement(), ], renderer=BillboardRenderer(fragment_texturizer), system=diffuse_system) @classmethod def emit(cls, position): emitter = StaticEmitter(template=Particle(position=(*position, 0), size=(16, ) * 3, rotation=(0, 0, 1), color=cls.color), deviation=Particle(age=0.2, rotation=(0, 0, 2)), velocity=domain.Disc((0, 0, 0), (0, 0, 1), 100)) emitter.emit(20, cls.fragments)
class Impact: lifetime = 0.5 color = (0.9, 0.6, 0.2, 0.3) spark_tex = pyglet.resource.texture('bullet.png') spark_texturizer = SpriteTexturizer(spark_tex.id) sparks = ParticleGroup(controllers=[ Lifetime(lifetime), Movement(), ColorBlender([(0, (1, 1, 1, 1)), (lifetime * 0.8, color), (lifetime, (0, 0, 0, 0))]), ], renderer=BillboardRenderer(spark_texturizer)) @classmethod def emit(cls, position, velocity): x, y = position + velocity * 0.3 emitter = StaticEmitter(template=Particle(position=(x, y, 0), size=(5, ) * 3, color=cls.color), deviation=Particle(age=0.2), velocity=domain.Disc((*-2 * velocity, 0), (0, 0, 1), 100)) emitter.emit(10, cls.sparks)
def create_group(self, controllers, texture): particlegroup = ParticleGroup(controllers=controllers, system=self.system) texturizer = SpriteTexturizer(texture.id) particlegroup.renderer = BillboardRenderer(texturizer) self.textures.add( texture) # hold a reference to this, otherwise it will get deleted return particlegroup
def explosion(self, pos, magnitude): sparks = ParticleGroup( controllers=[ Lifetime(3), Movement(damping=0.93), Fader(fade_out_start=0.75, fade_out_end=3.0), ], renderer=BillboardRenderer(SpriteTexturizer(self.particle1_tex.id))) spark_emitter = StaticEmitter( template=Particle( position=(pos[0], pos[1], 0), color=(1, 1, 1)), deviation=Particle( position=(1, 1, 0), velocity=(300, 300, 0), age=1.5), size=[(3, 3, 0), (4, 4, 0), (5, 5, 0), (5, 5, 0), (6, 6, 0), (7, 7, 0)]) spark_emitter.emit(magnitude, sparks) fire = ParticleGroup( controllers=[ Lifetime(2), Movement(damping=0.95), Growth(60), Fader(fade_in_start=0, start_alpha=0, fade_in_end=0.5, max_alpha=0.6, fade_out_start=1.0, fade_out_end=2.0) ], renderer=BillboardRenderer(SpriteTexturizer(self.smoke1_tex.id))) fire_emitter = StaticEmitter( template=Particle( position=(pos[0], pos[1], 0), size=(50, 50, 0)), deviation=Particle( position=(0.5, 0.5, 0), velocity=(70, 70, 0), size=(20, 20, 0), up=(0, 0, math.pi * 2), rotation=(0, 0, math.pi * 0.06), age=2, ), color=[(0.5, 0, 0), (0.5, 0.5, 0.5), (0.4, 0.1, 0.1), (0.85, 0.3, 0)], ) fire_emitter.emit(400, fire)
def particles(self): self.part = ParticleGroup(renderer=BillboardRenderer(texturizer2), controllers=[ Movement(max_velocity=self.speed, damping=0.95), Magnet(self.objective, charge=500, exponent=0), ]) self.emiter = StaticEmitter(position=self.domain, template=Particle( color=self.color, size=(self.size, self.size, 0), )) self.emiter.emit(1, self.part)
class Trail: LIFETIME = 0.2 sprite = pyglet.resource.texture('trail.png') group = ParticleGroup( controllers=[ Lifetime(LIFETIME), #Fader(start_alpha=1.0, fade_out_start=0, fade_out_end=LIFETIME), Growth(-50), Movement(), ], renderer=BillboardRenderer(SpriteTexturizer(sprite.id))) def __init__(self, player, viewport, level): self.player = player self.viewport = viewport self.level = level self.emitter = StaticEmitter( rate=player.body.velocity.length, template=Particle( position=(*level.map_to_world(player.position), 0), color=(1.0, 1.0, 1.0, 1.0), size=(16.0, ) * 3, ), ) self.group.bind_controller(self.emitter) pyglet.clock.schedule(self.update) def destroy(self): self.group.unbind_controller(self.emitter) def update(self, *_): level = self.level dir = self.player.body.velocity l = dir.length self.emitter.rate = l * 2 if self.player.health > 0 else 0 if l: back = self.player.position - dir.normalized() * 0.1 backwards = -0.1 * dir self.emitter.template.position = (*level.map_to_world(back), 0) self.emitter.template.velocity = (*level.map_to_world(backwards), 0) self.emitter.template.up = (0, 0, dir.get_angle() - self.viewport.angle)
def emit_sparks(self, pos, count): sparks = ParticleGroup( controllers=[ Lifetime(1), Movement(damping=0.93), Fader(fade_out_start=0.75, fade_out_end=1.0), ], renderer=BillboardRenderer(SpriteTexturizer(self.particle1_tex.id))) spark_emitter = StaticEmitter( template=Particle( position=(pos[0], pos[1], 0), color=(1, 1, 1)), deviation=Particle( position=(1, 1, 0), velocity=(100, 100, 0), age=1.5), size=[(6, 6, 0), (7, 7, 0), (12, 12, 0)]) spark_emitter.emit(count, sparks)
class Smoke: LIFETIME = 0.8 sprite = pyglet.resource.texture('smoke.png') group = ParticleGroup(controllers=[ Lifetime(LIFETIME), Fader(start_alpha=0.3, fade_out_start=0, fade_out_end=LIFETIME), Growth(100), Movement(), ], renderer=BillboardRenderer( SpriteTexturizer(sprite.id)), system=diffuse_system) def __init__(self, wpos): self.last_pos = wpos self.domain = Cylinder((*wpos, -10), (*wpos, 0), 5) self.emitter = StaticEmitter(rate=600, position=self.domain, template=Particle( color=(1.0, 1.0, 1.0, 0.3), size=(2.0, ) * 3, rotation=(0, 0, 1), velocity=(0, 0, 0), ), deviation=Particle( rotation=(0, 0, 0.5), angle=(0, 0, 6), )) self.group.bind_controller(self.emitter) def destroy(self): self.group.unbind_controller(self.emitter) def set_world_position(self, wpos, velocity): pos = Vec2d(wpos) back = pos - velocity self.domain.end_point1 = (*back, 0) self.domain.end_point0 = (*pos, 0) self.emitter.template.velocity = (*(-2 * velocity), 0)
glEnable(GL_BLEND) glShadeModel(GL_SMOOTH) glBlendFunc(GL_SRC_ALPHA, GL_ONE) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) glDisable(GL_DEPTH_TEST) spark_tex = image.load(os.path.join(os.path.dirname(__file__), 'flare3.png')).get_texture() sparks = ParticleGroup(controllers=[ Lifetime(3), Movement(damping=0.93), Fader(fade_out_start=0.75, fade_out_end=3.0), ], renderer=BillboardRenderer( SpriteTexturizer(spark_tex.id))) spark_emitter = StaticEmitter(template=Particle(position=(win.width / 2, win.height / 2, 0), color=(1, 1, 1)), deviation=Particle(position=(1, 1, 0), velocity=(300, 300, 0), age=1.5), size=[(3, 3, 0), (4, 4, 0), (5, 5, 0), (5, 5, 0), (6, 6, 0), (7, 7, 0)]) spark_emitter.emit(400, sparks) fire_tex = image.load(os.path.join(os.path.dirname(__file__), 'puff.png')).get_texture() fire = ParticleGroup(controllers=[
up=(0, 0, math.pi), rotation=(0, 0, math.pi), color=(0.5, 0.5, 0.5))) default_system.add_global_controller( Lifetime(3.0), Movement(min_velocity=5), Fader(max_alpha=0.7, fade_out_start=1, fade_out_end=3.0), ) images = [ image.load(os.path.join(os.path.dirname(__file__), 'flare%s.png' % (i + 1))) for i in range(4) ] group = ParticleGroup(controllers=[comet], renderer=BillboardRenderer( SpriteTexturizer.from_images(images))) pyglet.clock.schedule_interval(default_system.update, (1.0 / 30.0)) pyglet.clock.set_fps_limit(None) arc_radius = 150 angle = math.pi * 0.7 speed = 1.0 def move_comet(td): global angle, arc_radius, speed comet.template.position = (-math.sin(angle) * arc_radius * 0.3, math.sin(angle * 0.7) * arc_radius * 0.03, -math.cos(angle) * arc_radius - arc_radius * 1.05) comet.template.velocity = (comet.template.position.x * 0.05 -
rotation=(0, 0, math.pi), color=(0.5, 0.5, 0.5)) ) # Add group to ball ball.add_particle_group(comet) default_system.add_global_controller( Lifetime(1.75), # Gravity((0,-20,0)), Movement(min_velocity=20), Fader(max_alpha=0.7, fade_out_start=1, fade_out_end=1.75), ) group_tex = [] for i in range(4): group = ParticleGroup( controllers=[comet], renderer=BillboardRenderer()) try: texture = image.load(os.path.join(os.path.dirname(__file__), 'flare1.png')).texture # 'flare%s.png' % (i+1))).get_mipmapped_texture() except NotImplementedError: # Problem with mips not being implemented everywhere (cygwin?) texture = image.load(os.path.join(os.path.dirname(__file__), 'flare1.png')).texture group_tex.append((group, texture)) win.resize = resize win.set_visible(True) win.resize(win.width, win.height) ball_size = 15 screen_box = Boundary((ball_size / 2.0, ball_size / 2.0, 0), (win.width - ball_size / 2.0, win.height - ball_size / 2.0, 0))
win.on_resize = on_resize glClearColor(1, 1, 1, 1) glEnable(GL_BLEND) glShadeModel(GL_SMOOTH) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glDisable(GL_DEPTH_TEST) logo_files = glob.glob( os.path.join(os.path.dirname(__file__), 'logo_frames/logo*.png')) images = [pyglet.image.load(f) for f in sorted(logo_files)] texturizer = FlipBookTexturizer.from_images(images + images[-1::-1], 0.075) group = ParticleGroup( controllers=[Fader(fade_out_start=50.0, fade_out_end=58.5)], renderer=BillboardRenderer(texturizer)) template = Particle( size=(1, 1, 0), color=(1, 1, 1, 1), ) positions = set([((x - 25) * 1.4 + (y % 2.0) * 0.7, (y - 25) * 1.4, (z - 25) * 1.4 + (y % 2.0) * 0.7) for x in range(50) for y in range(50) for z in range(50)]) group.new(template) for i in range(12000): group.new(template, position=positions.pop(), age=49.35) win.set_visible(True) pyglet.clock.schedule_interval(default_system.update, (1.0 / 40.0)) pyglet.clock.set_fps_limit(None)
glMatrixMode(GL_MODELVIEW) glLoadIdentity() win.on_resize = resize electron_lifetime = 22 max_electrons = 6 trail_lifetime = 4.5 texture = image.load(os.path.join(os.path.dirname(__file__), 'flare3.png')).get_texture() texturizer = SpriteTexturizer(texture.id) nucleus = Sphere((0, 0, 0), 5) protons = ParticleGroup(renderer=BillboardRenderer(texturizer), controllers=[ Movement(), ]) proton_emitter = StaticEmitter(template=Particle( size=(30, 30, 0), color=(0.5, 1.0, 0.2, 0.5), ), size=[(26, 26, 0), (30, 30, 0), (34, 34, 0)], deviation=Particle(rotation=(0, 0, math.pi / 6), )) proton_emitter.emit(3, protons) electrons = ParticleGroup(renderer=BillboardRenderer(texturizer), controllers=[
# We should now be able to define all the particle engine stuff # without code changes to the rest of the game exhaust = load_texture('exhaust.png') exhaust_particles = ParticleGroup( controllers=[ controller.Movement(), controller.Lifetime(1), controller.ColorBlender([ (0.0, (1.0, 0.3, 0.3, 0.3)), (0.2, (1.0, 0.8, 0.3, 0.3)), (0.5, (1.0, 1.0, 1.0, 0.2)), (1.0, (1.0, 1.0, 1.0, 0.0)), ]), controller.Growth(-3) ], renderer=BillboardRenderer(SpriteTexturizer(exhaust.id)), ) explosion_particles = ParticleGroup( controllers=[ controller.Movement(), controller.Lifetime(2), controller.Fader(start_alpha=1, fade_out_start=1, fade_out_end=2, end_alpha=0.0) ], renderer=BillboardRenderer(SpriteTexturizer(exhaust.id)), )
def __init__(self, x, y, z, r, side=-1, hp=50, controlable=False, weapon_range=5, dispersion=5, agility=50, weapon_base_damage=2, guidance=100, shortguide=0, partColor=(0.6, 0.5, 0.2, 1), firerate=10, shots=1, vo=30, maxvel=10, ammoMaxvel=20, combatDistance=50, behavior=0, commander=None, multipleTargets=False, name="", ammoDamp=0.98): self.name = name self.domain = Sphere( (x, y, z), r ) # a.center -> vector del centro , a.outer_radius -> radio externo , a.inner_radius -> radio interno self.size = r self.controller = Collector(self.domain, callback=self.contact) self.magnet = Magnet(self.domain, charge=guidance, exponent=shortguide) self.commander = commander self.mission = self.domain.center self.target = None self.alive = True self.targetMode = ['standard', 1] self.behavior = behavior # 0:free 1: escort 2: slave self.hp = hp self.agility = agility self.maxvel = maxvel self.timer = {0: 0, 1: 2, 2: 0, 3: 0, 4: 0} # timers placeholder self.counter = {0: 0, 1: 0} # counters placeholder self.side = side self.combatDistance = combatDistance self.velocity = Vec3(0, 0, 0) self.multipleTargets = multipleTargets self.firerate = firerate self.weapon_base_damage = weapon_base_damage wbd = self.weapon_base_damage rr = r * 2 self.dispersion = dispersion self.vo = vo self.ammoDamp = ammoDamp self.ammoMaxvel = ammoMaxvel self.shots = shots self.weapon_range = weapon_range self.xx = self.yy = self.zz = 0 self.Objective = Sphere((0, 0, 0), 1) self.color = partColor # (0.4,0.5,0.4,0.5) #self.prevController = Collector(self.domain)#DUMMY CONTROLLER self.controlable = controlable self.impacto = ParticleGroup(renderer=BillboardRenderer(texturizer), controllers=[ Lifetime(1), Fader(fade_out_start=0, fade_out_end=1), ]) self.deathplosion = ParticleGroup( renderer=BillboardRenderer(texturizer), controllers=[ Lifetime(self.size / 5 + 1), Fader(fade_out_start=0, fade_out_end=self.size / 5 + 1), ]) self.selector_emitter = StaticEmitter(template=Particle( position=(0, 0, 0), color=self.color, )) self.impacto_emitter = StaticEmitter( template=Particle( position=(0, 0, 0), color=(0.9, 0.8, 0.8), ), position=self.domain, #size=[(5, 5, 5), (10, 10, 10), (15, 15, 15)], ) self.hull = ParticleGroup(renderer=BillboardRenderer(texturizer2), controllers=[ Lifetime(100000), Movement(max_velocity=self.maxvel, damping=0.98), Magnet(self.Objective, charge=self.agility, exponent=0), ]) emiter = StaticEmitter(position=self.domain, template=Particle( color=self.color, size=(rr, rr, rr), )) emiter.emit(1, self.hull) if trails: if maxvel / r >= 20: self.trail = ParticleGroup( renderer=BillboardRenderer(texturizer2), controllers=[ Lifetime(trailSize[0]), Fader(fade_in_start=0, fade_in_end=0.1, fade_out_start=0, fade_out_end=trailSize[0]), Growth(-1 * r), PerParticleEmitter(self.hull, rate=trailSize[1], template=Particle( color=self.color, size=(rr, rr, rr), )), ]) self.ammo = ParticleGroup(renderer=BillboardRenderer(texturizer), controllers=[ self.magnet, Movement(min_velocity=0, max_velocity=self.ammoMaxvel, damping=self.ammoDamp), Lifetime(self.weapon_range), Fader(fade_out_start=self.weapon_range - 1, fade_out_end=self.weapon_range), ]) self.weapon = PerParticleEmitter( self.hull, # rate=self.firerate, template=Particle( velocity=self.velocity, # fixed value position=(self.getPosition()), color=partColor, ), position=self.domain, size=[(wbd * 0.5, wbd * 0.5, wbd * 0.5), (wbd, wbd, wbd), (wbd * 1.5, wbd * 1.5, wbd * 1.5)], deviation=Particle( velocity=(self.dispersion, self.dispersion, self.dispersion * d3), rotation=(0, 0, math.pi / 6), #color=(0.05,0.05,0.05,0), ))
deviation=Particle(velocity=(0, 0, 15), up=(0, 0, math.pi), color=(0.1, 0.1, 0.1))) default_system.add_global_controller( Movement(max_velocity=10), Collector(viewer_plane), Gravity((0, 0, 15)), Growth(0.17), Fader(fade_in_end=0, max_alpha=0.3, fade_out_start=0, fade_out_end=8.0), ) texture = image.load(os.path.join(os.path.dirname(__file__), 'Particle.bmp')).get_texture() group = ParticleGroup(controllers=[jet], renderer=BillboardRenderer(SpriteTexturizer(texture.id))) default_system.run_ahead(5, 40) pyglet.clock.schedule_interval(default_system.update, (1.0 / 30.0)) pyglet.clock.set_fps_limit(None) time = 0 def ring(dt): """Emit a ring of particles periodically""" jet.emit(1000, group) pyglet.clock.schedule_interval(ring, 5)
up=(0,0,math.pi), rotation=(0,0,math.pi), color=(0.5, 0.5, 0.5)) ) #Add group to ball ball.add_particle_group(comet) default_system.add_global_controller( Lifetime(1.75), #Gravity((0,-20,0)), Movement(min_velocity=20), Fader(max_alpha=0.7, fade_out_start=1, fade_out_end=1.75), ) group_tex = [] for i in range(4): group = ParticleGroup(controllers=[comet], renderer=BillboardRenderer()) try: texture = image.load(os.path.join(os.path.dirname(__file__), 'flare1.png')).texture#'flare%s.png' % (i+1))).get_mipmapped_texture() except NotImplementedError: #Problem with mips not being implemented everywhere (cygwin?) texture = image.load(os.path.join(os.path.dirname(__file__), 'flare1.png')).texture group_tex.append((group, texture)) win.resize = resize win.set_visible(True) win.resize(win.width, win.height) ball_size = 15 screen_box = Boundary((ball_size/2.0, ball_size/2.0, 0), (win.width-ball_size/2.0,win.height-ball_size/2.0,0))