I'm trying to make snake AI with python-NEAT library. The problem i have been facing is with the inputs to the neural network. Right now, I'm just calculating the distance of it's from all of the walls (top, right, left, bottom) and distance of it from the food and feeding it to the neural network. on other hand on many YouTube videos and other threads on different platforms people are talking about checking if the coast to the left, right and front is clear and where the food is located. My question is how do i get that. Do i use opencv to grab images or is there other way in pygame?
Here is my snake object
class Snake:
def __init__(self):
self.snake_block = 10
self.x = int(round(random.randrange(0, height - self.snake_block) / 10.0) * 10.0)
self.y = int(round(random.randrange(0, height - self.snake_block) / 10.0) * 10.0)
self.dx = 0
self.dy = 0
self.snake_head = []
self.snake_full = []
self.snake_length = 1
self.dtw = 0
self.dbw = 0
self.dlw = 0
self.drw = 0
self.brain = SnakeAI()
self.direction = 4
self.dfx = 0
self.dfy = 0
self.step_allowed = 100
self.decesion = []
def draw(self):
for x in self.snake_full:
pygame.draw.rect(screen, red, (x[0], x[1], self.snake_block, self.snake_block))
def move(self):
if self.direction is 0:
self.dy = -self.snake_block
self.dx = 0
if self.direction is 1:
self.dy = self.snake_block
self.dx = 0
if self.direction is 2:
self.dy = 0
self.dx = -self.snake_block
if self.direction is 3:
self.dy = 0
self.dx = self.snake_block
self.step_allowed -= 1
self.x += self.dx
self.y += self.dy
self.snake_head = []
self.snake_head.append(self.x)
self.snake_head.append(self.y)
self.snake_full.append(self.snake_head)
if len(self.snake_full) > self.snake_length:
del self.snake_full[0]
self.draw()
def grow(self):
self.snake_length += 1
def senses(self, fx, fy):
self.dtw = self.y - 0
self.dbw = self.y - height
self.dlw = self.x - 0
self.drw = self.x - width
self.dfx = self.x - fx
self.dfy = self.y - fy
here is how i'm feeding it to the neural netwrok
snake.senses(food.x, food.y)
output = nets[snakes.index(snake)].activate((height, abs(snake.dbw), width, abs(snake.drw),
abs(snake.dfx), abs(snake.dfy)))
output = int(np.argmax(output))
snake.direction = output
snake.move()