Exemplo n.º 1
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.º 2
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
        )