changeset 223:c3018764942a

Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
author Simon Cross <hodgestar@gmail.com>
date Wed, 14 Sep 2011 22:29:57 +0200
parents 321f2eef40e3
children 0dfa3f52742b
files mamba/snake.py
diffstat 1 files changed, 34 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/snake.py	Wed Sep 14 22:19:49 2011 +0200
+++ b/mamba/snake.py	Wed Sep 14 22:29:57 2011 +0200
@@ -198,6 +198,40 @@
         dx, dy = distance * dx, distance * dy
         self.rect = self.rect.move(dx, dy)
 
+    def shift_head(self, ds):
+        """Shift the head a number of pixels in the direction of it
+        orientation.
+        """
+        dx, dy = self.orientation
+        TX, TY = TILE_SIZE
+        rx, ry = self.rect.left, self.rect.top
+        tx, ty = self.tile_pos
+        # WARNING: Tri-state logic ahead
+        # (Tri-state logic is the mamba's natural habitat)
+        if dx != 0:
+            newdx = rx + ds * dx
+            newtx = newdx / TX
+            if newtx > tx:
+                self.set_tile_pos((tx + 1, ty))
+                ds = newdx - self.rect.left
+                return True, ds
+            elif newtx < tx - 1:
+                self.set_tile_pos((tx - 1, ty))
+                ds = self.rect.left - newdx
+                return True, ds
+        else:
+            newdy = ry + ds * dy
+            newty = newdy / TY
+            if newty > ty:
+                self.set_tile_pos((tx, ty + 1))
+                ds = newdy - self.rect.top
+                return True, ds
+            elif newty < ty - 1:
+                self.set_tile_pos((tx, ty - 1))
+                ds = self.rect.top - newdy
+                return True, ds
+        return False, ds
+
 
 class Head(Segment):
     CLOSED = "snake-head"
@@ -239,40 +273,6 @@
             self.mouth_close()
             self.tongue_in()
 
-    def shift_head(self, ds):
-        """Shift the head a number of pixels in the direction of it
-        orientation.
-        """
-        dx, dy = self.orientation
-        TX, TY = TILE_SIZE
-        rx, ry = self.rect.left, self.rect.top
-        tx, ty = self.tile_pos
-        # WARNING: Tri-state logic ahead
-        # (Tri-state logic is the mamba's natural habitat)
-        if dx != 0:
-            newdx = rx + ds * dx
-            newtx = newdx / TX
-            if newtx > tx:
-                self.set_tile_pos((tx + 1, ty))
-                ds = newdx - self.rect.left
-                return True, ds
-            elif newtx < tx - 1:
-                self.set_tile_pos((tx - 1, ty))
-                ds = self.rect.left - newdx
-                return True, ds
-        else:
-            newdy = ry + ds * dy
-            newty = newdy / TY
-            if newty > ty:
-                self.set_tile_pos((tx, ty + 1))
-                ds = newdy - self.rect.top
-                return True, ds
-            elif newty < ty - 1:
-                self.set_tile_pos((tx, ty - 1))
-                ds = self.rect.top - newdy
-                return True, ds
-        return False, ds
-
 
 class Body(Segment):
     def __init__(self, tile_pos):