view mamba/mutators.py @ 85:5dd9d91aa94f

Overlay mutators (untested).
author Simon Cross <hodgestar@gmail.com>
date Sun, 11 Sep 2011 18:57:11 +0200
parents 80c97a6e53d2
children 9c43a23aa204
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, blend, filename)

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


class Multiply(Overlay):
    BLEND = BLEND_MULT