summaryrefslogtreecommitdiff
path: root/Snake.py
blob: c53af73ca60a17f969bccd61073a7e5b7e272243 (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
26
27
28
29

import Vec
from Vec import Vec2
import Game

class Snake:

    cells = []
    heading = Vec.up
    game = None
    hasCollided = False

    def __init__(self, cells: list[Vec2], game: Game):
        self.cells = cells
        self.game = game

    def move(self):
        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]