pygamegsoc12 / alchemymadness /

"""Sprites module"""

from weakref import ref

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

class Node(object):
    def __init__(self, *args, **kwargs):
        super(Node, self).__init__(*args, **kwargs)
        print self, args
        self._parent = None
        self._children = set() # unordered !!

    def parent(self):
        return self._parent() if self._parent is not None else None

    def parent(self, node):
        if node is not None:
            self._parent = ref(node)
            if isinstance(node, Node):
            if isinstance(self._parent, Node):
            self._parent = None

    def children(self):
        return self._children

    def add(self, node):
        node.parent = self

    def discard(self, node):
        node.parent = None

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_mask_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)
            self.changed = False
        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 move_mask(self, rect):
        self.target_mask_rect = rect
        self.diff_mask = tuple(self.target_mask_rect[i] - self.mask_rect[i] for i in range(len(self.mask_rect)))
        #self.mask_anim_time = time

    def update(self, time):
        """Update color and mask_rect

        time: time from passsed since last call in ms
        if self.target_mask_rect is not None:
            self.dirty = 1
            self.changed = True
            mask_rect = self.mask_rect
            mask_diff = self.diff_mask
            for i in range(len(mask_rect)):
                mask_rect[i] += mask_diff[i] * time / 1000.
                mask_rect[i] = (min if mask_diff[i] > 0 else max)(mask_rect[i], self.target_mask_rect[i])
            if mask_rect == self.target_mask_rect:
                self.target_mask_rect = None
            self.mask_rect = mask_rect
        if self.target_col is not None:
            self.dirty = 1
            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.