def test_delete_out_of_field(self): self.bullets.add(Bullet(0, 0, 1, 1, 1)) self.bullets.delete_out_of_field(self.field) self.assertEqual(2, len(self.bullets.arr)) self.bullets.add(Bullet(-10, -10, 1, 1, 1)) self.bullets.delete_out_of_field(self.field) self.assertEqual(2, len(self.bullets.arr))
def test_one_fire_an_angle(self): for angle in range(15, 170, 15): self.ship_bullet = Bullet(15, 15, 1, angle, 1) self.ship_bullet.fire(Rectangle(0, 0, 30, 30)) self.assertLess((15 + math.cos(angle * math.pi / 180) - self.ship_bullet.x_left), 10e-5) self.assertLess((15 - math.sin(angle * math.pi / 180) - self.ship_bullet.y_top), 10e-5)
def test_ricochet_from_up(self): self.bullet = Bullet(0, 3, 1, 45, 1, True) for i in range(10): self.bullet.fire(Rectangle(0, 0, 20, 20)) self.assertEqual(self.bullet.angle, -45 * math.pi / 180) self.assertEqual(self.bullet.module_vector, 5) self.assertLess(10 * math.cos(45 * math.pi / 180) - self.bullet.x_left, 10e-5) self.assertLess(self.bullet.y_top - 3, 10e-5)
def test_ricochet_from_left(self): self.bullet = Bullet(15, 15, 1, 45, 1, True) for i in range(10): self.bullet.fire(Rectangle(0, 0, 19, 19)) self.assertEqual(self.bullet.angle, math.pi - 45 * math.pi / 180) self.assertEqual(self.bullet.module_vector, 5) self.assertLess(self.bullet.x_left - 15, 10e-5) self.assertLess( 15 - 10 * math.sin(45 * math.pi / 180) - self.bullet.y_top, 10e-5)
def test_fire_30(self): self.ship_bullet = Bullet(15, 15, 1, 30, 1) for i in range(10): self.ship_bullet.fire(Rectangle(0, 0, 25, 25)) self.assertLess( (15 + 10 * math.cos(30 * math.pi / 180) - self.ship_bullet.x_left), 10e-5) self.assertLess( (15 - 10 * math.sin(30 * math.pi / 180) - self.ship_bullet.y_top), 10e-5)
def Update(): global spawntimer if spawntimer % 5 == 0: bullet = Bullet(0, 0, "s.png") bullet.SetVar('angle', 0) bullet.SetVar('radius', 0) objects.append(bullet) ##for recognition for bullet in objects: angle = bullet.GetVar('angle') radius = bullet.GetVar('radius') bullet.MoveTo(math.cos(angle) * radius, math.sin(angle) * radius) bullet.SetVar('angle', angle + math.pi / 32) bullet.SetVar('radius', radius + 3) spawntimer += 1 return None
class TestInvaderBullet(unittest.TestCase): def setUp(self): self.bullet = Bullet(20, 30, 2, 70, 10, True) def test_bullet_in_field(self): self.assertTrue(self.bullet.in_field(Rectangle(0, 0, 50, 50))) self.assertFalse(self.bullet.in_field(Rectangle(0, 0, 10, 10))) def test_kill_ship(self): self.assertTrue(self.bullet.kill_ship(Ship(30, 30, 10, 10, 5))) self.assertFalse(self.bullet.kill_ship(Ship(40, 30, 20, 20, 10))) def test_kill_invader(self): self.assertFalse( self.bullet.kill_invader(Invader(20, 30, 10, 10, 5, 1, 1, 1))) def test_kill_mystery_ship(self): self.assertFalse( self.bullet.kill_mystery_ship(MysteryShip(100, 100, 1, 1))) def test_ricochet_from_left(self): self.bullet = Bullet(15, 15, 1, 45, 1, True) for i in range(10): self.bullet.fire(Rectangle(0, 0, 19, 19)) self.assertEqual(self.bullet.angle, math.pi - 45 * math.pi / 180) self.assertEqual(self.bullet.module_vector, 5) self.assertLess(self.bullet.x_left - 15, 10e-5) self.assertLess( 15 - 10 * math.sin(45 * math.pi / 180) - self.bullet.y_top, 10e-5) def test_ricochet_from_up(self): self.bullet = Bullet(0, 3, 1, 45, 1, True) for i in range(10): self.bullet.fire(Rectangle(0, 0, 20, 20)) self.assertEqual(self.bullet.angle, -45 * math.pi / 180) self.assertEqual(self.bullet.module_vector, 5) self.assertLess(10 * math.cos(45 * math.pi / 180) - self.bullet.x_left, 10e-5) self.assertLess(self.bullet.y_top - 3, 10e-5)
def start(): global spawntimer,objects spawntimer=0 objects=[] bullet=Bullet(0,0,"s.png") objects.append(bullet)
def Update(): global spawntimer,mult spawntimer = spawntimer + 1 if(spawntimer % 300 == 0): for i in range(1,11): bullet = Bullet(0, 180,"s.png") bullet.SetVar('timer', 0) bullet.SetVar('offset', math.pi * i/5) bullet.SetVar('negmult', mult) bullet.SetVar('lerp', 0) objects.append(bullet) mult+=0.05 for bullet in objects: timer = bullet.GetVar('timer') offset = bullet.GetVar('offset') lerp = bullet.GetVar('lerp') posx = (70*lerp)*math.sin(timer*bullet.GetVar('negmult') + offset) posy = (70*lerp)*math.cos(timer + offset) + 180 - lerp*50 bullet.MoveTo(posx, posy) bullet.SetVar('timer', timer + 1/40) lerp+= 1 / 90 if lerp > 4: lerp=4 bullet.SetVar('lerp', lerp)
def setUp(self): self.ship_bullet = Bullet(15, 15, 1, 90, 5)
class TestShipBullet(unittest.TestCase): def setUp(self): self.ship_bullet = Bullet(15, 15, 1, 90, 5) def test_get_bottom(self): self.assertEqual(20, self.ship_bullet.y_bottom()) def test_get_right(self): self.assertEqual(20, self.ship_bullet.x_right()) def test_get_centre(self): self.assertEqual(Point(17.5, 17.5), self.ship_bullet.centre()) def test_ship_bullet_in_field(self): self.assertTrue(self.ship_bullet.in_field(Rectangle(0, 0, 50, 50))) self.assertFalse(self.ship_bullet.in_field(Rectangle(0, 0, 10, 10))) def test_fire_90(self): for i in range(10): self.ship_bullet.fire(Rectangle(0, 0, 30, 30)) self.assertEqual(self.ship_bullet.y_top, 14 - i) self.assertEqual(self.ship_bullet.x_left, 15) def test_one_fire_an_angle(self): for angle in range(15, 170, 15): self.ship_bullet = Bullet(15, 15, 1, angle, 1) self.ship_bullet.fire(Rectangle(0, 0, 30, 30)) self.assertLess((15 + math.cos(angle * math.pi / 180) - self.ship_bullet.x_left), 10e-5) self.assertLess((15 - math.sin(angle * math.pi / 180) - self.ship_bullet.y_top), 10e-5) def test_fire_30(self): self.ship_bullet = Bullet(15, 15, 1, 30, 1) for i in range(10): self.ship_bullet.fire(Rectangle(0, 0, 25, 25)) self.assertLess( (15 + 10 * math.cos(30 * math.pi / 180) - self.ship_bullet.x_left), 10e-5) self.assertLess( (15 - 10 * math.sin(30 * math.pi / 180) - self.ship_bullet.y_top), 10e-5) def test_ricochet_from_left(self): self.ship_bullet = Bullet(15, 15, 1, 45, 1) for i in range(10): self.ship_bullet.fire(Rectangle(0, 0, 19, 19)) self.assertEqual(self.ship_bullet.angle, math.pi - 45 * math.pi / 180) self.assertEqual(self.ship_bullet.module_vector, 5) self.assertEqual(self.ship_bullet.x_left, 15) self.assertLess( 15 - 10 * math.sin(45 * math.pi / 180) - self.ship_bullet.y_top, 10e-5) def test_kill_ship_ship_bullet(self): self.assertFalse(self.ship_bullet.kill_ship(Ship(15, 15, 20, 20, 5)))
def setUp(self): self.bullet = Bullet(20, 30, 2, 70, 10, True)
def setUp(self): self.field = Rectangle(0, 0, 30, 30) self.bullets = Bullets() self.bullet = Bullet(0, 0, 5, 10, 10) self.bullets.add(self.bullet)
def test_add(self): bullet = Bullet(10, 30, 15, 0, 30) self.bullets.add(bullet) self.assertTrue(bullet in self.bullets.arr)