Example #1
0
    def make_move(self, move_direction, width, height):
        """
        Used to change coordinates of character
        """
        try:

            if not move_direction in set(['w', 'd', 's', 'a']):

                raise MoveError("Appeared while player has moved")

        except MoveError as move_error:

            my_logger.exception(move_error)
            return False

        old_x = self.player_x
        old_y = self.player_y

        if move_direction == 'w':

            if self.player_y > 0:

                self.player_y -=1
                my_logger.info('You have moved up')

        elif move_direction == 'd':

            if self.player_x < width - 1:

                self.player_x +=1
                my_logger.info('You have moved right')

        elif move_direction == 's':

            if self.player_y < height - 1:

                self.player_y +=1
                my_logger.info('You have moved down')

        elif move_direction == 'a':

            if self.player_x > 0:

                self.player_x -=1
                my_logger.info('You have moved left')

        if old_x == self.player_x and old_y == self.player_y:

            my_logger.info("There is a wall!")

        return True
    def get_cell_content(self, cell_x, cell_y):

        try:

            if cell_x >= self.map_size[0] or cell_y >= self.map_size[1]:

                raise MapCoordinatesError(
                    "Appeared while getting cell content", self.map_size,
                    [cell_x, cell_y])

        except MapCoordinatesError as map_exception:

            my_logger.exception(map_exception)
            return "-"

        return self.dungeon_map[cell_y][cell_x]
    def set_cell_content(self, cell_x, cell_y, new_value):

        try:

            if cell_x >= self.map_size[0] or cell_y >= self.map_size[1]:

                raise MapCoordinatesError(
                    "Appeared while setting cell content", self.map_size,
                    [cell_x, cell_y])

        except MapCoordinatesError as map_exception:

            my_logger.exception(map_exception)
            return False

        self.dungeon_map[cell_y][cell_x] = new_value
        return True
Example #4
0
    def setup_game(self):
        """
        Game startup method: user should choose between playing new game or loading save
        """
        my_logger.debug("Game start choice")
        start_game_choice = input(
            'Enter "y" if you want to play a new game or\n"load" if you want to load existing game:\n'
        )
        my_logger.debug("Game start choice handling")

        if start_game_choice == "load":

            try:

                self.load_game()

            except (FileNotFoundError, pickle.UnpicklingError) as file_error:

                my_logger.exception(file_error)

                if isinstance(file_error, FileNotFoundError):

                    my_logger.error(
                        "There is no dungeon.sav file! Start a new game instead!"
                    )

                elif isinstance(file_error, pickle.UnpicklingError):

                    my_logger.error(
                        "dungeon.sav file is corrupted! Start a new game instead!"
                    )

                self.setup_new_game()

        else:

            self.setup_new_game()

        self.player_map.update_map(self.dungeon_map, self.player.player_x,
                                   self.player.player_y)
        self.player_map.draw_map()
        self.is_game_ongoing = True

        self.enemy_last_position = [self.enemy.enemy_x, self.enemy.enemy_y]
        self.enemy_thread = threading.Thread(target=self.enemy_update)
        self.enemy_thread.start()
    def set_map_size(self, width, height):

        if isinstance(width, int) and isinstance(height, int):

            try:

                if width < 5:

                    raise MapSizeError("", width, height)
                    width = 5

                elif width > 119:

                    raise MapSizeError("", width, height)
                    width = 119

                elif height < 5:

                    raise MapSizeError("", width, height)
                    height = 5

                elif height > 30:

                    raise MapSizeError("", width, height)
                    height = 30

            except MapSizeError as map_size_error:

                my_logger.exception(map_size_error)
                my_logger.error("Map size will be resetted to default value!")
                self.reset_map_size()
                return False

            self.map_size = [width, height]
            my_logger.debug("Map size has been stored successfully")
            return True

        else:

            my_logger.error("Wrong size type! Please, check the input")
            return False
    def save_game(self):
        """
        Game save (pickle!)
        """
        data = [
            self.dungeon_map.dungeon_map, self.dungeon_map.map_size,
            self.player_map.dungeon_map, self.player.player_x,
            self.player.player_y, self.player.health, self.player.treasures
        ]

        try:

            out_file = open('dungeon.sav', 'wb')

        except PermissionError as premission_error:

            my_logger.exception(premission_error)
            my_logger.error(
                "Unable to save game! So sorry! But you can play again!")

        pickle.dump(data, out_file)
Example #7
0
    def setup_new_game(self):
        """
        This function is used to generate new map of size, specified by user
        """
        while True:

            map_request = input(
                'Enter preferable map size in format "width:height", or just press enter to play with default map:\n'
            )
            my_logger.info("Generating map...")

            if not len(map_request):

                map_size = [20, 10]
                break

            else:

                try:

                    map_size = [int(token) for token in map_request.split(':')]
                    break

                except ValueError as val_error:

                    my_logger.exception(val_error)
                    my_logger.error("Try to enter map size once again!")

        self.dungeon_map.set_map_size(*map_size)
        self.dungeon_map.generate_map()
        my_logger.info("Map has been generated successfully!")

        self.player_map.set_map_size(*map_size)
        self.player_map.init_map()

        self.player.reset()
        self.player.randomize_initial_position(self.dungeon_map)

        self.enemy.randomize_initial_position(self.dungeon_map)