Exemplo n.º 1
0
def test_shot(before_map, after_map, is_same):
    level_before = LevelsRepository._parse_level(before_map)
    tank = level_before.tanks[0]

    for i in range(10):
        tank.shot(level_before)

    level_after = LevelsRepository._parse_level(after_map)
    assert is_same_levels(level_before, level_after) is is_same
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
def level():
    lines = [
        "...P",
        "...C",
        ".E..",
        "...I",
    ]
    return LevelsRepository._parse_level(lines)
Exemplo n.º 4
0
class Controller:
    def __init__(self):
        self.levels_repository = LevelsRepository()
        self.current_level = 0
        self.last_event = None

    def on_event(self, events: List, state: GameStates) \
            -> Tuple[GameStates, "Level"]:
        level = self.levels_repository[self.current_level]
        for event in events:
            self.last_event = event
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                return self.switch_pause(state), level
        if state == GameStates.START:
            self.current_level = 0
            return GameStates.START, level
        elif state == GameStates.GAME:
            new_state = self.play_game(level)
            if new_state is not None:
                return new_state
        elif state == GameStates.RELOAD_LEVEL:
            self.levels_repository.reload(self.current_level)
            return GameStates.GAME, level
        elif state == GameStates.SAVE_LEVEL:
            self.levels_repository.save_level()
            return GameStates.GAME, level
        elif state == GameStates.LOAD_SAVE:
            self.levels_repository.load_latest_save()
            return GameStates.GAME, level
        return state, level

    def switch_pause(self, state):
        if state == GameStates.GAME:
            return GameStates.PAUSE
        elif state == GameStates.PAUSE:
            return GameStates.GAME

    def play_game(self, level):
        for obj in level:
            obj.update(self.last_event, level)
        if level.player is None or level.command_center is None:
            logger.info("Player was loose")
            return GameStates.DIE, level
        elif len(level.tanks) == 0:
            logger.info("Player wins")
            self.current_level += 1
Exemplo n.º 5
0
def test_level_unserilize(level):
    level_rep = LevelsRepository()
    level_rep.latest_save_path = "tests/save.json"
    level_rep.load_latest_save()
    assert is_same_levels(level, level_rep.latest_level)
Exemplo n.º 6
0
def test_level_serialize(level):
    level_rep = LevelsRepository()
    level_rep.latest_level = level
    level_rep.save_level()
Exemplo n.º 7
0
 def __init__(self):
     self.levels_repository = LevelsRepository()
     self.current_level = 0
     self.last_event = None
Exemplo n.º 8
0
def levels_rep():
    return LevelsRepository()