Mercurial > mamba
view mamba/snake.py @ 118:1b9b17eb896b
Change default snake orientation to UP for now.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 11 Sep 2011 20:32:18 +0200 |
parents | 0c1005c76c87 |
children | 119c0fb758c2 |
line wrap: on
line source
"""The player snake object.""" from pygame.sprite import Group from mamba.sprites import BaseSprite from mamba import mutators class Snake(object): UP, DOWN, LEFT, RIGHT = [(0, -1), (0, 1), (-1, 0), (1, 0)] def __init__(self, tile_pos, orientation): self.segments = self.create_segments(tile_pos, orientation) self.segment_group = Group() self.segment_group.add(*self.segments) self.set_orientation(orientation) head = property(fget=lambda self: self.segments[0]) tail = property(fget=lambda self: self.segments[-1]) def create_segments(self, tile_pos, orientation): x, y = tile_pos dx, dy = orientation return [Head((x, y)), Body((x + dx, y + dy)), Tail((x + 2 * dx, y + 2 * dy)), ] def draw(self, surface): self.segment_group.draw(surface) def set_orientation(self, orientation): #self.orientation = orientation #for segment in self.segments: # segment.set_orientation(self) print orientation class Segment(BaseSprite): def __init__(self, image_name, tile_pos): super(Segment, self).__init__() image_name = "/".join(["snake", image_name]) self._images = {} for orientation, muts in [ (Snake.RIGHT, (mutators.RIGHT,)), (Snake.LEFT, (mutators.LEFT,)), (Snake.UP, (mutators.UP,)), (Snake.DOWN, (mutators.DOWN,)), ]: self._images[orientation] = self.load_image(image_name, muts) self.set_orientation(Snake.UP) self.set_tile_pos(tile_pos) def set_orientation(self, orientation): self.image = self._images[orientation] class Head(Segment): def __init__(self, tile_pos): super(Head, self).__init__("snake-head-mouth-open-r", tile_pos) class Body(Segment): def __init__(self, tile_pos): super(Body, self).__init__("snake-body-r", tile_pos) class Tail(Segment): def __init__(self, tile_pos): super(Tail, self).__init__("snake-tail-r", tile_pos)