Mercurial > mamba
view mamba/mutators.py @ 283:11cf3a259eaa
Iterate over copies, to avoid some issues with children changing in the loop
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 15 Sep 2011 21:44:26 +0200 |
parents | 0dfa3f52742b |
children |
line wrap: on
line source
"""Mutations to apply to images when they're loaded.""" from pygame.transform import rotate from pygame.locals import BLEND_RGBA_MULT, SRCALPHA from pygame.surface import Surface from mamba.data import load_tile_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 UP = R90 LEFT = R180 DOWN = R270 # overlays class Overlay(Mutator): """Overlay another image on top of the given one.""" def __init__(self, image_name, tileset="common", blend=0): super(Overlay, self).__init__(self.overlay, image_name, tileset, blend) def overlay(self, image, image_name, tileset, blend): image = image.copy() overlay = load_tile_image(image_name, tileset) image.blit(overlay, (0, 0), None, blend) return image # colour overlays class Colour(Mutator): """Overlay an image with a colour.""" def __init__(self, colour, blend=BLEND_RGBA_MULT): super(Colour, self).__init__(self.colour, colour, blend) def colour(self, image, colour, blend): image = image.copy() overlay = Surface(image.get_size(), SRCALPHA, image) overlay.fill(colour) image.blit(overlay, (0, 0), None, blend) return image # colours RED = Colour((0xff, 0, 0)) BLUE = Colour((0, 0, 255)) YELLOW = Colour((0xff, 0xff, 0)) SNAKE_GREEN = Colour((0x7c, 0xff, 0))