Ignore:
Timestamp:
09/02/13 20:35:41 (8 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Phase:
public
Message:

Hook up protagnist animations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r140 r143  
    151151            self._render_shape(surface, pos, angle)
    152152
     153    def animate(self):
     154        # Used by time animatations to advance the clock
     155        pass
     156
    153157
    154158def image_pos(image, pos):
     
    184188        surface.blit(image, image_pos(image, pos))
    185189        super(FacingImageRenderer, self).render(surface)
     190
     191
     192class AnimatedFacingImageRenderer(FacingImageRenderer):
     193    def __init__(self, left_images, right_images):
     194        self._images = {
     195            'left': left_images,
     196            'right': right_images,
     197        }
     198        self._frame = 0
     199        self._moving = False
     200
     201    def get_image(self, angle):
     202        if abs(angle) < math.pi / 2:
     203            face = 'right'
     204        else:
     205            face = 'left'
     206        if self._frame >= len(self._images[face]):
     207            self._frame = 0
     208        return self._images[face][self._frame]
     209
     210    def render(self, surface):
     211        pos = self.game_object.get_render_position(surface)
     212        image = self.get_image(self.game_object.get_render_angle())
     213        surface.blit(image, image_pos(image, pos))
     214        super(FacingImageRenderer, self).render(surface)
     215
     216    def animate(self):
     217        if self._moving:
     218            self._frame += 1
     219        else:
     220            self._frame = 0
     221
     222    def start(self):
     223        self._moving = True
     224
     225    def stop(self):
     226        self._moving = False
     227
     228
     229class TimedAnimatedRenderer(ImageRenderer):
     230
     231    def __init__(self, images):
     232        self._images = images
     233        self._frame = 0
     234        self._image = None
     235
     236    def _get_image(self):
     237        if self._frame > len(self._imaages):
     238            self._frame = 0
     239        return self._images[self._frame]
     240
     241    def render(self, surface):
     242        self._image = self._get_image()
     243        super(TimedAnimatedRenderer, self).render(surface)
     244
     245    def animate(self):
     246        self._frame += 1
    186247
    187248
     
    254315    def render(self, surface):
    255316        return self.renderer.render(surface)
     317
     318    def animate(self):
     319        self.renderer.animate()
    256320
    257321
Note: See TracChangeset for help on using the changeset viewer.