view mamba/mutators.py @ 125:625b22f92efa

the snake is green
author Adrianna Pińska <adrianna.pinska@gmail.com>
date Sun, 11 Sep 2011 21:35:39 +0200
parents 9c43a23aa204
children 9bef49d6db86
line wrap: on
line source

"""Mutations to apply to images when they're loaded."""

from pygame.transform import rotate
from pygame.locals import BLEND_ADD, BLEND_MULT

from mamba.data import load_image


class Mutator(object):

    def __init__(self, func, *args):
        self._func = func
        self._args = args

    def __call__(self, image):
        return self._func(image, *self._args)

    def __hash__(self):
        return hash((id(self._func), self._args))

    def __eq__(self, other):
        return (self._func is other.func) and self._args == other._args


# mutator that does nothing
NULL = Mutator(lambda x: x)

# sprites mutators
RIGHT = NULL
DOWN = Mutator(rotate, 90)
LEFT = Mutator(rotate, 180)
UP = Mutator(rotate, -90)

# tile mutators
TL = NULL
BL = Mutator(rotate, -90)
TR = Mutator(rotate, 90)
BR = Mutator(rotate, 180)


# overlays
class Overlay(Mutator):
    """Overlay another image on top of the given one."""

    BLEND = BLEND_ADD

    def __init__(self, filename, blend=None):
        if blend is None:
            blend = self.BLEND
        super(Overlay, self).__init__(self.overlay, filename, blend)

    def overlay(self, image, filename, blend):
        image = image.copy()
        overlay = load_image(filename)
        image.blit(overlay, (0, 0), None, blend)
        return image


# colours
BLUE = Overlay("tiles/common/blue.png", BLEND_MULT)
RED = Overlay("tiles/common/red.png", BLEND_MULT)
YELLOW = Overlay("tiles/common/yellow.png", BLEND_MULT)