# HG changeset patch # User Simon Cross # Date 1316032197 -7200 # Node ID c3018764942a1a9154e6a46a7893eedd3ff0a1fa # Parent 321f2eef40e3b7c4e91126aa0aa7c44969109032 Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit). diff -r 321f2eef40e3 -r c3018764942a mamba/snake.py --- 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):