SHARE
TWEET

PyGame animated sprite class

kbmonkey Sep 15th, 2013 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Sprite(pygame.sprite.Sprite):
  2.     """
  3.    Represents an animated sprite.
  4.    """
  5.  
  6.     def __init__(self, name, rect, *groups):
  7.         """
  8.        rect(Rect) of the sprite on screen.
  9.        *groups(sprite.Group) add sprite to these groups.
  10.        """
  11.  
  12.         super(Sprite, self).__init__(*groups)
  13.         self.name = name
  14.         self.rect = rect
  15.         self.image = None
  16.         self._images = []
  17.         self._start = pygame.time.get_ticks()
  18.         self._delay = 0
  19.         self._last_update = 0
  20.         self._frame = 0
  21.         self._hasframes = False
  22.         self.fps = 1
  23.         self.loop = -1
  24.         self.shift_speed = 0
  25.         self.destination = None
  26.  
  27.     @property
  28.     def is_moving(self):
  29.         """
  30.        Test if this sprite is busy moving to a destination position.
  31.        """
  32.  
  33.         if self.shift_speed and self.destination:
  34.             return self.rect.topleft != self.destination
  35.  
  36.     def addimage(self, image, fps, loop):
  37.         """
  38.        Allows adding of a animated sprite image.
  39.        The fps applies to all frames. It overwrites the previous fps value.
  40.        """
  41.  
  42.         self._images.append(image)
  43.         self._hasframes = len(self._images) > 0
  44.         if len(self._images) > 0:
  45.             self.image = self._images[0]
  46.         if fps <= 0:
  47.             fps = 1
  48.         self._delay = 1000 / fps
  49.         self.loop = loop
  50.         self._frame = 0
  51.  
  52.     def clear(self):
  53.         """
  54.        Clear sprite images
  55.        """
  56.  
  57.         while len(self._images) > 0:
  58.             del self._images[-1]
  59.         self._hasframes = False
  60.  
  61.     def canupdate(self, t):
  62.         """
  63.        Tests if it is time to update again
  64.        time is the current game ticks. It is used to calculate when to update
  65.            so that animations appear constant across varying fps.
  66.        """
  67.  
  68.         if t - self._last_update > self._delay:
  69.             return True
  70.  
  71.     def update(self, t):
  72.         """
  73.        Update the sprite animation if enough time has passed.
  74.        Also update the position if it has a shift_speed and destination set.
  75.        t would be pygame.time.get_ticks() passed from the caller.
  76.        Call this each game tick, either manually if this sprite is stored in a list,
  77.        or if you keep sprites in a PyGame.Group object it will be called for you when you
  78.        issue the Group.draw() method.
  79.        """
  80.  
  81.         if self.shift_speed and self.destination:
  82.             if self.rect.left < self.destination[0]:
  83.                 self.rect.left += self.shift_speed
  84.             if self.rect.left > self.destination[0]:
  85.                 self.rect.left -= self.shift_speed
  86.             if self.rect.top < self.destination[1]:
  87.                 self.rect.top += self.shift_speed
  88.             if self.rect.top > self.destination[1]:
  89.                 self.rect.top -= self.shift_speed
  90.  
  91.         if self.canupdate(t):
  92.             self._last_update = t
  93.             if self._hasframes:
  94.                 self._frame += 1
  95.                 if self._frame >= len(self._images):
  96.                     self._frame = 0
  97.                     if self.loop > 0:
  98.                         self.loop -= 1
  99.                     if self.loop == 0:
  100.                         self._hasframes = False
  101.                         self._frame = -1
  102.                 self.image = self._images[self._frame]
  103.  
  104.     def set_position(self, x, y, shift_speed=0):
  105.         """
  106.        Set the sprite position.
  107.        shift_speed determines the amount of pixels to shift the sprite to
  108.        it's new location. 0 is an instant jump.
  109.        """
  110.  
  111.         if shift_speed == 0:
  112.             self.shift_speed = 0
  113.             self.rect.topleft = (x, y)
  114.         else:
  115.             self.shift_speed = shift_speed
  116.             self.destination = (x, y)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top