view mamba/mutators.py @ 136:00ada2e29798

Somewhat better (but still hideous) image variant support.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 11 Sep 2011 23:56:59 +0200
parents 513037749086
children e0573297b17c
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

    def __repr__(self):
        return "<%s args=%r>" % (self.__class__.__name__, self._args)


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

# base mutators
R90 = Mutator(rotate, 90)
R180 = Mutator(rotate, 180)
R270 = Mutator(rotate, -90)

# sprites mutator aliases
RIGHT = NULL
DOWN = R90
LEFT = R180
UP = R270


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

    def __init__(self, filename, blend=0):
        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)