-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
149 lines (117 loc) · 3.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pygame
import os
import time
import neat
from model import Bird, Base, Pipe
pygame.font.init()
GENERATION = 0
WN_WIDTH = 500
WN_HIGHT = 800
BG_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs', 'bg.png')))
STAT_FONT = pygame.font.SysFont('comicsans', 50)
def draw_window(window, birds, base, pipes, score, gen):
window.blit(BG_IMG, (0,0))
for pipe in pipes:
pipe.draw(window)
score = STAT_FONT.render("Score: " + str(score), 1, (255,255,255))
window.blit(score, (WN_WIDTH - 10 - score.get_width(), 10))
generation = STAT_FONT.render("Genration: " + str(gen), 1, (255,255,255))
window.blit(generation, (10, 10))
base.draw(window)
for bird in birds:
bird.draw(window)
pygame.display.update()
def main(genomes, config):
global GENERATION
GENERATION +=1
genome = []
nets = []
birds = []
pipes = [Pipe(600)]
base = Base(730)
for _, g in genomes:
g.fitness = 0.0 # start with fitness level of 0
net = neat.nn.FeedForwardNetwork.create(g, config)
nets.append(net)
birds.append(Bird(230,350))
genome.append(g)
window = pygame.display.set_mode((WN_WIDTH, WN_HIGHT))
pygame.display.set_caption('Flappy Bird')
clock = pygame.time.Clock()
score = 0
run = True
# start = True
# e = True
while run:
clock.tick(40)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if len(birds) == 0:
break
for i,bird in enumerate(birds):
next_pipe = 0
if bird.x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
next_pipe = 1
bird.move()
genome[i].fitness += 0.1
result = nets[i].activate((bird.y, abs(bird.y - pipes[next_pipe].height), abs(bird.y - pipes[next_pipe].bottom)))
if result[0] > 0.5:
bird.jump()
if base.collide(bird):
genome[i].fitness -= 1
birds.pop(i)
nets.pop(i)
genome.pop(i)
continue
remove_pipes = []
passed = False
crashed = False
for pipe in pipes:
if pipe.collide(bird):
genome[i].fitness -= 1
birds.pop(i)
nets.pop(i)
genome.pop(i)
crashed = True
if pipe.x + pipe.PIPE_TOP.get_width() < 0:
remove_pipes.append(pipe)
if not pipe.passed and pipe.x < bird.x:
pipe.passed = True
passed = True
if crashed:
continue
# if the bird passed the pipe increment score and add new pipe
if passed:
genome[i].fitness += 1
score += 1
pipes.append(Pipe(600))
# delete pipes that are out of the screen
for remove in remove_pipes:
pipes.remove(remove)
# check if the bird hit the ground
if bird.y + bird.img.get_height() >= 730 or bird.y < 0:
genome[i].fitness -= 1
birds.pop(i)
nets.pop(i)
genome.pop(i)
continue
for pipe in pipes:
pipe.move()
base.move()
draw_window(window, birds, base, pipes, score, GENERATION)
def play_ai(config_path):
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(5))
winner = p.run(main, 20)
# Display the winning genome.
print('\nBest genome:\n{!s}'.format(winner))
if __name__ == '__main__':
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'config.txt')
play_ai(config_path)