def test_shot(self): field = GameField() tank = Tank() field.try_place_unit(tank, 60, 60) tank.current_direction = Direction.Left tank.shot(field) self.assertEqual(len(field.units), 2) tank.current_direction = Direction.Down tank.shot(field) self.assertEqual(len(field.units), 2) tank.current_direction = Direction.Down tank.shot_await_tick_pointer = 0 tank.shot(field) self.assertEqual(len(field.units), 3) tank.current_direction = Direction.Right tank.shot_await_tick_pointer = 0 tank.shot(field) self.assertEqual(len(field.units), 4) tank.current_direction = Direction.Up tank.shot_await_tick_pointer = 0 tank.shot(field) self.assertEqual(len(field.units), 5) tank.current_direction = Direction.Down tank.shot_await_tick_pointer = 0 tank.shot(field) self.assertEqual(len(field.units), 6)
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 )
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, )
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_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, )
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, )
def test_move_step(self): field = GameField() tank = TankBot() tank.max_speed = 2 tank.set_velocity(Direction.Up) self.assertTrue(field.try_place_unit(tank, 0, 0)) tank.move_step(field) self.assertEqual(tank.collision, Rect(0, 0, tank.collision.w, tank.collision.h)) tank.set_velocity(Direction.Down) tank.move_step(field) self.assertNotEqual(tank.collision, Rect(0, 0, tank.collision.w, tank.collision.h))
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 )
def load_from_file(full_file_name: str) -> GameField: with open(full_file_name, 'r', encoding='utf-8') as file: text_representation = file.read().split('\n') if (text_representation[0][:len("# MAP_FILE HEAD")] != "# MAP_FILE HEAD" and text_representation[-1] != "# MAP_FILE TAIL << AND"): return None field = GameField() field.width = int( text_representation[0].split(" << ")[1].split(" ")[0] ) * LITTLE_WALL_LENGTH field.height = int( text_representation[0].split(" << ")[1].split(" ")[1] ) * LITTLE_WALL_LENGTH collision = text_representation[0].split(" << ")[2].split(" ") field.player = Tank() field.player.health_points = PLAYER_TANK_HEALTH_POINTS field.player.max_speed = PLAYER_TANK_SPEED field.player.shot_await_tick_count = PLAYER_TANK_COOL_DOWN field.try_place_unit( field.player, LITTLE_WALL_LENGTH * int(collision[0]), LITTLE_WALL_LENGTH * int(collision[1]) ) for line in text_representation[1:-1]: if line[1] == "#": continue line_arguments = line.split(" << ") if line_arguments[0] == "ADD UNIT": collision = line_arguments[2].split(" ") unit = get_unit_by(line_arguments[1]) unit.collision.set_width(int(collision[2]) * LITTLE_WALL_LENGTH) unit.collision.set_height(int(collision[3]) * LITTLE_WALL_LENGTH) field.try_place_unit( unit, int(collision[0]) * LITTLE_WALL_LENGTH, int(collision[1]) * LITTLE_WALL_LENGTH, True ) elif line_arguments[0] == "ADD SPAWNER": collision = line_arguments[1].split(" ") spawner = TankBotSpawner(tank_to_go=int(line_arguments[2])) for tank in line_arguments[3].split(" "): spawner.tanks_to_go.append(get_unit_by(tank)) field.spawners.append(spawner) field.try_place_unit( spawner, int(collision[0]) * LITTLE_WALL_LENGTH, int(collision[1]) * LITTLE_WALL_LENGTH, True ) elif line_arguments[0] == "ADD BONUS": collision = line_arguments[1].split(" ") bonus_box = BonusBox() bonus_box.next_bonuses = get_bonus_by(line_arguments[2]) field.try_place_unit( bonus_box, LITTLE_WALL_LENGTH * int(collision[0]), LITTLE_WALL_LENGTH * int(collision[1]), True ) field.units_for_step = field.units_for_step_buffer field.units_for_step_buffer = list() field.units_for_intersect = field.units_for_intersect_buffer field.units_for_intersect_buffer = list() return field
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 )
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)