Mercurial > mamba
changeset 307:09ec51b9d385
Queue movement commands and handle them sanely. (FSVO)
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Fri, 16 Sep 2011 18:14:26 +0200 |
parents | 764247c4047e |
children | 31bbd0ed9b63 |
files | mamba/snake.py mamba/widgets/game.py |
diffstat | 2 files changed, 22 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/mamba/snake.py Fri Sep 16 18:13:13 2011 +0200 +++ b/mamba/snake.py Fri Sep 16 18:14:26 2011 +0200 @@ -27,6 +27,8 @@ self.frac_ds = 0.0 self.mutation = None self.coiled = True + self._orientation_changes = [] + self._already_oriented = False head = property(fget=lambda self: self.segments[0]) tail = property(fget=lambda self: self.segments[-1]) @@ -76,6 +78,7 @@ shifted, ds = self.head.shift_head(ds) if shifted: self.coiled = False + self._already_oriented = False self.head.shifted_tile() self.head.set_orientation(self.orientation) else: @@ -95,13 +98,22 @@ segment.shift_pixels(ds) world.interact(segment) - def set_orientation(self, 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._set_orientation() + + def send_new_direction(self, orientation): + self._orientation_changes.append(orientation) + self._set_orientation() + + def _set_orientation(self): + if self._already_oriented: + return + while self._orientation_changes: + orientation = self._orientation_changes.pop(0) + if ((0 == orientation[0] == self.head.orientation[0]) + or (0 == orientation[1] == self.head.orientation[1])): + continue self.orientation = orientation + self._already_oriented = True def check_self_crash(self): if self.coiled:
--- a/mamba/widgets/game.py Fri Sep 16 18:13:13 2011 +0200 +++ b/mamba/widgets/game.py Fri Sep 16 18:14:26 2011 +0200 @@ -26,10 +26,10 @@ def create_action_map(self): actions = {} - actions[K_LEFT] = (self.world.snake.set_orientation, (LEFT,)) - actions[K_RIGHT] = (self.world.snake.set_orientation, (RIGHT,)) - actions[K_DOWN] = (self.world.snake.set_orientation, (DOWN,)) - actions[K_UP] = (self.world.snake.set_orientation, (UP,)) + actions[K_LEFT] = (self.world.snake.send_new_direction, (LEFT,)) + actions[K_RIGHT] = (self.world.snake.send_new_direction, (RIGHT,)) + actions[K_DOWN] = (self.world.snake.send_new_direction, (DOWN,)) + actions[K_UP] = (self.world.snake.send_new_direction, (UP,)) return actions def action_callback(self, ev, widget):