i am trying to re-create the dino game from Chrome and automatize it using neat.
The dinosaur has a fixed X position and can only jump. The obstacles move towards the player are procedurally generated: i have a queue containing all the obstacles and when the x position of the last one has reached a certain position, a function generates another obstacle. If the first obstacle has gone too left, it gets popped, so the next one becomes the first.
The game is contained inside a Game() class. The constructor calls a reset function that creates an array for all the dinos controlled by the AI, the genomes and the networks.
def Reset(self, genomes, config):
self.nets = []
self.ge = []
self.dinos = []
for n, g in genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
self.nets.append(net)
dino = Dino()
self.dinos.append(dino)
g.fitness = 0
self.ge.append(g)
self.run()
In the update loop, i iterate through each remaning dino and i add a score to them. The output of the network is processed by giving it the dino x position, the x position of the first obstacle in the Queue and a bool that stes if the dino can jump or not. I also check the collision between the dinos and the obstacles. If the dino collides, his fitness score is lowered and is deleted from the game.
def update(self):
self.checkCollision()
for x, dino in enumerate(self.dinos):
self.ge[x].fitness += 0.1
output = self.nets[x].activate((dino.x, self.obstacleQueue.queue[0].rect.x, dino.canJump))
if output[0] > 0.5:
if dino.canJump:
dino.jump()
It actually (sometimes) works as it should: there are some dinos that jump trough the obstacles, but they can't get past the second. Then after 2 or 3 generations, they can't get past the first obstacle.
