def test_collision_asteroid_collides_ship(self): ship = GamePiece.Ship(size=1.,position=(0., 0., 2.)) asteroid = GamePiece.Asteroid1(size=1., position=(0., 0., 4.), start_velocity=(1., 1.), angular_velocity=-10.) self.gameboard._add_ship(ship) self.gameboard._add_asteroid(asteroid) self.gameboard._resolve_collision() self.assertTrue(self.gameboard.gameover)
def test_collision_asteroid_collides_projectile(self): projectile = GamePiece.Projectile(1.0, (0., 0., 0.), (1., 0.)) asteroid = GamePiece.Asteroid1(size=1., position=(0., 0., 4.), start_velocity=(1., 1.), angular_velocity=-10.) self.gameboard._add_asteroid(asteroid) self.gameboard._add_projectile(projectile) self.gameboard._resolve_collision() self.assertNotIn(asteroid, self.gameboard._asteroids) self.assertNotIn(projectile, self.gameboard._projectiles)
def test_col_poly_pt(self): pt1 = GamePiece.GamePiece(1.0, 'point', position=(1., 1., 0.)) pt2 = GamePiece.GamePiece(1.0, 'point', position=(2., 2., 0.)) poly1 = GamePiece.GamePiece(1.0, 'polygon', xy=[(0., 0.), (2., 0.), (2., 2.), (0., 2.)]) self.assertTrue(pt1.collides(poly1)) self.assertTrue(poly1.collides(pt1)) self.assertFalse(pt2.collides(poly1)) self.assertFalse(poly1.collides(pt2))
def test_col_poly_poly(self): gb1 = GamePiece.GamePiece(1.0, 'polygon', xy=[(0., 0.), (2., 0.), (0., 1.)]) gb2 = GamePiece.GamePiece(1.0, 'polygon', xy=[(1., 0.), (4., 2.), (4., 0.)]) gb3 = GamePiece.GamePiece(1.0, 'polygon', xy=[(-1., -1.), (-4., -2.), (-4., -0.)]) self.assertTrue(gb1.collides(gb2)) self.assertFalse(gb1.collides(gb3))
def test_init_(self): projectile = GamePiece.Projectile(size=1., position=(2., 3., 4.), velocity=(4.2, 5.3)) self.assertAlmostEqual( projectile.size + projectile.position[1] + projectile.velocity[1], 1. + 3. + 5.3)
def test_projectile_out_of_bounds(self): projectile = GamePiece.Projectile(size=4., position=(30., -10, 2.), velocity=(0., 0.)) self.gameboard._add_projectile(projectile) self.gameboard._projectiles_out_of_bounds() self.assertNotIn(projectile, self.gameboard._projectiles) self.assertNotIn(projectile, self.gameboard.moving_objects)
def test_side_normal_vector(self): xy = (0., 0.), (0., 1.), (1., 1.) con_pol = GamePiece.ConvexPolygon(xy) self.assertAlmostEqual( np.abs(con_pol.side_normal[0][0] + con_pol.side_normal[0][1]) + np.abs(con_pol.side_normal[1][0] + con_pol.side_normal[1][1]) + np.abs(con_pol.side_normal[2][0] + con_pol.side_normal[2][1]), 1. + 0. + 1.)
def test_pt_inside(self): poly1 = GamePiece.ConvexPolygon([(0., 0.), (1., 1.), (0., 1.)]) # Only points within the polygon's area return True. # Points outside the polygon's area as well as points on the edge # return False. self.assertTrue(poly1.point_inside((0.05, 0.2))) self.assertFalse(poly1.point_inside((2., 2.))) self.assertFalse(poly1.point_inside((1., 1.)))
def test_init_(self): asteroid = GamePiece.Asteroid1(size=3.6, position=(0., 0., 0.), angular_velocity=10., start_velocity=(1.2, 2.1)) self.assertAlmostEqual( asteroid.size + asteroid.velocity[0] + asteroid.position[0] + asteroid._angular_velocity, 3.6 + 1.2 + 0. + 10.)
def ship_fire(self): gunpos = self._ship.gunposition velo = ( DEFAULT_PROJECTILE_VELO * np.cos(gunpos[2]), DEFAULT_PROJECTILE_VELO * np.sin(gunpos[2])) projectile = GamePiece.Projectile(size=DEFAULT_PROJECTILE_SIZE, position=gunpos, velocity=velo) self._projectiles.append(projectile) self.moving_objects.append(projectile)
def test_side_vectors(self): xy = (0., 0.), (1., 2.), (-2., 1.) side = [] len_side = [] unity_side = [] for i in range(-1, len(xy) - 1): side.append((xy[i + 1][0] - xy[i][0], xy[i + 1][1] - xy[i][1])) len_side.append(np.sqrt(side[-1][0]**2. + side[-1][1]**2.)) unity_side.append( (side[-1][0] / len_side[-1], side[-1][1] / len_side[-1])) unity_side = unity_side[1:] + unity_side[:1] con_pol = GamePiece.ConvexPolygon(xy) self.assertAlmostEqual( unity_side[0][0] + unity_side[1][0] + unity_side[2][0] + unity_side[0][1] + unity_side[1][1] + unity_side[2][1], con_pol.side[0][0] + con_pol.side[1][0] + con_pol.side[2][0] + con_pol.side[0][1] + con_pol.side[1][1] + con_pol.side[2][1])
def test_init(self): GamePiece.Point((10., 2.))
def test_init(self): with self.assertRaises(TypeError): GamePiece.AsteroidBase(position=(0., 0., 0.), size=1.0, angular_velocity=0., start_velocity=(0., 0.))
def test_gun_position(self): ship = GamePiece.Ship(size=10., position=(0., 0., 0.)) self.assertAlmostEqual(ship.gunposition[0], 2. * 10. / 3.) self.assertAlmostEqual(ship.gunposition[1], 0.) self.assertAlmostEqual(ship.gunposition[2], 0.)
def test_init(self): ship = GamePiece.Ship(size=2.4) self.assertAlmostEqual(ship.size, 2.4)
def test_initialize(self): pos = (1., 2., 0.) gamePiece = GamePiece.PhysicsEngine(position=pos) self.assertEqual(gamePiece.position, pos)
def test_projection(self): xy = [(0., 0.), (1., 1.), (-1., 1.)] poly = GamePiece.ConvexPolygon(xy) proj = poly.projection((1. / np.sqrt(2.), 1. / np.sqrt(2.))) self.assertAlmostEqual(proj[0] + proj[1] + proj[2], np.sqrt(2.))
def test_col_pt_pt(self): pt1 = GamePiece.GamePiece(1.0, 'point', position=(1., 0., 0.)) pt2 = GamePiece.GamePiece(1.0, 'point', position=(1., 0., 0.)) self.assertFalse(pt1.collides(pt2))
def test_init_point(self): gb = GamePiece.GamePiece(1.0, 'point', position=(1., 1., 0.)) self.assertIsInstance(gb._gb_repr, GamePiece.Point)
def test_init_polygon_xy_missing(self): with self.assertRaises(TypeError): GamePiece.GamePiece(1.0, 'polygon')
def test_nocollision(self): poly1 = GamePiece.ConvexPolygon([(0., 0.), (1., 1.), (0., 1.)]) poly2 = GamePiece.ConvexPolygon([(1., 0.), (1., 1.), (2., 1.)]) self.assertFalse(poly1.collides(poly2))
def test_rotate(self): poly = GamePiece.ConvexPolygon([(0., 0.), (0., 1.), (1., 0.)]) poly.rotate(np.pi / 2.) self.assertAlmostEqual(poly.xy[1][0], -1.)
def test_initialize_wrong_position_parameters(self): with self.assertRaises(ValueError): GamePiece.PhysicsEngine((1., 2.))
def test_increase_velo(self): gamePiece = GamePiece.PhysicsEngine((0., 0., 0.), acceleration=10.) gamePiece.thrust = 1 gamePiece.step(0.1) self.assertAlmostEqual(gamePiece.velocity, (1., 0.))
def test_init_wrong_type(self): with self.assertRaises(TypeError): GamePiece.GamePiece(1.0, 10)