This repository has been archived on 2025-05-08. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Snake/snake.py
2019-04-28 01:08:24 +02:00

49 lines
1.5 KiB
Python

from typing import Tuple, List
from directions import *
from game import Game
class Snake:
def __init__(self, name: str, colour: Tuple[int, int, int]):
self.name: str = name
self.colour: Tuple[int, int, int] = colour
self.board: Game = None
self.body: List[Tuple[int, int]] = None
self.direction: int = EAST
self.can_change_direction: bool = True
self.dead: bool = True
def register(self, game: Game, body: List[Tuple[int, int]]):
self.board: Game = game
self.body: List[Tuple[int, int]] = body
self.dead: bool = False
def change_direction(self, new_direction: int):
if self.can_change_direction and (new_direction + self.direction) % 2:
self.direction: int = new_direction
self.can_change_direction: bool = False
def tick(self):
if self.dead:
return
headx, heady = self.body[-1]
if self.direction == NORTH:
heady -= 1
elif self.direction == EAST:
headx += 1
elif self.direction == SOUTH:
heady += 1
elif self.direction == WEST:
headx -= 1
if not (0 <= headx < self.board.width and 0 <= heady < self.board.height):
self.dead: bool = True
return
if not self.board.eat_apple(headx, heady):
self.body.pop(0)
if (headx, heady) in self.body:
self.dead: bool = True
return
self.body.append((headx, heady))
self.can_change_direction: bool = True