示例#1
0
    def remove_waypoint(self, row, column):
        """Remove a waypoint from the stack.

        This method removes the first occurrence of a waypoint in the stack.

        If the waypoint cannot be found, it raises a ValueError exception.
        If the row and column parameters are not int, an
        PglInvalidTypeException is raised.

        :param row: The "row" part of the waypoint's coordinate.
        :type row: int
        :param column: The "column" part of the waypoint's coordinate.
        :type row: int
        :raise PglInvalidTypeException: If any of the parameters is not an int.
        :raise ValueError: If the waypoint is not found in the stack.

        Example::

            method()
        """
        if type(row) is not int:
            raise base.PglInvalidTypeException('"row" is not an integer. It must be.')
        if type(column) is not int:
            raise base.PglInvalidTypeException(
                '"column" is not an integer. It must be.'
            )
        try:
            idx = self.waypoints.index((row, column))
            self.waypoints.pop(idx)
        except ValueError:
            raise base.PglException(
                "invalid_waypoint",
                f"Waypoint ({row},{column}) does not exist in the waypoints stack.",
            )
示例#2
0
    def add_waypoint(self, row, column):
        """Add a waypoint to the list of waypoints.

        Waypoints are used one after the other on a FIFO basis
        (First In, First Out).

        If not destination (i.e destination == (None, None)) have been set yet, that
        method sets it.

        :param row: The "row" part of the waypoint's coordinate.
        :type row: int
        :param column: The "column" part of the waypoint's coordinate.
        :type row: int
        :raise PglInvalidTypeException: If any of the parameters is not an int.

        Example::

            pf = PathFinder(game=mygame, actuated_object=npc1)
            pf.add_waypoint(3,5)
            pf.add_waypoint(12,15)

        """
        if type(row) is not int:
            raise base.PglInvalidTypeException('"row" is not an integer. It must be.')
        if type(column) is not int:
            raise base.PglInvalidTypeException(
                '"column" is not an integer. It must be.'
            )
        self.waypoints.append((row, column))
        if self.destination == (None, None):
            self.destination = (row, column)
示例#3
0
文件: core.py 项目: ry-v1/pygamelib
    def set_sprixel(self, row, column, val):
        """
        Set a specific sprixel in the sprite to the given value.
        :param name: some param
        :type name: str

        Example::

            method()
        """
        if type(row) is not int or type(column) is not int:
            raise base.PglInvalidTypeException(
                "Sprite.set_sprixel(row, column, val) row and column needs to be "
                "integer.")
        if row < 0 or row >= self.size[1] or column < 0 or column >= self.size[
                0]:
            raise base.PglException(
                "out_of_sprite_boundaries",
                f"Sprite.set_sprixel(): ({row},{column}) is out of bound.",
            )
        if not isinstance(val, Sprixel):
            raise base.PglInvalidTypeException(
                "Sprite.set_sprixel(row, column, val) val needs to be a Sprixel"
            )
        self._sprixels[row][column] = val
示例#4
0
    def next_frame(self):
        """Update the parent's model, sprixel or sprite with the next frame of the
        animation.

        That method takes care of automatically replaying the animation if the
        last frame is reached if the state is constants.RUNNING.

        If the the state is PAUSED it still update the parent.model
        and returning the current frame. It does NOT actually go to next frame.

        If parent is not a sub class of
        :class:`~pygamelib.board_items.BoardItem` an exception is raised.

        :raise: :class:`~pygamelib.base.PglInvalidTypeException`

        Example::

            item.animation.next_frame()

        .. WARNING:: If you use Sprites as frames, you need to make sure your Animation
            is attached to a :class:`~pygamelib.board_items.BoardComplexItem`.

        """
        if not isinstance(self.parent, board_items.BoardItem):
            raise base.PglInvalidTypeException(
                "The parent needs to be a sub class of BoardItem."
            )
        if self.state == constants.STOPPED:
            return
        elif self.state == constants.RUNNING:
            self._frame_index += 1
            if self._frame_index >= len(self.frames):
                if self.auto_replay:
                    self.reset()
                else:
                    self._frame_index = len(self.frames) - 1
        if type(self.frames[self._frame_index]) is str:
            self.parent.model = self.frames[self._frame_index]
        elif isinstance(self.frames[self._frame_index], Sprixel):
            self.parent.sprixel = self.frames[self._frame_index]
        elif isinstance(self.frames[self._frame_index], Sprite):
            self.parent.sprite = self.frames[self._frame_index]
            self.parent.update_sprite()
        else:
            raise base.PglInvalidTypeException(
                "Animation.next_frame(): the frame is neither a string, a sprixel nor a"
                " sprite."
            )
        return self.frames[self._frame_index]
示例#5
0
    def add_frame(self, frame):
        """Add a frame to the animation.

        The frame has to be a string (that includes sprites from the Sprite
        module and squares from the Utils module).

        Raise an exception if frame is not a string.

        :param frame: The frame to add to the animation.
        :type frame: str
        :raise: :class:`pygamelib.base.PglInvalidTypeException`

        Example::

            item.animation.add_frame(Sprite.ALIEN)
            item.animation.add_frame(Sprite.ALIEN_MONSTER)
        """
        if (
            type(frame) is str
            or isinstance(frame, Sprixel)
            or isinstance(frame, Sprite)
        ):
            self.frames.append(frame)
        else:
            raise base.PglInvalidTypeException(
                'The "frame" parameter must be a string, Sprixel or Sprite.'
            )
示例#6
0
 def model(self, value):
     if type(value) is str:
         self.__model = value
     else:
         raise base.PglInvalidTypeException(
             f"A Sprixel.model must be a string. {value} is not a string."
         )
示例#7
0
文件: core.py 项目: ry-v1/pygamelib
    def add(self, sprite):
        """
        Add a Sprite to the collection. This method is simply a shortcut to the usual
        dictionnary affectation. The collection requires the name of the Sprite to be
        the key. That method does that automatically.

        :param sprite: A Sprite object to add to the collection.
        :type sprite: :class:`Sprite`

        .. WARNING:: As SpriteCollection index Sprites by their name if you change the
           Sprite's name *after* adding it to the collection you will need to manually
           update the keys.

        Example::

            sprites_village1 = SpriteCollection.load_json_file('gfx/village1.spr')
            new_village = SpriteCollection()
            new_village.add( copy.deepcopy( sprites_village1.get('bakery') ) )
            print( new_village['bakery'] )
        """
        if isinstance(sprite, Sprite):
            self.data[sprite.name] = sprite
        else:
            raise base.PglInvalidTypeException(
                "SpriteCollection.add(sprite): require a Sprite object.")
示例#8
0
文件: core.py 项目: ry-v1/pygamelib
    def __init__(self,
                 model="",
                 bg_color="",
                 fg_color="",
                 is_bg_transparent=None):
        super().__init__()
        self.model = model
        self.bg_color = bg_color
        self.fg_color = fg_color
        if ((type(model) is not str
             and "FormattingString" not in str(type(model)))
                or (type(bg_color) is not str
                    and "FormattingString" not in str(type(bg_color)))
                or (type(fg_color) is not str
                    and "FormattingString" not in str(type(fg_color)))):
            raise base.PglInvalidTypeException(
                "Sprixel(model, bg_color, fg_color): all 3 variables needs to be a "
                "string or an empty string.")

        if (bg_color is None
                or bg_color == "") and (is_bg_transparent is None
                                        or is_bg_transparent == ""):
            self.is_bg_transparent = True
        else:
            self.is_bg_transparent = False
示例#9
0
文件: core.py 项目: ry-v1/pygamelib
    def remove_frame(self, index):
        """Remove a frame from the animation.

        That method remove the frame at the specified index and return it
        if it exists.

        If the index is out of bound an exception is raised.
        If the index is not an int an exception is raised.

        :param index: The index of the frame to remove.
        :type index: int
        :rtype: str
        :raise: IndexError, PglInvalidTypeException

        Example::

            item.animation.remove_frame( item.animation.search_frame(
                Sprite.ALIEN_MONSTER)
            )

        """
        if type(index) is not int:
            raise base.PglInvalidTypeException(
                'The "index" parameter must be an int.')
        if index <= self._frame_index and self._frame_index > 0:
            self._frame_index -= 1
        return self.frames.pop(index)
示例#10
0
文件: core.py 项目: ry-v1/pygamelib
 def fg_color(self, value):
     if type(value) is str or "FormattingString" in str(type(value)):
         self.__fg_color = value
     else:
         raise base.PglInvalidTypeException(
             f"A Sprixel.fg_color must be a string. {value} is not a string."
         )
示例#11
0
文件: core.py 项目: ry-v1/pygamelib
    def search_frame(self, frame):
        """Search a frame in the animation.

        That method is returning the index of the first occurrence of "frame".

        Raise an exception if frame is not a string.

        :param frame: The frame to find.
        :type frame: str
        :rtype: int
        :raise: :class:`~pygamelib.base.PglInvalidTypeException`

        Example::

            item.animation.remove_frame(
                item.animation.search_frame(Sprite.ALIEN_MONSTER)
            )

        """
        if (type(frame) is str or isinstance(frame, Sprixel)
                or isinstance(frame, Sprite)):
            return self.frames.index(frame)
        else:
            raise base.PglInvalidTypeException(
                'The "frame" parameter must be a string.')
示例#12
0
文件: core.py 项目: ry-v1/pygamelib
 def dtanimate(self, value):
     if type(value) is float or type(value) is int:
         self.__dtanimate = value
     else:
         raise base.PglInvalidTypeException(
             "Animation.dtanimate(value): value needs to be an int or float."
         )
示例#13
0
 def __init__(
     self,
     game=None,
     actuated_object=None,
     circle_waypoints=True,
     parent=None,
     algorithm=constants.ALGO_BFS,
 ):
     if actuated_object is not None and parent is None:
         self.actuated_object = actuated_object
         self.parent = actuated_object
     elif parent is not None:
         self.actuated_object = parent
         self.parent = parent
     super().__init__(self.parent)
     self.destination = (None, None)
     self.game = game
     self._current_path = []
     self.waypoints = []
     self._waypoint_index = 0
     self.circle_waypoints = circle_waypoints
     self.algorithm = algorithm
     if type(self.algorithm) is not int or (
         self.algorithm != constants.ALGO_BFS
         and self.algorithm != constants.ALGO_ASTAR
     ):
         raise base.PglInvalidTypeException(
             "In Actuator.PathFinder.__init__(..,algorithm) algorithm must be"
             "either ALGO_BFS or ALGO_ASTAR."
         )
示例#14
0
文件: core.py 项目: ry-v1/pygamelib
    def sprixel(self, row=0, column=None):
        """Return a sprixel at a specific position within the sprite.

        If the column is set to None, the whole row is returned.

        :param row: The row to access within the sprite.
        :type row: int
        :param column: The column to access within the sprite.
        :type column: int

        Example::

            # Return the entire line at row index 2
            scanline = house_sprite.sprixel(2)
            # Return the specific sprixel at sprite internal coordinate 2,3
            house_sprixel = house_sprite.sprixel(2, 3)

        .. WARNING:: For performance consideration sprixel() does not check the size of
           its matrix. This method is called many times during rendering and 2 calls to
           len() in a row are adding up pretty quickly.
           It checks the boundary of the sprite using the cached size. Make sure it is
           up to date!
        """
        if type(row) is not int:
            raise base.PglInvalidTypeException(
                "Sprite.sprixel(): Row is not an int.")
        if row < 0 or row >= self.size[1]:
            raise base.PglException(
                "out_of_sprite_boundaries",
                f"Sprite.sprixel(): Row ({row}) is out of the Sprite boundaries.",
            )
        if column is None:
            return self._sprixels[row]
        else:
            if type(column) is not int:
                raise base.PglInvalidTypeException(
                    "Sprite.sprixel(): Column is not an int.")
            if column < 0 or column >= self.size[0]:
                raise base.PglException(
                    "out_of_sprite_boundaries",
                    f"Sprite.sprixel(): Column ({column}) is out of the "
                    "Sprite boundaries.",
                )
            return self._sprixels[row][column]
示例#15
0
 def test_exceptions(self):
     e = pgl_base.PglException("error", "message")
     self.assertEqual(e.error, "error")
     self.assertEqual(e.message, "message")
     e = pgl_base.PglInvalidLevelException("message")
     self.assertEqual(e.message, "message")
     e = pgl_base.PglInvalidTypeException("message")
     self.assertEqual(e.message, "message")
     e = pgl_base.PglObjectIsNotMovableException("message")
     self.assertEqual(e.message, "message")
     e = pgl_base.PglInventoryException("error", "message")
     self.assertEqual(e.error, "error")
     self.assertEqual(e.message, "message")
示例#16
0
    def set_destination(self, row=0, column=0):
        """Set the targeted destination.

        :param row: "row" coordinate on the board grid
        :type row: int
        :param column: "column" coordinate on the board grid
        :type column: int
        :raises PglInvalidTypeException: if row or column are not int.

        Example::

            mykillernpc.actuator.set_destination(
                mygame.player.pos[0], mygame.player.pos[1]
            )
        """
        if type(row) is not int or type(column) is not int:
            raise base.PglInvalidTypeException(
                "In Actuator.PathFinder.set_destination(x,y) both x and y must be "
                "integer.")
        self.destination = (row, column)
示例#17
0
文件: core.py 项目: ry-v1/pygamelib
    def play_all(self):
        """Play the entire animation once.

        That method plays the entire animation only once, there is no auto
        replay as it blocks the game (for the moment).

        If the the state is PAUSED or STOPPED, the animation does not play and
        the method return False.

        If parent is not a sub class of
        :class:`~pygamelib.board_items.BoardItem` an exception is raised.

        If screen_refresh is not defined or is not a function an exception
        is raised.

        :raise: :class:`~pygamelib.base.PglInvalidTypeException`

        Example::

            item.animation.play_all()
        """
        if self.state == constants.PAUSED or self.state == constants.STOPPED:
            return False
        if self.refresh_screen is None or not callable(self.refresh_screen):
            raise base.PglInvalidTypeException(
                "The refresh_screen parameter needs to be a callback "
                "function reference.")
        if not isinstance(self.parent, board_items.BoardItem):
            raise base.PglInvalidTypeException(
                "The parent needs to be a sub class of BoardItem.")
        self.reset()
        previous_time = time.time()
        ctrl = 0
        while ctrl < len(self.frames):
            if self.dtanimate >= self.display_time:
                # Dirty but that's a current limitation: to restore stuff on the board's
                # overlapped matrix, we need to either move or replace an item after
                # updating the sprite. This is only for sprites that have null items but
                # we don't want to let any one slip.
                # Also: this is convoluted...
                if (isinstance(self.parent, board_items.BoardComplexItem)
                        and self.parent.parent is not None and
                    (isinstance(self.parent.parent, board_items.engine.Board)
                     or isinstance(self.parent.parent,
                                   board_items.engine.Game))):
                    b = None
                    if isinstance(self.parent.parent,
                                  board_items.engine.Board):
                        b = self.parent.parent
                    else:
                        b = self.parent.parent.current_board()
                    pos = self.parent.pos
                    # We have to think that someone could try to animate the player
                    # while not on the current board.
                    try:
                        b.remove_item(self.parent)
                    except Exception:
                        return
                    self.next_frame()
                    b.place_item(self.parent, pos[0], pos[1])
                else:
                    self.next_frame()
                self.refresh_screen()
                ctrl += 1
                self.dtanimate = 0
            self.dtanimate += time.time() - previous_time
            previous_time = time.time()
        return True