collision
authorFrank DeMarco <frank.s.demarco@gmail.com>
Sat, 26 Apr 2014 10:06:54 +0000 (19:06 +0900)
committerFrank DeMarco <frank.s.demarco@gmail.com>
Sat, 26 Apr 2014 10:06:54 +0000 (19:06 +0900)
scale_sieve/ScaleSieve.py

index 85b60a8..b21aa4f 100644 (file)
@@ -1,6 +1,6 @@
 from random import randint, randrange, choice
 
-from pygame import Surface, PixelArray
+from pygame import Surface, PixelArray, Rect
 from pygame.draw import aalines, polygon
 from pygame.locals import *
 
@@ -96,25 +96,28 @@ class Sieve(Strip):
 
     def add_frames(self):
         bar_locations = []
+        self.bar_rects = bar_rects = []
         x = 0
+        sh = 30
         nodeset = self.get_game().interpolator.get_nodeset("scale")
         self.bar_w = bar_w = 3
         self.gaps = gaps = []
         while x < nodeset[-1].x:
             bar_locations.append(x)
+            bar_rects.append(Rect(x, 0, bar_w, sh))
             gaps.append(nodeset.get_y(x, natural=True))
             x += gaps[-1]
-        surface = Surface((x, 30))
+        surface = Surface((x, sh))
         transparent_color = (255, 0, 255)
         surface.fill(transparent_color)
         surface.set_colorkey(transparent_color)
         frames = surface, surface.copy()
         colors = (0, 255, 0), (153, 0, 204)
         for x in bar_locations:
+            bar_rects.append(Rect(x + surface.get_width(), 0, bar_w, sh))
             for ii, frame in enumerate(frames):
-                frame.fill(colors[ii], (x, 0, bar_w, surface.get_height()))
-                frame.fill(colors[ii - 1], (x + 1, 1, 1,
-                                            surface.get_height() - 2))
+                frame.fill(colors[ii], (x, 0, bar_w, sh))
+                frame.fill(colors[ii - 1], (x + 1, 1, 1, sh - 2))
         for frame in frames:
             self.add_frame(frame)
 
@@ -146,6 +149,8 @@ class Sieve(Strip):
                 location.bottom = acid.get_level()
         self.electric.location.centery = self.location.centery + 13
         self.electric.update()
+        for rect in self.bar_rects:
+            rect.centery = self.location.centery
         Strip.update(self)
 
 
@@ -196,6 +201,14 @@ class Triangles(GameChild, list):
     def update(self):
         self.populate()
         if self[0].location.collidelist(self.parent.sieve.locations) != -1:
+            sieve = self.parent.sieve
+            print sieve.bar_rects, self[0].collision_rects
+            for br in sieve.bar_rects:
+                for tr in self[0].collision_rects:
+                    if tr.move((self[0].location.left,
+                                0)).colliderect(br.move((sieve.location.left,
+                                                         0))):
+                        print "collision"
             self.remove(self[0])
         for triangle in self:
             triangle.update()
@@ -217,12 +230,14 @@ class Triangle(Sprite):
         x = 0
         height = surface.get_height()
         margin = 12
+        self.collision_rects = collision_rects = []
         for width in widths:
             x += sieve.bar_w
             points = (x + margin / 2, height - 2), \
                      (x + width - margin / 2 - 1, height - 2), \
                      (x + width / 2.0, 1)
             polygon(surface, (60, 255, 220), points)
+            collision_rects.append(Rect(points[0], (width - margin - 1, 1)))
             x += width - sieve.bar_w
         self.add_frame(surface)
         self.location.centerx = self.get_display_surface().get_rect().centerx
@@ -232,6 +247,8 @@ class Triangle(Sprite):
         ratio = 1 - (self.get_game().sieve.location.bottom - acid.ry[0]) / \
                 float(acid.ry[1] - acid.ry[0])
         self.move(dy=3 * ratio + .5)
+        for rect in self.collision_rects:
+            rect.bottom = self.location.bottom
         Sprite.update(self)