From ae319acbf4c29551be07c1406c434e129a84b51e Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sat, 1 Nov 2025 16:32:45 +0200 Subject: feat: boxes, pushing, failing before entering 'solid' tiles --- Snake.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'Snake.py') 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] -- cgit v1.2.3