summaryrefslogtreecommitdiff
path: root/Snake.py
blob: 84f3505d09beee5821a4f120eb0ca0d4f262fb35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

import Vec
from Vec import Vec2
import Game

class Snake:


    def __init__(self, cells: list[Vec2], game: "Game.Game") -> None:
        self.heading = Vec.up
        self.hasCollided = False
        self.cells = cells
        self.game = game

    def move(self) -> None:
        nextPos = self.cells[0] + self.heading
        last = self.cells.pop()
        if self.game.enter(nextPos, self.heading):
            self.cells.insert(0, nextPos)
        else:
            self.cells.append(last)
            self.hasCollided = True

    def head(self) -> Vec2:
        return self.cells[0]