summaryrefslogtreecommitdiff
path: root/Snake.py
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-11-01 16:32:45 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-11-01 16:32:45 +0200
commitae319acbf4c29551be07c1406c434e129a84b51e (patch)
tree5533cf9db4c73a7619c6107dca0842c4bc8f9319 /Snake.py
parent57f20a5ef761985b34817846d471a064b180e089 (diff)
downloadSnakePuzzle-ae319acbf4c29551be07c1406c434e129a84b51e.tar.gz
SnakePuzzle-ae319acbf4c29551be07c1406c434e129a84b51e.zip
feat: boxes, pushing, failing before entering 'solid' tiles
Diffstat (limited to 'Snake.py')
-rw-r--r--Snake.py24
1 files changed, 15 insertions, 9 deletions
diff --git a/Snake.py b/Snake.py
index 81c3b0b..c53af73 100644
--- a/Snake.py
+++ b/Snake.py
@@ -1,23 +1,29 @@
import Vec
+from Vec import Vec2
+import Game
class Snake:
cells = []
heading = Vec.up
+ game = None
+ hasCollided = False
- def __init__(self, cells):
+ def __init__(self, cells: list[Vec2], game: Game):
self.cells = cells
+ self.game = game
def move(self):
- self.cells.pop()
- self.cells.insert(0, self.cells[0] + self.heading)
-
- def headInTail(self):
- res = False
- for i in range(1, len(self.cells)):
- res = res or (self.cells[0] == self.cells[i])
- return res
+ nextPos = self.cells[0] + self.heading
+ if self.game.enter(nextPos, self.heading):
+ if nextPos in self.cells[1:len(self.cells)-1]:
+ self.hasCollided = True
+ else:
+ self.cells.pop()
+ self.cells.insert(0, nextPos)
+ else:
+ self.hasCollided = True
def head(self):
return self.cells[0]