From 106c4799fc35587dab41ddd8b54813e861333624 Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Tue, 27 Dec 2022 08:58:36 +0000 Subject: [PATCH] allow moving into a snake that is moving away on that frame, and disallow moving into the same space --- snake.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/snake.py b/snake.py index 2add650..2025c90 100644 --- a/snake.py +++ b/snake.py @@ -346,21 +346,27 @@ class GameArea: 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 -- 2.34.1