pygamegsoc12 / alchemymadness / potion.py

"""Mixing potion"""

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

import resources
import layout

class Potion(DirtySprite):
    chems = 0  # Chem count
    formula = (0,0,0)  # Target formula
    col = (0,0,0)  # Current formula
    target_col = None
    max_chems = 0  # Target number of chems
    rect = layout.Main.potion_rect
    image_rect = Rect((0, rect.y), rect.size)
    target_y = None
    changed = False

    def __init__(self, *groups):
        """
        Init potion sprite.

        Args:
          groups to add sprite to

        """
        super(Potion, self).__init__(*groups)
        # Image with transparency
        self.t_image = resources.background.subsurface(self.rect)
        # Sprite image
        self._image = pygame.Surface(self.rect.size)
        self.changed = True

    @property
    def image(self):
        if self.changed:
            self._image.fill((0,0,0))
            self._image.fill(self.col, self.image_rect)
            self._image.blit(self.t_image, (0,0))
        return self._image


    def set_formula(self, r=0, g=0, b=0):
        """Set the target formula for the level."""
        self.chems = 0
        self.formula = (r, g, b)
        self.max_chems = sum(self.formula)
        # Offset the rect to be at bottom of potion
        self.image_rect.y = self.image_rect.h

    def add_color(self, col):
        """
        Add a colour from a player's board.

        Args:
          col: tuple containing rgb colour.

        """
        self.chems += 1
        n = ((self.chems-1) / float(self.chems))
        self.target_col = (self.col[0] * n, self.col[1] * n, self.col[2] * n)
        self.target_col = (self.target_col[0] + col[0]/float(self.chems),
                           self.target_col[1] + col[1]/float(self.chems),
                           self.target_col[2] + col[2]/float(self.chems))
        self.diff_col = ((self.target_col[0] - self.col[0]),
                         (self.target_col[1] - self.col[1]),
                         (self.target_col[2] - self.col[2]))

        # Update position
        n = self.chems / float(self.max_chems)
        self.target_y = self.image_rect.h*(1-n)

    def get_score(self):
        """Returns a score based on accuracy of the formula."""
        pass

    def update(self, time):
        # Move rect smoothly
        if self.target_y is not None:
            self.dirty = 1
            self.image_rect.y -= 5*(time/1000.)
            if self.image_rect.y < self.target_y:
                self.image_rect.y = self.target_y
                self.target_y = None
        # Lerp colours
        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.col[0] + diff[0], self.col[1] + diff[1],
                   self.col[2] + diff[2]]
            for i in range(3):
                if diff[i] > 0:
                    col[i] = min(col[i], self.target_col[i])
                else:
                    col[i] = max(col[i], self.target_col[i])

            self.col = tuple(col)
            if self.col == 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 ProjectModifiedEvent.java.
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.