pygamegsoc12 / alchemymadness /

"""Sprites module"""

import pygame
from pygame.locals import Rect
from pygame.sprite import DirtySprite

class MaskedSprite(DirtySprite):
    """ Sprite with color

    rect, mask_rect, t_image should be overriden
    changed should be set to True whenever mask_rect is modified
    rect = Rect(0,0,10,10)
    changed = False
    fill_flags = pygame.locals.BLEND_RGBA_MULT
    target_col = None
    target_rect = None

    def __init__(self, color, *groups):
            color - color of sprite - (r,g,b) tuple
        super(MaskedSprite, self).__init__(*groups)
        self._image = pygame.Surface(self.rect.size) # Sprite colored image
        self.t_image = pygame.Surface(self.rect.size) # Sprite image
        self._color = color  # Block color
        self.mask_rect = Rect((0,0), self.rect.size)
        self.changed = True
        self.dirty = 1

    def image(self):
        if self.changed:
            self._image = self.t_image.copy()
            self._image.fill(self._color, self.mask_rect.clip(self._image.get_rect()), self.fill_flags)
        return self._image

    def color(self):
        return self._color

    def color(self, value):
        self._color = value
        self.changed = True
        self.dirty = 1

    def mix_color(self, color, ratio=0.5):
        """Mix a color with specified ratio

        col: tuple containing rgb colour
        ratio: color mix ratio (1 - max new color)
        self.target_col = tuple(self.color[i] * (1 - ratio) + color[i] * ratio for i in range(len(self.color)))
        self.diff_col = tuple(self.target_col[i] - self.color[i] for i in range(len(self.color)))

    def update(self, time):
        """Update color and mask_rect"""
        if self.target_col is not None:
            self.changed = True
            diff = (self.diff_col[0] * (time/500.),
                    self.diff_col[1] * (time/500.),
                    self.diff_col[2] * (time/500.))
            col = [self.color[0] + diff[0], self.color[1] + diff[1],
                   self.color[2] + diff[2]]
            for i in range(3):
                if diff[i] > 0:
                    col[i] = min(col[i], self.target_col[i])
                    col[i] = max(col[i], self.target_col[i])

            self.color = tuple(col)
            if self.color == self.target_col:
                self.target_col = None
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.