Exemplo n.º 1
0
class TestBreakableWall(TestCase):
    def test_get_render_info(self):
        wall = BreakableWall()
        wall.collision = Rect(0, 0, LITTLE_WALL_LENGTH * 5,
                              LITTLE_WALL_LENGTH * 7)
        info = wall.get_render_info()

        self.assertEqual(len(info), 5 * 7)
        self.assertEqual(info[0][0], UnitType.BrickWall)
        self.assertEqual(info[0][1].w, LITTLE_WALL_LENGTH)
        self.assertEqual(info[0][1].h, LITTLE_WALL_LENGTH)
        self.assertEqual(info[0][2], Direction.Up)

    def test_on_explosion(self):
        self.field = GameField()
        wall = BreakableWall()
        wall.collision = Rect(0, 0, LITTLE_WALL_LENGTH * 5,
                              LITTLE_WALL_LENGTH * 7)
        self.field_rectangle = Rect(0, 0, self.field.width, self.field.height)
        self.field.try_place_unit(wall, 0, 0)
        wall.on_explosion(self.field, Rect(0, 0, 20, 20))

        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)),
            5 * 7 - 4,
        )
Exemplo n.º 2
0
    def test_step(self):
        field = GameField()
        field_rectangle = Rect(0, 0, field.width, field.height)
        spawner = TankBotSpawner()
        field.try_place_unit(spawner, 1, 1)
        spawner.no_tank_tick_pointer = spawner.no_tank_tick_count

        spawner.step(field)
        self.assertEqual(len(field.get_intersected_units(field_rectangle)), 0)

        spawner.step(field)
        self.assertEqual(len(field.get_intersected_units(field_rectangle)), 0)

        spawner.no_tank_tick_pointer = spawner.no_tank_tick_count
        spawner.step(field)
        self.assertEqual(len(field.get_intersected_units(field_rectangle)), 0)

        spawner.no_tank_tick_pointer = 0
        field.try_remove_unit(spawner.current_tank)
        field.update()
        self.assertEqual(len(field.get_intersected_units(field_rectangle)), 0)

        spawner.no_tank_tick_pointer = spawner.no_tank_tick_count
        spawner.is_tank_alive = False
        spawner.step(field)
        spawner.no_tank_tick_pointer = spawner.no_tank_tick_count
        self.assertEqual(len(field.get_intersected_units(field_rectangle)), 0)

        spawner.step(field)
        self.assertEqual(len(field.get_intersected_units(field_rectangle)), 0)
Exemplo n.º 3
0
class TestBullet(TestCase):
    def test_move_step(self):
        self.field = GameField()
        wall = BreakableWall()
        wall.collision = Rect(
            0, 0, LITTLE_WALL_LENGTH * 5, LITTLE_WALL_LENGTH * 7
        )
        self.field_rectangle = Rect(0, 0, self.field.width, self.field.height)
        self.field.try_place_unit(wall, 0, 0)
        bullet = Bullet(Tank())
        bullet.current_direction = Direction.Left
        self.field.try_place_unit(bullet, LITTLE_WALL_LENGTH * 5 + 1, 1)

        bullet.move_step(self.field)

        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)),
            5 * 7 - 2,
        )
Exemplo n.º 4
0
class TestGameField(TestCase):
    def test_try_place_unit(self):
        self.field = GameField()
        self.field_rectangle = Rect(0, 0, self.field.width, self.field.height)

        self.assertTrue(self.field.try_place_unit(get_default_unit(), 1, 1))
        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)), 1
        )

        self.assertFalse(self.field.try_place_unit(get_default_unit(), 1, 1))
        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)), 1
        )

        self.assertTrue(self.field.try_place_unit(get_default_unit(), 4, 4))
        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)), 2
        )

        self.assertFalse(self.field.try_place_unit(get_default_unit(), -1, -1))
        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)), 2
        )

        self.assertFalse(
            self.field.try_place_unit(get_default_unit(), 2, 56465435)
        )
        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)), 2
        )

    def test_try_remove_unit(self):
        self.field = GameField()
        self.field_rectangle = Rect(0, 0, self.field.width, self.field.height)

        unit = get_default_unit()
        self.field.try_place_unit(unit, 1, 1)
        self.assertTrue(self.field.try_remove_unit(unit))
        self.field.update()
        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)), 0
        )

    def test_explode(self):
        self.field = GameField()
        self.field_rectangle = Rect(0, 0, self.field.width, self.field.height)
        self.field.try_place_unit(BreakableWall(16, 16), 1, 1)
        self.field.try_place_unit(BreakableWall(16, 16), 3, 1)
        self.field.try_place_unit(BreakableWall(16, 16), 6, 1)
        self.field.try_place_unit(BreakableWall(16, 16), 77, 1)
        self.field.try_place_unit(BreakableWall(16, 16), 300, 1)
        self.field.try_place_unit(BreakableWall(16, 16), 26, 1)

        self.field.explode(Bullet(Tank()), 200, Rect(0, 0, 6, 6))

        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)), 1
        )

        self.field = GameField()
        self.field.try_place_unit(BreakableWall(32, 32), 20, 20)
        self.field.explode(Bullet(Tank()), 10, Rect(20, 20, 6, 6))
        self.assertEqual(
            len(self.field.get_intersected_units(self.field_rectangle)), 3
        )