summaryrefslogtreecommitdiff
path: root/Snake.py
diff options
context:
space:
mode:
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]