view mamba/snake.py @ 129:c533b7c9cbe8

Fatter snake.
author Simon Cross <hodgestar@gmail.com>
date Sun, 11 Sep 2011 22:11:15 +0200
parents 646bb61c9ea6
children 513037749086
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 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 = 60.0

    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)
        (x, y), l = self.pos, self.length
        lines(surface, (0, 0, 255), False, [(x, y), (x + l, y)], 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)

    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

    def __init__(self, image_name, tile_pos):
        super(Segment, self).__init__()
        self._base_image = "/".join(["snake", image_name])
        self._orientation = Snake.UP

        self.make_images(self.GREEN)
        self.update_image()
        self.set_tile_pos(tile_pos)

    def make_images(self, colour_overlay):
        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(self._base_image, muts + (colour_overlay,))

    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.make_images(colour_overlay)
        self.update_image()


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)