snake.slither(self.width, self.height - 1)
r, c = snake.segments[-1]
actions[snake] = self.tiles[r][c].action()
- for snake in self.snakes:
- if not snake.alive: continue
- if not snake.turbo and self.frame % 2 == 0: continue
+ for snake in actions:
+ # prevent running into a tail that is just moving away
+ if any(snake.segments[-1] == s.segments[0] for s in actions if not actions[snake].is_score()):
+ actions[snake] = NullAction()
+ for snake in actions:
+ # if two or more snakes move into the same empty tile, they all die
+ if any(snake.segments[-1] == s.segments[-1] for s in actions if snake is not s):
+ actions[snake] = DeathAction()
+ for snake in actions:
r, c = snake.segments[-1]
action = actions[snake]
- if action.is_teleport():
- self.tiles[r][c] = EmptyTile()
- r, c = snake.segments[-1] = action.row, action.column
- self.tiles[r][c] = SnakeTile(snake)
if action.is_turbo():
snake.turbo = 100
if not action.is_score():
r0, c0 = snake.segments[0]
self.tiles[r0][c0] = EmptyTile()
snake.shrink()
+ self.tiles[r][c] = SnakeTile(snake)
+ if action.is_teleport():
+ self.tiles[r][c] = EmptyTile()
+ r, c = snake.segments[-1] = action.row, action.column
if action.is_death():
self.tiles[r][c] = DeadTile()
snake.alive = False