Ignore:
Timestamp:
Sep 7, 2013, 6:59:28 AM (7 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Message:

Hack together tile animation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/render.py

    r397 r400  
    217217        self._alpha = alpha
    218218
    219     def _make_surface(self, surface):
     219    def _make_surface(self, surface, image):
     220        size = surface.get_size()
     221        mask = pygame.surface.Surface(size, pgl.SRCALPHA)
     222        pointlist = [pymunk.pygame_util.to_pygame(p, surface)
     223                     for p in self.outline]
     224        rect = pygame.draw.polygon(mask,
     225                                   pygame.color.Color(
     226                                       255, 255, 255, self._alpha),
     227                                   pointlist, 0)
     228        self._offset = (rect.x, rect.y)
     229        tiled = tile_surface((rect.w, rect.h), image)
     230        tiled.blit(mask, (0, 0), rect,
     231                   special_flags=pgl.BLEND_RGBA_MULT)
     232        return tiled
     233
     234    def render(self, surface):
    220235        if not self._tiled:
    221             size = surface.get_size()
    222             mask = pygame.surface.Surface(size, pgl.SRCALPHA)
    223             pointlist = [pymunk.pygame_util.to_pygame(p, surface)
    224                          for p in self.outline]
    225             rect = pygame.draw.polygon(mask,
    226                                        pygame.color.Color(
    227                                            255, 255, 255, self._alpha),
    228                                        pointlist, 0)
    229             self._offset = (rect.x, rect.y)
    230             self._tiled = tile_surface((rect.w, rect.h), self._tile_image)
    231             self._tiled.blit(mask, (0, 0), rect,
    232                              special_flags=pgl.BLEND_RGBA_MULT)
    233 
    234     def render(self, surface):
    235         self._make_surface(surface)
     236            self._tiled = self._make_surface(surface, self._tile_image)
    236237        surface.blit(self._tiled, self._offset)
    237238        super(TiledRenderer, self).render(surface)
     239
     240
     241class TimedTiledRenderer(TiledRenderer):
     242    """Animate tiles"""
     243
     244    # Should make this a mixin with TimeAnimate, but not right now
     245
     246    def __init__(self, outline, images, frame_ticks=1, alpha=255):
     247        self._images = images
     248        self._frame_ticks = frame_ticks
     249        self.outline = outline
     250        self._frames = [None] * len(images)
     251        self._offset = None
     252        self._alpha = alpha
     253        self._frame = 0
     254        self._frame_tick = 0
     255
     256    def advance_tick(self):
     257        self._frame_tick += 1
     258        if self._frame_tick > self._frame_ticks:
     259            self._frame_tick = 0
     260            self._frame += 1
     261        if self._frame >= len(self._images):
     262            self._frame = 0
     263
     264    def reset(self):
     265        self._frame_tick = 0
     266        self._frame = 0
     267
     268    def update(self, seconds):
     269        self.advance_tick()
     270
     271    def render(self, surface):
     272        if not self._frames[self._frame]:
     273            self._frames[self._frame] = self._make_surface(
     274                surface, self._images[self._frame])
     275        self._tiled = self._frames[self._frame]
     276        super(TimedTiledRenderer, self).render(surface)
Note: See TracChangeset for help on using the changeset viewer.