Mercurial > mamba
diff mamba/snake.py @ 110:e6299eb296ce
Draw the world!
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 11 Sep 2011 20:17:53 +0200 |
parents | 7ce2d2d8381a |
children | 0c1005c76c87 |
line wrap: on
line diff
--- a/mamba/snake.py Sun Sep 11 20:15:32 2011 +0200 +++ b/mamba/snake.py Sun Sep 11 20:17:53 2011 +0200 @@ -1,28 +1,72 @@ """The player snake object.""" +from pygame.sprite import Group + from mamba.sprites import BaseSprite from mamba import mutators -class Snake(BaseSprite): +class Snake(object): - UP, DOWN, LEFT, RIGHT = range(4) + UP, DOWN, LEFT, RIGHT = [(0, -1), (0, 1), (-1, 0), (1, 0)] def __init__(self, tile_pos, orientation): - super(Snake, self).__init__(image_name="snake/" - "snake-head-mouth-open-r") - self.load_images() - self.set_tile_pos(tile_pos) + self.segments = self.create_segments(tile_pos, orientation) + self.segment_group = Group() + self.segment_group.add(*self.segments) self.set_orientation(orientation) - def load_images(self): - pass + 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_sprites.draw(surface) def set_orientation(self, orientation): - self._orientation = orientation - print ["UP", "DOWN", "LEFT", "RIGHT"][orientation] - # TODO: update image + #self.orientation = orientation + #for segment in self.segments: + # segment.set_orientation(self) + print orientation -class Head(object): - pass +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.RIGHT) + 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)