changeset 232:ec23ecbbd1c6

Move U-turn checking to orientation-setting time, and only update the head's orientaiton at tile shift
author Stefano Rivera <stefano@rivera.za.net>
date Wed, 14 Sep 2011 23:35:14 +0200
parents bf7bf54ed6ff
children d081399b4adb
files mamba/snake.py
diffstat 1 files changed, 8 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/snake.py	Wed Sep 14 23:33:37 2011 +0200
+++ b/mamba/snake.py	Wed Sep 14 23:35:14 2011 +0200
@@ -20,7 +20,7 @@
         self.pending_segments = []  # segments waiting to be added
         self.segment_group = Group()
         self.segment_group.add(*reversed(self.segments))
-        self.set_orientation(orientation)
+        self.orientation = orientation
         self.speed = 120.0  # pixel / s
         self.frac_ds = 0.0
         self.mutation = None
@@ -75,6 +75,7 @@
             if shifted:
                 self.coiled = False
                 self.head.shifted_tile()
+                self.head.set_orientation(self.orientation)
             else:
                 break
             if self.pending_segments:
@@ -86,28 +87,19 @@
                     segment.shift_tile(segment.get_tile_state())
             else:
                 self.shiftup_segments(1, tile_state)
-            if not self.check_uturn():
-                self.head.set_orientation(self.orientation)
 
         self.check_self_crash()
         for segment in self.segments:
             segment.shift_pixels(ds)
             world.interact(segment)
 
-    def check_uturn(self):
-        if len(self.segments) > 1:
-            neck_x, neck_y = self.segments[1].tile_pos
-            head_x, head_y = self.head.tile_pos
-            if (head_x + self.orientation[0] == neck_x and
-                head_y + self.orientation[1] == neck_y):
-                # Don't allow the snake to go back on itself
-                # immediately. More creative self-inflicted
-                # deaths are allowed though.
-                return True
-        return False
-
     def set_orientation(self, orientation):
-        self.orientation = orientation
+        # Don't allow the snake to go back on itself
+        # immediately. More creative self-inflicted
+        # deaths are allowed though.
+        if (abs(self.head.orientation[0] - orientation[0]) < 2
+                and abs(self.head.orientation[1] - orientation[1]) < 2):
+            self.orientation = orientation
 
     def check_self_crash(self):
         if self.coiled: