NEAT-Flappy-Bird
NEAT-Flappy-Bird copied to clipboard
unsure of how to move to the next generation
hello, I worked with your code but tried to implement it online using pyautogui. however I have a issue with it not moving to the next generation and I don't have any idea how to do that. this is done on flappy bird.io by the way
import neat import pyautogui import os import time import numpy '''
distance between top pipe and bottom pipe is pipe y- bird y if two y's then closest to bird # location of gap
fitness function is time since starting
for fitness function
a = time.time() called when starting b = time .time() called when dead (b-a) = time of flight for bird '''
5 seconds to first pipe
just needs a if bird y = on bottom of screen
def main(genomes, config): for x in range(0,1):
try:
bird = pyautogui.locateOnScreen("fpb.png", confidence=0.2, grayscale=True, region=(850, 145, 975, 706))
nets = []
ge = []
birds = []
for genome_id, genome in genomes: # gnomes
net = neat.nn.FeedForwardNetwork.create(genome, config)
nets.append(net)
birds.append(bird)
genome.fitness = 0
ge.append(genome)
a = 1
b = 1
for x, bird in enumerate(birds):
a = 1
c = 1
start_time = time.time()
while a == c:
h = pyautogui.locateCenterOnScreen("fpb.png", confidence=0.4, grayscale=True, region=(850, 145, 975, 706))
g = pyautogui.locateOnScreen("top_pipe.png", confidence=0.8, grayscale=True, region=(850, 0, 1191, 685)) # region contains tuple showing where to look
l = pyautogui.locateOnScreen("fpb.png", confidence=0.2, grayscale=True, region=(850, 145, 975, 706))
b = pyautogui.locateOnScreen("bottom_pipe.png", confidence=0.8, grayscale=True, region=(850, 0, 1191, 200))
print(g,l)
try:
print('printing k...')
k = g[0] - l[0]
print(k)
except TypeError:
k = 0
print('k = 0')
try:
m = l[2]-b[0]
print(l[2])
print(b[0])
print(m)
if m <= -1:
m = 0
print(m)
except TypeError:
m = 0
p = pyautogui.locateCenterOnScreen("fpb.png", confidence=0.2, grayscale=True, region=(850, 145, 975, 706))
output = net.activate(((p[1]), (k), (m)))
print(output)
q = [0.5]
if output >= q:
pyautogui.click(l)
if output == [-1.0]:
end_time = time.time()
ge[x].fitness -= 1 # reduces fitness score for birds that hit a pipe
birds.pop(x)
nets.pop(x)
ge.pop(x)
if pyautogui.locateCenterOnScreen("fpb.png", confidence=0.4, grayscale=True, region=(850, 145, 975, 706)) == None:
print('dead bird')
end_time = time.time()
if pyautogui.locateOnScreen("pipedeath.png", confidence=0.5, grayscale=True) != None:
ge[x].fitness = (start_time-end_time)
ge[x].fitness -= 1 # reduces fitness score for birds that hit a pipe
birds.pop(x)
nets.pop(x)
ge.pop(x)
elif pyautogui.locateOnScreen('pipedeath.png', confidence=0.5, grayscale=True) == None:
ge[x].fitness = (start_time-end_time)
birds.pop(x)
nets.pop(x)
ge.pop(x)
pyautogui.click(864, 516)
try:
if g[1] >= [665]:
print('dead bird')
end_time = time.time()
if pyautogui.locateOnScreen("pipedeath.png", confidence=0.5, grayscale=True) != None:
ge[x].fitness = (start_time-end_time)
ge[x].fitness -= 1 # reduces fitness score for birds that hit a pipe
birds.pop(x)
nets.pop(x)
ge.pop(x)
elif pyautogui.locateOnScreen('pipedeath.png', confidence=0.5, grayscale=True) == None:
ge[x].fitness = (start_time-end_time)
birds.pop(x)
nets.pop(x)
ge.pop(x)
except TypeError:
pass
pyautogui.click(864, 516)
except IndexError:
neat.generation +=1
# gets location of birdif bird.y + pyautogui.locateOnscreen('fpb.png', confidence=0.5, grayscale=True) >= # y of dead bird
# create an array that keeps the current total of pipes passed as an array if pyautogui.locateOnScreen # add one for each number seen for above 10 do one and 10 minus current number and 20 minus and thirty minus
def run(config_file): config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file)
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
gen = 0
if gen <=50:
gen += 1
winner = p.run(main)
if name == "main": local_dir = os.path.dirname(file) config_path = os.path.join(local_dir, 'neat-config.txt') run(config_path)
you can use the p.run(eval_genomes, num_of_generations) winner = p.run(eval_genomes, num)
you can use the p.run(eval_genomes, num_of_generations) winner = p.run(eval_genomes, num)
I have also run into this problem. My program only runs once then it stops. Not sure why
I have same issue too.