Changeset 104:1be3eebb87c4


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

More consistent debug rendering.

Location:
nagslang
Files:
2 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
  • nagslang/protagonist.py

    r101 r104  
    11import pymunk
    22import pymunk.pygame_util
    3 
    4 import pygame
    53
    64from nagslang.constants import COLLISION_TYPE_PLAYER
     
    86    GameObject, SingleShapePhysicser, FacingImageRenderer)
    97from nagslang.mutators import FLIP_H
    10 from nagslang.options import options
    118from nagslang.resources import resources
    129
     
    5653        self._renderers = {
    5754            self.HUMAN_FORM: FacingImageRenderer(
     55                self._shapes[self.HUMAN_FORM],
    5856                self._get_image('human_1.png'),
    5957                self._get_image('human_1.png', FLIP_H)),
    6058            self.WOLF_FORM: FacingImageRenderer(
     59                self._shapes[self.WOLF_FORM],
    6160                self._get_image('werewolf_1.png'),
    6261                self._get_image('werewolf_1.png', FLIP_H)),
     
    134133            return
    135134        self._body.apply_impulse((dx, dy))
    136 
    137     def render(self, surface):
    138         if options.debug:
    139             # Less general that pymunk.pygame_utils.draw, but also a
    140             # lot less noisy
    141             color = pygame.color.THECOLORS['lightblue']
    142             if hasattr(self._shapes[self.form], 'get_vertices'):
    143                 # polygon bounding box
    144                 points = [pymunk.pygame_util.to_pygame(p, surface)
    145                           for p in self._shapes[self.form].get_vertices()]
    146                 pygame.draw.lines(surface, color, True, points, 2)
    147             else:
    148                 # draw the circle
    149                 centre = pymunk.pygame_util.to_pygame(
    150                     self._shapes[self.form].body.position, surface)
    151                 radius = int(self._shapes[self.form].radius)
    152                 pygame.draw.circle(surface, color, centre, radius, 2)
    153         super(Protagonist, self).render(surface)
Note: See TracChangeset for help on using the changeset viewer.