1. pygame
  2. Untitled project
  3. pygamegsoc12

Source

pygamegsoc12 / alchemymadness / potion.py

"""Mixing potion"""

from pygame.locals import Rect

class Potion():
    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
    start_y = 470
    target_y = None
    rect = Rect(450, 470, 170, 113)

    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.rect.y = self.start_y + self.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.start_y + self.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.rect.y -= 5*(time/1000.)
            if self.rect.y < self.target_y:
                self.rect.y = self.target_y
                self.target_y = None
        # Lerp colours
        if self.target_col is not None:
            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