1. sirchristian
  2. Snakes

Commits

sirchristian  committed 9578419

Modified collision logic to use generators

  • Participants
  • Parent commits 164e5ba
  • Branches default

Comments (0)

Files changed (2)

File myobjects/snake.py

View file
  • Ignore whitespace
         """ returns the current rectangles for the snake """
         head_rect = self._head['surface'].get_rect()
         head_rect.move_ip(self._head['pos'])
-        rects = [head_rect]
+        yield head_rect
 
         for segment in self._segments:
             rect = segment['surface'].get_rect()
             rect.move_ip(segment['pos'])
-            rects.append(rect)
-        return rects
+            yield rect
 
     def update(self, game_surface):
         """ Draws the snake onto the given surface """

File python.py

View file
  • Ignore whitespace
                     rattle_num_frames_in_dir = rattle_max_frames_in_dir+1
 
             # check to see if we collided with ourself, or the bad guy
-            p_rects = python.get_rects()
-            r_rects = rattle.get_rects()
-            for x in range(len(p_rects)):
-                self_collide = False
-                for y in range(len(p_rects)):
-                    if x == y:
-                        continue 
-                    if p_rects[x].inflate(-1,-1).colliderect(p_rects[y].inflate(-1,-1)):
-                        self_collide = True
-                rattle_collide = p_rects[x].collidelist(rattle.get_rects()) > -1
-                if (self_collide or rattle_collide):
+            for r1,r2,r3 in ((r1.inflate(-1,-1),r2,r3)
+                    for r1 in python.get_rects()
+                    for r2 in python.get_rects() if r1 != r2
+                    for r3 in rattle.get_rects()):
+            
+                if r1.colliderect(r2) or r1.colliderect(r3):
                     playing = False
                     replay = gameOver(game_surface)
-
+                    break
+							
             # eat
             python.try_eat(food_items)
             rattle.try_eat(food_items)