def run_logic(self, world):
        while self._is_alive:
            time.sleep(self._moving_interval)

            neighbors = world.get_neighbor_cells(self.position)
            self.position = random.choice(neighbors)

            player = world.get_player()
            if self.position == player.position:
                player.hurt('ugly enemy')
                world.spawn_enemy(self)

                if player.is_dead():
                    logger.warning(
                        'Enemy has killed you. Provide input to finish the game.'
                    )
                    self.kill()
            else:
                logger.warning('Enemy is looking for you!')
Exemplo n.º 2
0
def game_loop():
    while True:
        user_input = input('Enable debug mode (y/n):')
        if user_input == 'y':
            settings.DEBUG_ENABLED = True
            break
        elif user_input == 'n':
            settings.DEBUG_ENABLED = False
            break
        else:
            print('Invalid input. Try again.\n')

    game_started = False
    while not game_started:
        user_input = input('New game(1)\nLoad game (2)\n')

        if user_input == '1':
            world.create_world(10)
            player_pos = world.spawn_player()
            game_started = True
            logger.warning('New game started')
        elif user_input == '2':
            try:
                player_pos = world.load()
                game_started = True
                logger.warning('Game loaded')
            except RuntimeError as error:
                logger.warning(error)

    won = False
    lose = False
    while not (won or lose):
        if world.is_trap_around(player_pos):
            logger.warning('There is a trap within one square from you!')
        else:
            logger.warning('No traps around.')
        if world.is_treasure_around(player_pos):
            logger.warning('There is a treasure within one square from you!')
        else:
            logger.warning('No treasures around.')

        input_is_valid = False
        while not input_is_valid:
            logger.warning(
                'Your move (w/a/s/d)? (Input \'save\' to save game)')
            user_input = input()

            input_is_valid = True
            if user_input in directions.keys():
                world.move_player(player_pos, directions[user_input])
            elif user_input == 'save':
                world.save(player_pos)
                logger.warning('Game saved.')
            else:
                logger.warning('Invalid input. Try again.')
                input_is_valid = False

        won = world.is_found_treasure(player_pos)
        lose = world.is_trapped(player_pos)

    if won:
        logger.warning('You won!')
    else:
        logger.warning('You lose!')

    world.print_world(player_pos)
def game_loop():
    while True:
        user_input = input('Enable debug mode (y/n):')
        if user_input == 'y':
            settings.DEBUG_ENABLED = True
            break
        elif user_input == 'n':
            settings.DEBUG_ENABLED = False
            break
        else:
            print('Invalid input. Try again.\n')

    world = None
    while world is None:
        user_input = input('New game(1)\nLoad game (2)\n')

        if user_input == '1':
            while world is None:
                user_input = input('Enter desired map size:')
                try:
                    world = World(size=int(user_input))
                    world.spawn_player(Player('urpoK'))
                    world.spawn_enemy(Enemy())
                    world.run_enemy()
                    logger.warning('New game started')
                except (InvalidMapSizeError, ValueError) as error:
                    logger.warning(error)
        elif user_input == '2':
            try:
                world = World.load()
                logger.warning('Game loaded')
            except NoSavedGamesError as error:
                logger.warning(error)

    won = False
    lose = False
    while not (won or lose):
        if world.is_trap_around():
            logger.warning('There is a trap within one square from you!')
        else:
            logger.warning('No traps around.')
        if world.is_treasure_around():
            logger.warning('There is a treasure within one square from you!')
        else:
            logger.warning('No treasures around.')

        input_is_valid = False
        while not input_is_valid:
            logger.warning(
                'Your move (w/a/s/d)? (Input \'save\' to save game)')
            user_input = input()

            input_is_valid = True
            if user_input == 'save':
                world.save()
                logger.warning('Game saved.')

            direction = None
            try:
                direction = directions[user_input]
            except KeyError:
                logger.warning(
                    'Invalid input (you must choose from \'w/a/s/d/save\' commands).'
                )
                input_is_valid = False

            if direction:
                try:
                    world.move_player(direction)
                except OutOfMapError as error:
                    logger.warning(error)

        world.update()

        won = world.is_found_treasure()
        lose = world.is_trapped()

    if won:
        logger.warning('You won!')
    else:
        logger.warning('You lose!')

    world.print()
 def pickup_treasure(self):
     self._bag += 1
     logger.warning('You picked up treasure. You got {} treasures'.format(
         self._bag))
 def hurt(self):
     if not self.is_dead():
         self._hp -= 1
         logger.warning('You got hurt by trap. You left with {} hp'.format(
             self._hp))
Exemplo n.º 6
0
 def hurt(self, offender):
     if not self.is_dead():
         self._hp -= 1
         logger.warning('You got hurt by {}. You left with {} hp'.format(
             offender, self._hp))