Changeset 160:0c9b0449485e


Ignore:
Timestamp:
Sep 2, 2013, 10:50:11 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Consolidate image rendering stuff a bit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r159 r160  
    166166        self._image = image
    167167
    168     def render(self, surface):
     168    def get_image(self):
     169        return self._image
     170
     171    def rotate_image(self, image):
     172        angle = self.game_object.get_render_angle() * 180 / math.pi
     173        return pygame.transform.rotate(image, angle)
     174
     175    def render_image(self, surface, image):
     176        image = self.rotate_image(image)
    169177        pos = self.game_object.get_render_position(surface)
    170         angle = self.game_object.get_render_angle() * 180 / math.pi
    171         image = pygame.transform.rotate(self._image, angle)
    172178        surface.blit(image, image_pos(image, pos))
     179
     180    def render(self, surface):
     181        self.render_image(surface, self.get_image())
    173182        super(ImageRenderer, self).render(surface)
    174183
    175184
    176 class FacingImageRenderer(Renderer):
     185class FacingImageRenderer(ImageRenderer):
    177186    def __init__(self, left_image, right_image):
    178187        self._images = {
     
    188197            self._face = 'left'
    189198
    190     def get_image(self, angle):
     199    def rotate_image(self, image):
     200        # Facing images don't get rotated.
     201        return image
     202
     203    def get_facing_image(self):
     204        return self._images[self._face]
     205
     206    def get_image(self):
     207        angle = self.game_object.get_render_angle()
    191208        self._update_facing(angle)
    192         return self._images[self._face]
    193 
    194     def render(self, surface):
    195         pos = self.game_object.get_render_position(surface)
    196         image = self.get_image(self.game_object.get_render_angle())
    197         surface.blit(image, image_pos(image, pos))
    198         super(FacingImageRenderer, self).render(surface)
     209        return self.get_facing_image()
    199210
    200211
     
    209220        self._face = 'left'
    210221
    211     def get_image(self, angle):
    212         self._update_facing(angle)
     222    def get_facing_image(self):
    213223        if self._frame >= len(self._images[self._face]):
    214224            self._frame = 0
    215225        return self._images[self._face][self._frame]
    216 
    217     def render(self, surface):
    218         pos = self.game_object.get_render_position(surface)
    219         image = self.get_image(self.game_object.get_render_angle())
    220         surface.blit(image, image_pos(image, pos))
    221         super(FacingImageRenderer, self).render(surface)
    222226
    223227    def animate(self):
     
    241245        self._image = None
    242246
    243     def _get_image(self):
     247    def get_image(self):
    244248        if self._frame > len(self._imaages):
    245249            self._frame = 0
    246250        return self._images[self._frame]
    247 
    248     def render(self, surface):
    249         self._image = self._get_image()
    250         super(TimedAnimatedRenderer, self).render(surface)
    251251
    252252    def animate(self):
Note: See TracChangeset for help on using the changeset viewer.