allow moving into a snake that is moving away on that frame, and disallow moving...
authorGeoffrey Allott <geoffrey@allott.email>
Tue, 27 Dec 2022 08:58:36 +0000 (08:58 +0000)
committerGeoffrey Allott <geoffrey@allott.email>
Tue, 27 Dec 2022 08:58:36 +0000 (08:58 +0000)
snake.py

index 2add65025cc0a51a11e35956788e428d0b1a772b..2025c9068198eb1d9a9a4d02b4f1cc7cf1bb82d9 100644 (file)
--- 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