-
Notifications
You must be signed in to change notification settings - Fork 0
/
flappy.py
176 lines (134 loc) · 4.75 KB
/
flappy.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import pygame
import os
import neat
from Bird import Bird
from Pipe import Pipe
from Floor import Floor
pygame.font.init() # init font
local_dir = os.getcwd()
'''
WINDOW STATS
'''
WIN_WIDTH = 600
WIN_HEIGHT = 800
FLOOR = 730
STAT_FONT = pygame.font.SysFont("comicsans", 50)
END_FONT = pygame.font.SysFont("comicsans", 70)
local_dir = os.getcwd()
WIN = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("Flappy Bird")
'''
LOAD IMAGES
'''
bg_img = pygame.transform.scale(pygame.image.load(os.path.join(local_dir,"res","background.png")).convert_alpha(), (600, 900))
gen = 0
def draw_window(win, birds, pipes, floor, score, gen):
if gen == 0:
gen = 1
win.blit(bg_img, (0,0))
for pipe in pipes:
pipe.draw(win)
floor.draw(win)
for bird in birds:
bird.draw(win)
# score
score_label = STAT_FONT.render("Score: " + str(score),1,(255,255,255))
win.blit(score_label, (WIN_WIDTH - score_label.get_width() - 15, 10))
# generations
score_label = STAT_FONT.render("Gens: " + str(gen-1),1,(255,255,255))
win.blit(score_label, (10, 10))
# alive
score_label = STAT_FONT.render("Alive: " + str(len(birds)),1,(255,255,255))
win.blit(score_label, (10, 50))
pygame.display.update()
def eval_genomes(genomes, config):
"""
runs the simulation of the current population of
birds and sets their fitness based on the distance they
reach in the game.
"""
global WIN, gen
win = WIN
gen += 1
# start by creating lists holding the genome itself, the
# neural network associated with the genome and the
# bird object that uses that network to play
nets = []
birds = []
ge = []
for genome_id, genome in genomes:
genome.fitness = 0 # start with fitness level of 0
net = neat.nn.FeedForwardNetwork.create(genome, config)
nets.append(net)
birds.append(Bird(230,350))
ge.append(genome)
floor = Floor(FLOOR)
pipes = [Pipe(700)]
score = 0
clock = pygame.time.Clock()
run = True
while run and len(birds) > 0:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
break
pipe_ind = 0
if len(birds) > 0:
if len(pipes) > 1 and birds[0].x > pipes[0].x + pipes[0].WIDTH: # determine whether to use the first or second
pipe_ind = 1 # pipe on the screen for neural network input
for x, bird in enumerate(birds): # Reward each bird for each frame they sta alive
ge[x].fitness += 0.1
bird.move()
# send bird location, top pipe location and bottom pipe location and determine from network whether to jump or not
output = nets[birds.index(bird)].activate((bird.y, abs(bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom)))
if output[0] > 0.5:
bird.jump()
floor.move()
# MOVE THE SCENE
rem = []
add_pipe = False
for pipe in pipes:
pipe.move()
# check for collision
for bird in birds:
if pipe.collide(bird, win): # REDUCE FITNESS OF COLLIDED BIRDS AND REMOVE THEM
ge[birds.index(bird)].fitness -= 1
nets.pop(birds.index(bird))
ge.pop(birds.index(bird))
birds.pop(birds.index(bird))
if pipe.x + pipe.WIDTH < 0:
rem.append(pipe)
if not pipe.passed and pipe.x < bird.x:
pipe.passed = True
add_pipe = True
if add_pipe:
score += 1
pipes.append(Pipe(WIN_WIDTH))
for r in rem:
pipes.remove(r)
for bird in birds:
if bird.y + bird.img.get_height() - 10 >= FLOOR or bird.y < -50:
nets.pop(birds.index(bird))
ge.pop(birds.index(bird))
birds.pop(birds.index(bird))
draw_window(WIN, birds, pipes, floor, score, gen)
def run(config_file):
"""
RUN THE NEAT ALGORITHM
"""
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_file)
# Creating a population
p = neat.Population(config)
# Run for 10 generations
winner = p.run(eval_genomes, 10)
# show final stats
print('\nBest genome:\n{!s}'.format(winner))
if __name__ == '__main__':
local_dir = os.getcwd()
config_path = os.path.join(local_dir, 'config-feedforward.txt')
run(config_path)