Changeset 104:1be3eebb87c4
- Timestamp:
- 09/02/13 11:21:53 (7 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- nagslang
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
nagslang/game_object.py
r93 r104 6 6 7 7 from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH 8 from nagslang.options import options 8 9 9 10 … … 75 76 76 77 class 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 77 100 def render(self, surface, pos, angle): 78 raise NotImplementedError() 101 if options.debug: 102 self._render_shape(surface, pos, angle) 79 103 80 104 … … 85 109 86 110 class ImageRenderer(Renderer): 87 def __init__(self, image): 111 def __init__(self, shape, image): 112 super(ImageRenderer, self).__init__(shape) 88 113 self._image = image 89 114 90 115 def render(self, surface, pos, angle): 91 116 surface.blit(self._image, image_pos(self._image, pos)) 117 super(ImageRenderer, self).render(surface, pos, angle) 92 118 93 119 94 120 class 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) 96 123 self._images = { 97 124 'left': left_image, … … 107 134 image = self.get_image(angle) 108 135 surface.blit(image, image_pos(image, pos)) 136 super(FacingImageRenderer, self).render(surface, pos, angle) 109 137 110 138 111 139 class ShapeRenderer(Renderer): 112 def __init__(self, shape):113 self._shape = shape114 115 140 def render(self, surface, pos, angle): 116 import pymunk.pygame_util117 pymunk.pygame_util.draw(surface, self._shape)141 self._render_shape(surface, pos, angle) 142 super(ShapeRenderer, self).render(surface, pos, angle) 118 143 119 144 -
nagslang/protagonist.py
r101 r104 1 1 import pymunk 2 2 import pymunk.pygame_util 3 4 import pygame5 3 6 4 from nagslang.constants import COLLISION_TYPE_PLAYER … … 8 6 GameObject, SingleShapePhysicser, FacingImageRenderer) 9 7 from nagslang.mutators import FLIP_H 10 from nagslang.options import options11 8 from nagslang.resources import resources 12 9 … … 56 53 self._renderers = { 57 54 self.HUMAN_FORM: FacingImageRenderer( 55 self._shapes[self.HUMAN_FORM], 58 56 self._get_image('human_1.png'), 59 57 self._get_image('human_1.png', FLIP_H)), 60 58 self.WOLF_FORM: FacingImageRenderer( 59 self._shapes[self.WOLF_FORM], 61 60 self._get_image('werewolf_1.png'), 62 61 self._get_image('werewolf_1.png', FLIP_H)), … … 134 133 return 135 134 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 a140 # lot less noisy141 color = pygame.color.THECOLORS['lightblue']142 if hasattr(self._shapes[self.form], 'get_vertices'):143 # polygon bounding box144 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 circle149 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.