Commits

Szymon Wróblewski  committed 4525fc3

DirtyRect update method animates color and mask_rect now
Block have independent move
fixed error in layout.py code

  • Participants
  • Parent commits 0cd8ea5

Comments (0)

Files changed (5)

File alchemymadness/blocks.py

 
 class Block(MaskedSprite):
     """ A single block.
+
+    color: block color (r, g, b)
+    parent: Blocks instance
     """
     rect = layout.Main.block_rect
+    speed = 1.0
 
-    def __init__(self, color, *groups):
+    def __init__(self, color, parent, *groups):
         super(Block, self).__init__(color, *groups)
+        self.parent = parent
         self.t_image = resources.block
+        self.position = [0, 0] # topleft corner of grid
+        self._top = float(self.rect.top)
 
-    def grid_pos(self, pos, grid_rect):
-        """ set the rect for the given (x,y) grid pos
+    def move(self, d):
+        """Move block horizontally in given direction
+
+        d: positive - move right, negative - move left
         """
-        ox, oy, w, h = self.rect
+        self.rect.centerx += self.parent.grid.grid_size.w * d
+        self.dirty = 1
 
-        gx, gy = pos
-        x, y = gx * w, gy * h
-        x += grid_rect[0]
-        y += grid_rect[1]
-        self.rect = pygame.Rect(x, y, w, h)
-        #print self.rect
-        #self.changed = True
+    @property
+    def position(self):
+        b_size = self.parent.grid.block_size
+        g_pos = self.parent.grid.rect.topleft
+        return ((self.rect.left - g_pos[0]) / b_size[0],
+                (self.rect.top - g_pos[1]) / b_size[1])
+
+    @position.setter
+    def position(self, value):
+        b_size = self.parent.grid.block_size
+        g_pos = self.parent.grid.rect.topleft
+        self.rect.left = value[0] * b_size[0] + g_pos[0]
+        self.rect.top = value[1] * b_size[1] + g_pos[1]
+        self.top = float(self.rect.top)
+        self.dirty = 1
+
+    def update(self, time):
+        self.top += self.speed * self.rect.h * time / 100.
+        self.rect.top = self.top
+        pos = self.position
+        g_h = self.parent.grid.grid_size.h
+        print pos
+        if (pos[0], pos[1]+1) in self.parent.grid.grid or pos[1] + 1 >= g_h:
+            print pos
+            self.parent.grid.grid[pos] = self
+            self.parent.freze() # TODO: freze() in Blocks
+            self.parent.sprite_group.remove(self)
         self.dirty = 1
 
 
     """
     rect = layout.Main.small_block_rect
 
-    def __init__(self, color, *groups):
-        super(SmallBlock, self).__init__(color, *groups)
+    def __init__(self, color, parent, *groups):
+        super(SmallBlock, self).__init__(color, parent, *groups)
         self.t_image = resources.sblock
 
 
     """
     av_colors = [(255,0,0), (0,255,0), (0,0,255)] # available colors
 
-    def __init__(self, parent):
-        """ parent - a Grid instance.
+    def __init__(self, grid):
+        """ grid - a Grid instance.
         """
         super(Blocks, self).__init__()
         self.sprite_group = pygame.sprite.Group()
-        self.parent = parent
-        BlockC = Block if not self.parent.small else SmallBlock
-        self.sprite_group.add([BlockC(random.choice(self.av_colors)) for i in range(3)])
+        self.grid = grid
+        BlockC = Block if not self.grid.small else SmallBlock
+        self.sprite_group.add([BlockC(random.choice(self.av_colors), self) for i in range(3)])
         # how do we handle location, and rotation? We say where the grid it is on.
         #     top, left is (0,0)
         self.grid_positions = [[0,0], [1,0], [2,0]]
-
-
         self.on_moved()
 
     def on_moved(self):
         i = 0
         for b, pos in izip(self.sprite_group, self.grid_positions):
             #print "on_moved {} {}".format(i, pos)
-            b.grid_pos(pos, self.parent.rect)
+            b.position = pos
             i += 1
 
         #print "-" * 20
 
-    def update(self, time):
-        pass
-
     def has_collided(self):
         """ checks if we have collided with some other blocks.
               See if the blocks of the same color are below.
         for b in self.grid_positions:
 
             b[0] += 1
-            if b[0] >= self.parent.grid_size.w:
+            if b[0] >= self.grid.grid_size.w:
                 doit = False
         if not doit:
             for b in self.grid_positions:
         doit = True
         for b in self.grid_positions:
             b[1] += 1
-            if b[1] >= self.parent.grid_size.h:
+            if b[1] >= self.grid.grid_size.h:
                 doit = False
         if not doit:
             for b in self.grid_positions:
         self.grid_num = grid_num
         self.small = grid_num != 0
         self.rect = layout.Main.grid_rects[grid_num]
-
+        self.block_size = (self.rect.w / self.grid_size.w, self.rect.h / self.grid_size.h)
         self.restart()
 
     @property

File alchemymadness/layout.py

     small_block_rect = Rect(0, 0, 12, 12)
     grid_size = Rect(0, 0, 10, 15) # size in blocks
     grid_rects = (
-        Rect(50, 50, block_rect.w * grid_size[0], block_rect.h * grid_size[1]),
-        Rect(330, 58, small_block_rect.w * grid_size[0], small_block_rect.h * grid_size[1]),
-        Rect(478, 58, small_block_rect.w * grid_size[0], small_block_rect.h * grid_size[1]),
-        Rect(626, 58, small_block_rect.w * grid_size[0], small_block_rect.h * grid_size[1]),
+        Rect(50, 50, block_rect.w * grid_size.w, block_rect.h * grid_size.h),
+        Rect(330, 58, small_block_rect.w * grid_size.w, small_block_rect.h * grid_size.h),
+        Rect(478, 58, small_block_rect.w * grid_size.w, small_block_rect.h * grid_size.h),
+        Rect(626, 58, small_block_rect.w * grid_size.w, small_block_rect.h * grid_size.h),
     )
     potion_rect = Rect(372, 360, 130, 194)
     small_potion_rect = Rect(553, 454, 68, 100)

File alchemymadness/main.py

     # Initialise level
     pot = potion.Potion()
     pot.set_formula(3, 5, 3)
-    # TODO: change something so potion dont start filled with white fluid
     sprites = pygame.sprite.LayeredDirty(pot)
     sprites.clear(screen, resources.background)
 

File alchemymadness/potion.py

           col: tuple containing rgb colour.
 
         """
-        self.chems += 1
-        n = ((self.chems-1) / float(self.chems))
-        self.mix_color(col, 1-n)
-#        self.target_col = (self.color[0] * n, self.color[1] * n, self.color[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.color[0]),
-#                         (self.target_col[1] - self.color[1]),
-#                         (self.target_col[2] - self.color[2]))
-        # Update position
-        n = self.chems / float(self.max_chems)
-        self.target_y = self.mask_rect.h*(1-n)
+        if self.chems < self.max_chems:
+            self.chems += 1
+            self.mix_color(col, 1/float(self.chems))
+            r = self.mask_rect.copy()
+            n = self.chems / float(self.max_chems)
+            r.y = self.mask_rect.h*(1-n)
+            self.move_mask(r)
 
     def get_score(self):
         """Returns a score based on accuracy of the formula."""
         pass
-
-    def update(self, time):
-        #super(Potion, self).update(time)
-        # Move rect smoothly
-        if self.target_y is not None:
-            self.dirty = 1
-            self.mask_rect.y -= 5*(time/1000.)
-            if self.mask_rect.y < self.target_y:
-                self.mask_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.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])
-                else:
-                    col[i] = max(col[i], self.target_col[i])
-
-            self.color = tuple(col)
-            if self.color == self.target_col:
-                self.target_col = None

File alchemymadness/sprite.py

     changed = False
     fill_flags = pygame.locals.BLEND_RGBA_MULT
     target_col = None
-    target_rect = None
+    target_mask_rect = None
 
     def __init__(self, color, *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.mask_rect = Rect((0, 0), self.rect.size)
         self.changed = True
         self.dirty = 1
 
         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
 
     @property
         """Mix a color with specified ratio
 
         col: tuple containing rgb colour
-        ratio: color mix ratio (1 - max new color)
+        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"""
+        """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.),