def test_player_take_bonus(bonus): level = Level(100, 100) level.player = Player(Vector(50, 50)) health, speed, = level.player.health, level.player.speed level.bonuses = [bonus(Vector(50, 50))] level.bonuses[0].update(None, level) assert health != level.player.health or speed != level.player.speed
def test_level_repository_get_from_symbol(symbol, result): obj = LevelsRepository._get_from_symbol(symbol, Vector(0, 0)) if obj is None: assert obj is result elif isinstance(obj, list): assert obj[0] is result else: assert isinstance(obj, result)
class Directions(Enum): UP = Vector(0, -1) RIGHT = Vector(1, 0) DOWN = Vector(0, 1) LEFT = Vector(-1, 0) def get_angle(self): return { self.UP: 0, self.RIGHT: -90, self.LEFT: 90, self.DOWN: 180, }.get(self) @staticmethod def random_direction(): return random.choice([ Directions.UP, Directions.RIGHT, Directions.DOWN, Directions.LEFT ])
def _parse_level(cls, lines: List[str]) -> "Level": objects = {name: [] for name in GameObject.registry.keys()} for y, line in enumerate(lines): for x, symbol in enumerate(line.strip()): position = Vector(x * CELL_WIDTH, y * CELL_HEIGHT) game_obj = cls._get_from_symbol(symbol, position) if game_obj is None: continue objects[game_obj.__class__.__name__].append(game_obj) return cls.create_level(x * CELL_WIDTH, y * CELL_HEIGHT, objects)
def load_latest_save(self): if self.latest_save_path is None: return logger.debug("Started unserialization") with open(self.latest_save_path) as json_file: serialize_obj = json.load(json_file) width = serialize_obj.pop("width") height = serialize_obj.pop("height") objects = {} for class_name, values in serialize_obj.items(): objects[class_name] = [] logger.debug(f"Started process group {class_name}") for json_obj in values: position = Vector(json_obj["x"], json_obj["y"]) direction = json_obj.get("direction") if direction: game_obj = GameObject.registry[class_name](position, direction) else: game_obj = GameObject.registry[class_name](position) objects[class_name].append(game_obj) self.latest_level = self.create_level(width, height, objects)
def test_missile_update_wall(level): level.walls = [Wall(Vector(10, 10))] health = level.walls[0].health level.missiles = [Missile(Vector(10, 10), Directions.DOWN)] level.missiles[0].update(None, level) assert health != level.walls[0]
def test_missile_update(level): missile = Missile(Vector(10, 10), Directions.DOWN) missile.update(None, level)
def test_missile_kill(level): missile = Missile(Vector(10, 10), Directions.DOWN) level.missiles.append(missile) missile.kill(level) assert len(level.missiles) == 0
import pytest from battle_city import Level from battle_city.game_objects import Missile, Directions from battle_city.game_objects.blocks import Wall from battle_city.utils import Vector @pytest.mark.parametrize("position, direction", [(Vector(0, 0), Directions.UP)]) def test_missile_init(position, direction): missile = Missile(position, direction) assert True def test_missile_kill(level): missile = Missile(Vector(10, 10), Directions.DOWN) level.missiles.append(missile) missile.kill(level) assert len(level.missiles) == 0 def test_missile_update(level): missile = Missile(Vector(10, 10), Directions.DOWN) missile.update(None, level) def test_missile_update_wall(level): level.walls = [Wall(Vector(10, 10))] health = level.walls[0].health level.missiles = [Missile(Vector(10, 10), Directions.DOWN)]
def test_player_on_event(event): level = Level(100, 100) level.player = Player(Vector(50, 50)) level.player.update(event, level) assert 0 != level.player.rect.x or 0 != level.player.rect.y
def test_player_init(): Player(Vector(0, 0)) assert True
import pytest from pytest_mock import mocker from battle_city import GameObject from battle_city.utils import Vector @pytest.mark.parametrize("position, result", [ (Vector(0, 1), True), (Vector(-100, -100), False) ]) def test_game_object_in_borders(level, position, result): assert GameObject.in_borders(position, level) is result
def test_vector_init(x, y, result): vector = Vector(x, y) assert vector == result assert vector.x == x assert vector.y == y
import pytest from battle_city.utils import Vector @pytest.mark.parametrize("x, y, result", [(10, 0, Vector(10, 0)), (0, 10.5, Vector(0, 10.5)), (-6, 10.5, Vector(-6, 10.5))]) def test_vector_init(x, y, result): vector = Vector(x, y) assert vector == result assert vector.x == x assert vector.y == y @pytest.mark.parametrize("first, second, result", [(Vector(0, 10), 20, Vector(20, 30)), (Vector(5.5, 2.5), 0.5, Vector(6, 3)), (Vector(5.5, 2.5), Vector(5.5, 2.5), Vector(11, 5))]) def test_vector_sum(first, second, result): assert first + second == result @pytest.mark.parametrize("first, second, result", [(Vector(0, 10), 20, Vector(0, 200)), (Vector(5.5, 2.5), 0.5, Vector(2.75, 1.25))]) def test_vector_mul(first, second, result): assert first * second == result