Ignore:
Timestamp:
09/02/13 11:21:53 (8 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Phase:
public
Message:

More consistent debug rendering.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/game_object.py

    r93 r104  
    66
    77from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH
     8from nagslang.options import options
    89
    910
     
    7576
    7677class Renderer(object):
     78    def __init__(self, shape):
     79        self._shape = shape
     80
     81    def _render_shape(self, surface, pos, angle):
     82        # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
     83        color = getattr(
     84            self._shape, 'color', pygame.color.THECOLORS['lightblue'])
     85        # We only explicitly draw Circle and Poly shapes. Everything else we
     86        # forward to pymunk.
     87        if isinstance(self._shape, pymunk.Circle):
     88            centre = pymunk.pygame_util.to_pygame(
     89                self._shape.body.position, surface)
     90            radius = int(self._shape.radius)
     91            pygame.draw.circle(surface, color, centre, radius, 2)
     92        elif isinstance(self._shape, pymunk.Poly):
     93            # polygon bounding box
     94            points = [pymunk.pygame_util.to_pygame(p, surface)
     95                      for p in self._shape.get_vertices()]
     96            pygame.draw.lines(surface, color, True, points, 2)
     97        else:
     98            pymunk.pygame_util.draw(surface, self._shape)
     99
    77100    def render(self, surface, pos, angle):
    78         raise NotImplementedError()
     101        if options.debug:
     102            self._render_shape(surface, pos, angle)
    79103
    80104
     
    85109
    86110class ImageRenderer(Renderer):
    87     def __init__(self, image):
     111    def __init__(self, shape, image):
     112        super(ImageRenderer, self).__init__(shape)
    88113        self._image = image
    89114
    90115    def render(self, surface, pos, angle):
    91116        surface.blit(self._image, image_pos(self._image, pos))
     117        super(ImageRenderer, self).render(surface, pos, angle)
    92118
    93119
    94120class FacingImageRenderer(Renderer):
    95     def __init__(self, left_image, right_image):
     121    def __init__(self, shape, left_image, right_image):
     122        super(FacingImageRenderer, self).__init__(shape)
    96123        self._images = {
    97124            'left': left_image,
     
    107134        image = self.get_image(angle)
    108135        surface.blit(image, image_pos(image, pos))
     136        super(FacingImageRenderer, self).render(surface, pos, angle)
    109137
    110138
    111139class ShapeRenderer(Renderer):
    112     def __init__(self, shape):
    113         self._shape = shape
    114 
    115140    def render(self, surface, pos, angle):
    116         import pymunk.pygame_util
    117         pymunk.pygame_util.draw(surface, self._shape)
     141        self._render_shape(surface, pos, angle)
     142        super(ShapeRenderer, self).render(surface, pos, angle)
    118143
    119144
Note: See TracChangeset for help on using the changeset viewer.