-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
185 lines (152 loc) · 5.86 KB
/
game.py
File metadata and controls
185 lines (152 loc) · 5.86 KB
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
177
178
179
180
181
182
183
184
185
import os
import pickle
from typing import overload
import pygame.sprite
from pygame import Color
from constants import *
from objects import Bird, Pipe, BIRD_X
import neat
pygame.init()
score_font = pygame.font.SysFont("Arial", 50)
stats_font = pygame.font.Font("IBMPlexSans-Regular.ttf", 20)
PIPE_SPACING = 300
class Game:
instance_number = 0
def __init__(self):
self.score = 0
self.simulation_number = 0
self.game_is_on = True
self.win = pygame.display.set_mode((WIDTH, HEIGHT))
self.pipes = []
def init_bot(self, *, genomes=None, config=None, bird=None, max_score=None):
self.simulation_number = Game.instance_number
Game.instance_number += 1
self.human_playing = False
self.genomes = genomes
self.config = config
self.bird = bird
self.max_score = max_score
self.game_is_on = True
self.win = pygame.display.set_mode((WIDTH, HEIGHT))
self.birds = []
if not genomes:
self.birds.append(bird)
return
for _, g in genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
g.fitness = 0
self.birds.append(Bird(net, g))
def init_human(self):
self.human_playing = True
self.birds = [Bird(None, None)]
def run(self):
clock = pygame.time.Clock()
while self.game_is_on:
clock.tick(FPS)
self.events()
self.check_collision()
self.update()
self.draw(self.win)
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game_is_on = False
pygame.quit()
quit()
elif self.human_playing and event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.birds[0].jump()
def update(self):
# add pipe
if len(self.pipes) == 0 or self.pipes[-1].x < WIDTH - PIPE_SPACING:
self.pipes.append(Pipe())
for bird in self.birds[:]:
bird.update()
if bird.y < 0 or bird.y > HEIGHT:
bird.die()
self.birds.remove(bird)
if not self.human_playing:
if self.config is not None:
bird.genome.fitness += 0.01
next_pipe = self.pipes[0]
if next_pipe.x + next_pipe.width + 50 < BIRD_X:
next_pipe = self.pipes[1]
output = bird.net.activate((bird.y, next_pipe.gap_top, next_pipe.gap_top + next_pipe.gap_size))
if output[0] > 0.5:
bird.jump()
for pipe in self.pipes[:]:
pipe.update()
if pipe.x < -pipe.width:
self.pipes.remove(pipe)
self.score += 1
if not self.human_playing:
if self.config is not None:
for bird in self.birds:
bird.genome.fitness += 5
if not self.human_playing and self.max_score is not None and self.score >= self.max_score:
self.game_is_on = False
def check_collision(self):
for pipe in self.pipes:
for bird in self.birds:
if pipe.check_collision(bird):
bird.die()
self.birds.remove(bird)
break
if len(self.birds) == 0:
self.game_is_on = False
def draw_stats(self, win):
if self.config is not None:
# draw number of birds
text = stats_font.render(f"Birds: {len(self.birds)}", True, Color("white"))
win.blit(text, text.get_rect(topleft=(10, 10)))
# draw simulation number
text = stats_font.render(f"Generation: {self.simulation_number}", True, Color("white"))
win.blit(text, text.get_rect(topleft=(10, 40)))
# draw average fitness
avg_fitness = sum([bird.genome.fitness for bird in self.birds]) / len(self.birds) if len(
self.birds) > 0 else 0
text = stats_font.render(f"Avg. Fitness: {avg_fitness:.2f}", True, Color("white"))
win.blit(text, text.get_rect(topleft=(10, 70)))
else:
# Show trained
text = stats_font.render(f"Trained", True, Color("white"))
win.blit(text, text.get_rect(topleft=(10, 10)))
text = score_font.render(str(self.score), True, Color("Gray"))
win.blit(text, text.get_rect(center=(WIDTH // 2, 50)))
def draw(self, win):
win.fill(Color("black"))
for bird in self.birds:
bird.draw(win, self.pipes, show_input=True)
for pipe in self.pipes:
pipe.draw(win)
if not self.human_playing:
self.draw_stats(win)
pygame.display.flip()
def main(genomes, config):
game = Game()
game.init_bot(genomes=genomes, config=config, max_score=50)
game.run()
def run(config_path):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
if os.path.exists("best_genome"):
with open("best_genome", 'rb') as file:
winner = pickle.load(file)
net = neat.nn.FeedForwardNetwork.create(winner, config)
bird = Bird(net, None)
game = Game()
game.init_bot(bird=bird, max_score=1000)
game.run()
else:
population = neat.Population(config)
population.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
population.add_reporter(stats)
winner = population.run(main)
with open("best_genome", 'wb') as file:
pickle.dump(winner, file)
if __name__ == '__main__':
game = Game()
game.init_human()
game.run()