changeset 198:2876658d0b98

Don't allow the snake to reverse onto itself immediately.
author Simon Cross <hodgestar@gmail.com>
date Wed, 14 Sep 2011 19:29:15 +0200
parents adbf59aa81c1
children 986e72d2cb4d
files mamba/snake.py
diffstat 1 files changed, 9 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/snake.py	Wed Sep 14 19:27:22 2011 +0200
+++ b/mamba/snake.py	Wed Sep 14 19:29:15 2011 +0200
@@ -55,6 +55,15 @@
             world.interact(segment)
 
     def set_orientation(self, orientation):
+        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 + orientation[0] == neck_x and
+                head_y + orientation[1] == neck_y):
+                # Don't allow the snake to go back on itself
+                # immediately. More creative self-inflicted
+                # deaths are allowed though.
+                return
         self.orientation = orientation
 
     def crash(self):