def test_artist_draw_center(self): block = pygame.surface.Surface((2, 2)) block.fill((255, 0, 0), pygame.Rect(0, 0, 2, 2)) ps = particle.ParticleSystem( particle.Particle(0, 1, 1, 0, 3, 'pixel'), particle.EmitterBurst.single(1), artist=particle.ArtistFadeOverlay(block, particle.CENTER, [(255, 255, 255, 255), (255, 255, 255, 255)]), ) world = pygame.surface.Surface((4, 4)) world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4)) desired_world = pygame.surface.Surface((4, 4)) desired_world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4)) for x in (0, 1): for y in (0, 1): # Ugh this is only 254 for some reason # get it together pygame desired_world.set_at((x, y), (254, 0, 0)) ps.update_state(1) ps.draw_on(world) print(world.get_at((1, 1))) assert (compare_surfaces(desired_world, world))
def test_brownian(self): # Not much we can test here, just that it works physics = particle.PhysicsJitter( x=1, jitter=particle.PhysicsJitter.brownian) p = particle.Particle(0, 0) physics(1, p) self.assertEquals(p.y, 0) # y unchanged :P
def test_particle_construction(self): p = particle.Particle(-1, +1, +2, -2, +5, 'spark') self.assertEquals(p.x, -1) self.assertEquals(p.y, +1) self.assertEquals(p.dx, +2) self.assertEquals(p.dy, -2) self.assertEquals(p.life, +5) self.assertEquals(p.species, 'spark')
def test_kick(self): physics = particle.PhysicsKick(x=2, y=5) p = particle.Particle(x=100, y=100) self.assertEquals(p.x, 100) self.assertEquals(p.y, 100) physics(1, p) self.assertEquals(p.x, 102) self.assertEquals(p.y, 105) physics(3, p) self.assertEquals(p.x, 108) self.assertEquals(p.y, 120)
def test_acceleration_radial(self): # Radial indicating +2 in dy direction physics = particle.PhysicsAcceleration.radial(2, 90) p = particle.Particle(0, 0, dx=100, dy=100) self.assertEquals(p.dx, 100) self.assertEquals(p.dy, 100) physics(1, p) self.assertEquals(p.dx, 100) self.assertEquals(p.dy, 102) physics(3, p) self.assertEquals(p.dx, 100) self.assertEquals(p.dy, 108)
def test_acceleration(self): # Accelerating +3 in dx direction physics = particle.PhysicsAcceleration(3, 0) p = particle.Particle(0, 0, dx=100, dy=100) self.assertEquals(p.dx, 100) self.assertEquals(p.dy, 100) physics(1, p) self.assertEquals(p.dx, 103) self.assertEquals(p.dy, 100) physics(3, p) self.assertEquals(p.dx, 112) self.assertEquals(p.dy, 100)
def test_draw_three_particles(self): # start it artist = ArtistNull() ps = particle.ParticleSystem( particle.Particle(0, -1, 1, 0, 3, 'giblet'), particle.EmitterBurst.single(3), artist=artist, ) # advance 1 second ps.update_state(1) ps.draw_on(None) self.assertEquals(artist.count, 3) self.assertEquals(artist.last_particle, ps.particles[-1])
def test_composite_alt_constructor(self): physics = particle.PhysicsComposite().add( particle.PhysicsKick(x=2), particle.PhysicsKick(y=5), ) p = particle.Particle(x=100, y=100) self.assertEquals(p.x, 100) self.assertEquals(p.y, 100) physics(1, p) self.assertEquals(p.x, 102) self.assertEquals(p.y, 105) physics(3, p) self.assertEquals(p.x, 108) self.assertEquals(p.y, 120)
def test_particle_death(self): # start it # Particles alive for 3 seconds, one generated every two seconds ps = particle.ParticleSystem( particle.Particle(0, -1, 1, 0, 3, 'giblet'), particle.EmitterBurst.repeat(1, 2)) ps.update_state(1) self.assertEquals(len(ps.particles), 1) print(ps.particles) ps.update_state(1) self.assertEquals(len(ps.particles), 2) print(ps.particles) p1, p2 = ps.particles ps.update_state(1) self.assertEquals(len(ps.particles), 1) print(ps.particles) self.assertIs(p2, ps.particles[0])
def test_one_particle(self): # start it ps = particle.ParticleSystem( particle.Particle(0, -1, 1, 0, 3, 'giblet'), particle.EmitterBurst.single(1)) self.assertEquals(len(ps.particles), 0) self.assertTrue(ps.is_alive()) # advance 1 second ps.update_state(1) self.assertEquals(len(ps.particles), 1, 'Particle not created') self.assertTrue(ps.is_alive()) p = ps.particles[0] self.assertEquals(p.x, 1) # Moved one unit in +x direction self.assertEquals(p.y, -1) # unchanged self.assertEquals(p.dx, 1) self.assertEquals(p.dy, 0) self.assertEquals(p.life, 2) # Lost one second of life self.assertEquals(p.species, 'giblet') # advance another second to t=2 ps.update_state(1) self.assertEquals(len(ps.particles), 1, 'Particle killed too early') self.assertTrue(ps.is_alive()) p = ps.particles[0] self.assertEquals(p.x, 2) # Moved another unit in +x direction self.assertEquals(p.y, -1) # unchanged self.assertEquals(p.dx, 1) self.assertEquals(p.dy, 0) self.assertEquals(p.life, 1) # Lost another second of life self.assertEquals(p.species, 'giblet') # advance another second to t=3 ps.update_state(1) self.assertEquals(len(ps.particles), 0, 'Particle not destroyed when life=0') self.assertFalse(ps.is_alive()) # Keep `p` from previous call self.assertEquals(p.x, 3) # Moved yet another unit in +x direction self.assertEquals(p.y, -1) # still unchanged self.assertEquals(p.dx, 1) self.assertEquals(p.dy, 0) self.assertEquals(p.life, 0) # Lost all life self.assertEquals(p.species, 'giblet')
def test_artist_draw_center(self): block = pygame.surface.Surface((2, 2)) block.fill((255, 0, 0), pygame.Rect(0, 0, 2, 2)) ps = particle.ParticleSystem( particle.Particle(0, 1, 1, 0, 3, 'pixel'), particle.EmitterBurst.single(1), artist=particle.ArtistSimple(block, particle.CENTER), ) world = pygame.surface.Surface((4, 4)) world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4)) desired_world = pygame.surface.Surface((4, 4)) desired_world.fill((0, 255, 0), pygame.Rect(0, 0, 4, 4)) for x in (0, 1): for y in (0, 1): desired_world.set_at((x, y), (255, 0, 0)) ps.update_state(1) ps.draw_on(world) assert (compare_surfaces(desired_world, world))
def test_jitter(self): # Jitter cycles between +1 & -1 times time delta jitter_iter = itertools.cycle((1, -1)) def jitter(dt): return dt * next(jitter_iter) physics = particle.PhysicsJitter(y=2, jitter=jitter) p = particle.Particle(0, 0) self.assertEquals(p.y, 0) # Jitter one second; uses jitter value 1 * 1 second * scale 2, +2 to y physics(1, p) self.assertEquals(p.y, 2) # Jitter 2s; uses jitter value -1 * 2 second * scale 2, -4 to y physics(2, p) self.assertEquals(p.y, -2) # Jitter 5s; uses jitter value 1 * 5 second * scale 2, +10 to y physics(5, p) self.assertEquals(p.y, 8)
def test_inertia(self): physics = particle.PhysicsInertia() p = particle.Particle(0, -1, 1, 0, 3, 'giblet') # advance 1 second physics(1, p) self.assertEquals(p.x, 1) # Moved one unit in +x direction self.assertEquals(p.y, -1) # unchanged self.assertEquals(p.dx, 1) self.assertEquals(p.dy, 0) self.assertEquals(p.life, 2) # Lost one second of life self.assertEquals(p.species, 'giblet') # advance another two seconds to t=3 physics(2, p) self.assertEquals(p.x, 3) # Moved yet another unit in +x direction self.assertEquals(p.y, -1) # still unchanged self.assertEquals(p.dx, 1) self.assertEquals(p.dy, 0) self.assertEquals(p.life, 0) # Lost all life self.assertEquals(p.species, 'giblet')
def test_particle_repr(self): p = particle.Particle(-1, +1, +2, -2, +5, 'spark') r = repr(p) self.assertEquals( r, 'Particle(-1, 1, dx=2, dy=-2, life=5, species=\'spark\')')