Mercurial > mamba
view mamba/snake.py @ 157:31881bf8ddda
Lockable text button
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Tue, 13 Sep 2011 22:42:44 +0200 |
parents | 9c47bf162ea1 |
children | e0573297b17c |
line wrap: on
line source
"""The player snake object.""" from pygame.sprite import Group from pygame.locals import BLEND_MULT #from pygame.draw import lines from pygame.draw import rect from mamba.sprites import BaseSprite, tile_sizify from mamba import mutators class Snake(object): UP, DOWN, LEFT, RIGHT = [(0, -1), (0, 1), (-1, 0), (1, 0)] SPEED = 60.0 # pixels / s 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) self.pos = tile_sizify(tile_pos) self.length = 80 self.xs = self.snake_length(self.length) self.ys = self.snake_length(self.length) head = property(fget=lambda self: self.segments[0]) tail = property(fget=lambda self: self.segments[-1]) def snake_length(self, size=10): result = [] for i in range(size): result.append(0) return result 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) (x, y), l = self.pos, self.length #lines(surface, (0, 0, 255), False, [(x, y), (x + l, y)], 20) for i in range(0, len(self.xs)): rect(surface, (0, 0, 255), [(self.xs[i], self.ys[i]), (20, 20)]) def update(self, dt): x, y = self.pos ox, oy = self.orientation ds = self.SPEED * dt self.pos = (x + ox * ds, y + oy * ds) for i in reversed(xrange(1, len(self.xs))): self.xs[i] = self.xs[i - 1] self.ys[i] = self.ys[i - 1] self.xs[0] = self.pos[0] self.ys[0] = self.pos[1] def set_orientation(self, orientation): self.orientation = orientation #self.orientation = orientation #for segment in self.segments: # segment.set_orientation(self) class Segment(BaseSprite): GREEN = mutators.Overlay("tiles/common/snake/green.png", BLEND_MULT) BLUE = mutators.BLUE RED = mutators.RED YELLOW = mutators.YELLOW _detail_mutators = () def __init__(self, image_name, tile_pos): super(Segment, self).__init__() self.set_base_image(image_name) self._colour_overlay = self.GREEN self._orientation = Snake.UP self.make_images() self.update_image() self.set_tile_pos(tile_pos) def set_base_image(self, image_name): self._base_image = "/".join(["snake", image_name]) def make_images(self): self._images = {} for orientation, muts in [ (Snake.RIGHT, (mutators.RIGHT,)), (Snake.LEFT, (mutators.LEFT,)), (Snake.UP, (mutators.UP,)), (Snake.DOWN, (mutators.DOWN,)), ]: all_muts = (self._colour_overlay,) + self._detail_mutators + muts self._images[orientation] = self.load_image(self._base_image, all_muts) def update_image(self): self.image = self._images[self._orientation] def set_orientation(self, orientation): self._orientation = orientation self.update_image() def set_colour(self, colour_overlay): self._colour_overlay = colour_overlay self.make_images() self.update_image() class Head(Segment): CLOSED = "snake-head" OPEN = "snake-head-mouth-open-r" EYE = mutators.Overlay("tiles/common/snake/snake-head-eye-r.png") TONGUE = mutators.Overlay("tiles/common/snake/snake-head-tongue-r.png") def __init__(self, tile_pos): self._detail_mutators = (self.EYE,) super(Head, self).__init__(self.CLOSED, tile_pos) def mouth_open(self): self.set_base_image(self.OPEN) self.make_images() self.update_image() def mouth_closed(self): self.set_base_image(self.CLOSED) self.make_images() self.update_image() def tongue_out(self): self._detail_mutators = (self.EYE, self.TONGUE) self.make_images() self.update_image() def tongue_in(self): self._detail_mutators = (self.EYE,) self.make_images() self.update_image() class Body(Segment): def __init__(self, tile_pos): super(Body, self).__init__("snake-body", tile_pos) class Tail(Segment): def __init__(self, tile_pos): super(Tail, self).__init__("snake-tail", tile_pos)