def move(self, direction: Direction) -> (Point, bool, bool): # move the snake in a new direction snake_head = self.snake.move(direction, self.ate_food) self._n_steps += 1 if self.snake.touches_tail() or self.__is_touching_wall(): self.game_over() return Point.from_numpy( snake_head), self.ate_food, self.is_game_over() # check if the snake touches food if self.__is_touching_food(): self.ate_food = True self._n_food_eaten += 1 self._n_steps_without_food = 0 print("Ate food") self.__random_food() else: self.ate_food = False self._n_steps_without_food += 1 if self._n_steps_without_food > 1000: self.game_over() return Point.from_numpy( snake_head), self.ate_food, self.is_game_over() self.__draw_snake() self.__draw_food() return Point.from_numpy(snake_head), self.ate_food, self.is_game_over()
def move(self, direction: Direction) -> (Point, bool, bool): snake_head = self.grid.move_snake(direction, self.ate_food) self._n_steps += 1 if self.grid.snake().touches_tail( ) or self.grid.snake_is_touching_wall(): self.game_over() return Point.from_numpy( snake_head), self.ate_food, self.is_game_over() # check if the snake touches food if self.grid.snake_is_touching_food(): self.ate_food = True self._n_food_eaten += 1 self._n_steps_without_food = 0 self.grid.random_food() else: self.ate_food = False self._n_steps_without_food += 1 if self._n_steps_without_food > 1000: self.game_over() return Point.from_numpy(snake_head), self.ate_food, self.is_game_over()
def food_position(self) -> Point: return Point.from_numpy(self.grid.food().position())
def snake_head(self) -> Point: return Point.from_numpy(self.grid.snake().head())
def __is_touching_food(self) -> bool: return Point.from_numpy(self.snake.head()) == self.food_position()