view mamba/snake.py @ 145:20b2cedf0f1d

Snake movement now turn
author Gideon Visser <gideon@gideonvisser.com>
date Mon, 12 Sep 2011 23:53:03 -0700
parents 2c643ec8dc4e
children 9c47bf162ea1
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._base_image = "/".join(["snake", image_name])
        self._colour_overlay = self.GREEN
        self._orientation = Snake.UP

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

    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):
    def __init__(self, tile_pos):
        self._eye = mutators.Overlay("tiles/common/snake/snake-head-eye-r.png")
        self._tongue = mutators.Overlay(
                "tiles/common/snake/snake-head-tongue-r.png")
        self._detail_mutators = (self._eye,)
        super(Head, self).__init__("snake-head-mouth-open-r", tile_pos)

    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-r", tile_pos)


class Tail(Segment):
    def __init__(self, tile_pos):
        super(Tail, self).__init__("snake-tail-r", tile_pos)