Commits

sirchristian committed 4a41dbf Draft

Fixed colliding with self with generators

  • Participants
  • Parent commits 9578419

Comments (0)

Files changed (2)

File myobjects/snake.py

             rect.move_ip(segment['pos'])
             yield rect
 
+    def head_hit_body(self):
+        """ returns True if the head hit the body """
+        head_rect = self._head['surface'].get_rect()
+        head_rect.inflate_ip(-1,-1)
+        head_rect.move_ip(self._head['pos'])
+        for segment in self._segments:
+            rect = segment['surface'].get_rect()
+            rect.move_ip(segment['pos'])
+            if (head_rect.colliderect(rect)):
+                return True
+
+        return False
+
     def update(self, game_surface):
         """ Draws the snake onto the given surface """
         head_rect = self._head['surface'].get_rect()
                 if not moved_rattle:
                     rattle_num_frames_in_dir = rattle_max_frames_in_dir+1
 
-            # check to see if we collided with ourself, or the bad guy
-            for r1,r2,r3 in ((r1.inflate(-1,-1),r2,r3)
+            # detect collisions with the bad snake
+            for r1,r2 in ((r1,r2)
                     for r1 in python.get_rects()
-                    for r2 in python.get_rects() if r1 != r2
-                    for r3 in rattle.get_rects()):
+                    for r2 in rattle.get_rects()):
             
-                if r1.colliderect(r2) or r1.colliderect(r3):
+                if r1.colliderect(r2):
                     playing = False
                     replay = gameOver(game_surface)
                     break
-							
+
+            # detect collisions with ourself
+            if python.head_hit_body():
+                playing = False
+                replay = gameOver(game_surface)
+                break
+
+            # eat
+            python.try_eat(food_items)
             # eat
             python.try_eat(food_items)
             rattle.try_eat(food_items)
         Returns True if the game should restart """
 
     text = 'GAME OVER'
-    font = pygame.font.SysFont('None', 128)
+    font = pygame.font.SysFont('Tahoma', 128)
     line = font.render(text, True, (20, 20, 220))
 
     x_center = (SCREENSIZE[0] / 2) - (line.get_width() / 2)