Szymon Wróblewski avatar Szymon Wróblewski committed e061192

added Node base class for Block, Blocks, Grid,
every Block have independent frozen state, when it stops animating

Comments (0)

Files changed (2)

alchemymadness/blocks.py

 
 import resources
 import layout
-from sprite import MaskedSprite
+from sprite import MaskedSprite, Node
 
-class Block(MaskedSprite):
+class Block(Node, MaskedSprite):
     """ A single block.
 
     color: block color (r, g, b)
         self.rect = self.rect.copy()
         self.position = [0, 0] # topleft corner of grid
         self._top = float(self.rect.top)
+        self.frozen = False
 
     def move(self, d):
         """Move block horizontally in given direction
 
         d: positive - move right, negative - move left
         """
-        self.rect.centerx += self.parent.grid.grid_size.w * d
+        self.rect.centerx += self.parent.parent.grid_size.w * d
         self.dirty = 1
 
     @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])
+        grid = self.parent.parent
+        return ((self.rect.left - grid.rect.left) / grid.block_size[0],
+                (self.rect.top - grid.rect.top) / grid.block_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]
+        grid = self.parent.parent
+        self.rect.left = value[0] * grid.block_size[0] + grid.rect.left
+        self.rect.top = value[1] * grid.block_size[1] + grid.rect.top
         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
+        if not self.frozen:
+            self.top += self.speed * self.rect.h * time / 200.
+            self.rect.top = self.top
+            pos = self.position
+            g_h = self.parent.parent.grid_size.h
+            if (pos[0], pos[1]+1) in self.parent.parent.grid or pos[1] + 1 >= g_h:
+                self.position = pos
+                self.frozen = True
+                self.parent.parent.grid[pos] = self
+                self.parent.sprite_group.remove(self)
+            self.dirty = 1
 
 
 class SmallBlock(Block):
         self.t_image = resources.sblock
 
 
-class Blocks(object):
+class Blocks(Node):
     """ contains 3 Block() instances.
     """
     av_colors = [(255,0,0), (0,255,0), (0,0,255)] # available colors
 
-    def __init__(self, grid):
-        """ grid - a Grid instance.
+    def __init__(self, parent):
+        """ parent - a Grid instance.
         """
         super(Blocks, self).__init__()
         self.sprite_group = pygame.sprite.Group()
-        self.grid = grid
-        BlockC = Block if not self.grid.small else SmallBlock
+        self.parent = parent
+        BlockC = Block if not self.parent.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)
         for b in self.grid_positions:
 
             b[0] += 1
-            if b[0] >= self.grid.grid_size.w:
+            if b[0] >= self.parent.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.grid.grid_size.h:
+            if b[1] >= self.parent.grid_size.h:
                 doit = False
         if not doit:
             for b in self.grid_positions:
             b.mix_color(color, 1)
 
 
-class Grid(object):
+class Grid(Node):
     """ Contains Blocks()
     """
     grid_size = layout.Main.grid_size

alchemymadness/sprite.py

 """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)
+        self._parent = None
+        self._children = set()
+
+    @property
+    def parent(self):
+        return self._parent() if self._parent is not None else None
+
+    @parent.setter
+    def parent(self, node):
+        if node is not None:
+            self._parent = ref(node)
+            if isinstance(node, Node):
+                node._children.add(self)
+        else:
+            if isinstance(self._parent, Node):
+                node._children.discard(self)
+            self._parent = None
+
+    @property
+    def children(self):
+        return self._children
+
+
 class MaskedSprite(DirtySprite):
     """ Sprite with color
 
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.