-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestingmodel.py
More file actions
472 lines (337 loc) · 14.5 KB
/
testingmodel.py
File metadata and controls
472 lines (337 loc) · 14.5 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# LIBRARY IMPORTS
import pygame # pygame module
import sys
import os
import neat
import time
import random
import mainmenu
import pickle # use this module to save the best bird into a file and then you can load in the file and use the neural network associated with it
pygame.font.init()
pygame.init()
# sets the title to the text within the quotes
pygame.display.set_caption("FlappyBirdAI (NEAT)")
# Constants
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 700
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.NOFRAME)
FONT = pygame.font.SysFont("comicsans", 50)
FONT2 = pygame.font.Font('freesansbold.ttf', 20)
FONT3 = pygame.font.SysFont("comicsans", 35)
GEN = 0 # keeping track of the current generation for visual purpose
# tranform.scale2x enlarges the image
BIRD_IMGS = [
pygame.transform.scale2x(pygame.image.load(os.path.join("./imgs", "bird1.png"))
), pygame.transform.scale2x(pygame.image.load(os.path.join("./imgs", "bird2.png"))),
pygame.transform.scale2x(pygame.image.load(
os.path.join("./imgs", "bird3.png")))
]
PIPE_IMG = pygame.transform.scale2x(pygame.image.load(
os.path.join("./imgs", "pipe.png")))
BASE_IMG = pygame.transform.scale2x(pygame.image.load(
os.path.join("./imgs", "base.png")))
background_image = pygame.image.load(
os.path.join("./imgs", "bg.png"))
BG_IMG = pygame.transform.scale(background_image, (500, 700))
TestBG = pygame.image.load(os.path.join(
"./imgs", "TestingStatsRect.png"))
class Bird:
IMGS = BIRD_IMGS
MAX_ROTATION = 25 # Will be used to tilt the bird up and down
ROT_VEL = 20 # Rotation velocity
ANIMATION_TIME = 5
def __init__(self, x, y):
self.x = x
self.y = y
self.tilt = 0
self.tick_count = 0
self.vel = 0
self.height = self. y
self.img_count = 0
self.img = self.IMGS[0]
self.rect = pygame.Rect(
self.x, self.y, self.img.get_width(), self.img.get_height())
self.color = (random.randint(0, 255), random.randint(
0, 255), random.randint(0, 255))
self.RECT_Y = 0
def jump(self):
self.vel = -10.5
self.tick_count = 0 # keeps track of when we last jumped
self.height = self.y
def move(self):
self.tick_count += 1
displacement = self.vel*self.tick_count + 1.5*self.tick_count**2
if displacement >= 16: # defining the terminal velocity here
displacement = 16
if displacement < 0:
displacement -= 2
self.y = self.y + displacement
# tilting conditions defined below
if displacement < 0 or self.y < self.height + 50:
if self.tilt < self.MAX_ROTATION:
self.tilt = self.MAX_ROTATION
else:
if self.tilt > - 90:
self.tilt -= self.ROT_VEL
self.RECT_Y = self.y
def draw_hitbox(self):
self.rect.y = self.RECT_Y
pygame.draw.rect(SCREEN, self.color, (self.rect.x,
self.rect.y, self.rect.width, self.rect.height), 2)
def draw(self, SCREEN):
self.img_count += 1
if self.img_count < self.ANIMATION_TIME:
self.img = self.IMGS[0]
elif self.img_count < self.ANIMATION_TIME * 2:
self.img = self.IMGS[1]
elif self.img_count < self.ANIMATION_TIME * 3:
self.img = self.IMGS[2]
elif self.img_count < self.ANIMATION_TIME * 4:
self.img = self.IMGS[1]
elif self.img_count == self.ANIMATION_TIME * 4 + 1:
self.img = self.IMGS[0]
self.img_count = 0
# this if statement prevents the bird from flapping when it's doing a 90 degree nose dive downwards
if self.tilt <= -90:
self.img = self.IMGS[1]
self.img_count = self.ANIMATION_TIME * 2
# rotates the bird on its center
rotated_image = pygame.transform.rotate(self.img, self.tilt)
new_rect = rotated_image.get_rect(
center=self.img.get_rect(topleft=(self.x, self.y)).center)
SCREEN.blit(rotated_image, new_rect.topleft)
# draws the hitbox around the dinosaurs
def get_mask(self):
return pygame.mask.from_surface(self.img)
class Pipe:
GAP = 200 # the space between the 2 pipes
VEL = 5 # the speed at which the pipes will move across the screen
def __init__(self, x):
self.x = x
self.height = 0
self.gap = 100
self.top = 0
self.bottom = 0
# this line flips the pipe image for the top pipe
self.PIPE_TOP = pygame.transform.flip(PIPE_IMG, False, True)
self.PIPE_BOTTOM = PIPE_IMG
self.passed = False # This is set to true if the bird has already passed the pipe
self.set_height()
def set_height(self):
self.height = random.randrange(50, 450)
# math for setting the top pipe below
self.top = self.height - self.PIPE_TOP.get_height()
self.bottom = self.height + self.GAP
def move(self):
self.x -= self.VEL
def draw(self, SCREEN):
SCREEN.blit(self.PIPE_TOP, (self.x, self.top)) # displays the top pipe
SCREEN.blit(self.PIPE_BOTTOM, (self.x, self.bottom))
def collide(self, bird):
# in this project, we will you masks for pixel perfect collisions
# mask is basically an array/list of location of the pixels inside of a box
bird_mask = bird.get_mask()
top_pipe_mask = pygame.mask.from_surface(self.PIPE_TOP)
bottom_pipe_mask = pygame.mask.from_surface(self.PIPE_BOTTOM)
# we have to calculate the offset.
# An offset is how far these masks are from eachother. The distance between an 2 left hand corners in the case of pygame
# the offset of the bird from the top pipe
top_offset = (self.x - bird.x, self.top - round(bird.y))
# the offset of the bird from the bottom pipe
bottom_offset = (self.x - bird.x, self.bottom - round(bird.y))
# returns None if they don't collide
bottom_point = bird_mask.overlap(bottom_pipe_mask, bottom_offset)
top_point = bird_mask.overlap(top_pipe_mask, top_offset)
# if there is a collision, it returns True otherwise it returns False
if top_point or bottom_point:
return True
return False
class Base:
VEL = 5
WIDTH = BASE_IMG.get_width()
IMG = BASE_IMG
def __init__(self, y):
self.y = y
self.x1 = 0
self.x2 = self.WIDTH
def move(self):
self.x1 -= self.VEL
self.x2 -= self.VEL
# these statements creates the infinite moving background
if self.x1 + self.WIDTH < 0:
self.x1 = self.x2 + self.WIDTH
if self.x2 + self.WIDTH < 0:
self.x2 = self.x1 + self.WIDTH
def draw(self, SCREEN):
SCREEN.blit(self.IMG, (self.x1, self.y))
SCREEN.blit(self.IMG, (self.x2, self.y))
class MenuButton(): # Custom class for buttons in pygame
# Init method defined
def __init__(self, image, hoverimage, pos):
self.image = image
self.hoverimage = hoverimage
self.x_pos = pos[0]
self.y_pos = pos[1]
self.rect = self.image.get_rect(center=(self.x_pos, self.y_pos))
# Method to update the button
def update(self, screen):
if self.image is not None:
screen.blit(self.image, self.rect)
# Method to checkout for mouse inputs
def checkForInput(self, position):
if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom):
return True
return False
# Method to change the text color of the button on hover
def ButtonHover(self, position, image, hoverimage):
if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom):
self.image = hoverimage
else:
self.image = image
def draw_window(SCREEN, birds, pipes, base, score, ButtonList, MENU_MOUSE_POS):
global population
SCREEN.blit(BG_IMG, (0, 0))
for pipe in pipes:
pipe.draw(SCREEN)
base.draw(SCREEN)
for bird in birds:
bird.draw(SCREEN)
# Renders the Training Stats Rectangle on the screen
SCREEN.blit(TestBG, (500, 0))
score_text = FONT3.render(str(score), True, (255, 255, 255))
SCREEN.blit(score_text, (880, 52))
generation_text = FONT.render(
"Generation: Best Offspring", 1, "#AD06E8")
SCREEN.blit(generation_text, (640, 350))
population_text = FONT.render(
"Birds Remaining: " + str(len(birds)), 1, "#AD06E8")
SCREEN.blit(population_text, (590, 450))
for button in ButtonList:
# Changes the color of the buttons on hover
button.ButtonHover(MENU_MOUSE_POS, button.image, button.hoverimage)
button.update(SCREEN)
pygame.display.update()
# Main game function
def game(genomes, config):
global nets, ge, birds, population
nets = []
ge = []
birds = [Bird(210, 300)] # bird = Bird(210, 300) # orginal code line
# genomes returns a tuple (id, object). We only need the object
for _, genome in genomes:
net = neat.nn.FeedForwardNetwork.create(genome, config)
nets.append(net)
ge.append(genome)
# genome.fitness = 0 # setting the initial fitness of our birds to 0
base = Base(620)
pipes = [Pipe(550)]
clock = pygame.time.Clock()
score = 0
run = True
while run:
clock.tick(30)
MENU_MOUSE_POS = pygame.mouse.get_pos()
# this code tackles the problem where there are 2 pairs of pipes on the screen and the bird doesn't know which to calculate the distance from
pipe_index = 0
if len(birds) > 0:
if len(pipes) > 1 and birds[0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
pipe_index = 1
else: # if there are no birds, we quit this generation/running the game
run = False
break
# inputs to the neural network
for x, bird in enumerate(birds):
bird.move()
ge[x].fitness += 0.1 # initial survival fitness given to the bird
# Activating the neural network with our inputs
# The comment below talks about the distance calculation of the bird from the top and bottom pipes.
# abs(bird.y - pipes[pipe_index].height) - height here gives us the location of the top pipe, abs(bird.y - pipe[pipe_index].bottom) - bottom gives us the location of the bottom pipe
output = nets[x].activate((bird.y, abs(
bird.y - pipes[pipe_index].height), abs(bird.y - pipes[pipe_index].bottom)))
if output[0] > 0.5: # output is a list
bird.jump()
add_pipe = False
remove_pipe = []
for pipe in pipes:
for x, bird in enumerate(birds):
if pipe.collide(bird):
# decrements the fitness if a bird hits a pipe
ge[x].fitness -= 1
# removing bird, its neural net, and its genes from the 3 lists
birds.pop(x)
nets.pop(x)
ge.pop(x)
if not pipe.passed and pipe.x < bird.x:
pipe.passed = True
add_pipe = True
if pipe.x + pipe.PIPE_TOP.get_width() < 0:
remove_pipe.append(pipe)
pipe.move()
# appends a new pipe for display once we pass a pipe and adds the scores
if add_pipe:
for g in ge:
g.fitness += 5 # incrementing the fitness of a bird if it passes thru a pipe succesfully
score += 1
pipes.append(Pipe(550))
# removes the passed pipes from the remove_pipe list
for r in remove_pipe:
pipes.remove(r)
for x, bird in enumerate(birds):
# checks whether the bird has hit the floor
if bird.y + bird.img.get_height() >= 620 or bird.y < 0:
birds.pop(x)
nets.pop(x)
ge.pop(x)
base.move() # displays the animation of the moving base
BACK_BUTTON = MenuButton(image=pygame.image.load(
"./imgs/GameBackButton.png"), hoverimage=pygame.image.load(
"./imgs/GameBackButtonHover.png"), pos=(600, 635))
QUIT_BUTTON = MenuButton(image=pygame.image.load(
"./imgs/GameQuitButton.png"), hoverimage=pygame.image.load(
"./imgs/GameQuitButtonHover.png"), pos=(900, 635))
# List to contain the buttons
ButtonList = [BACK_BUTTON, QUIT_BUTTON]
draw_window(SCREEN, birds, pipes, base,
score, ButtonList, MENU_MOUSE_POS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if pygame.key.get_pressed()[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if BACK_BUTTON.checkForInput(MENU_MOUSE_POS):
mainmenu.MainMenu()
pass
if QUIT_BUTTON.checkForInput(MENU_MOUSE_POS):
pygame.quit()
sys.exit()
# CODE TO USE FOR USING A SAVED GENOME
def replay_genome(config_path, genome_path="BestBirdNN"):
global population
# Load requried NEAT config
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
population = neat.Population(config)
# adding stats reporters. These are optional
# these give us some statistical output whenever we run the program
population.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
population.add_reporter(stats)
# Unpickle saved winner
with open(genome_path, "rb") as f: # the 'b' in rb stands for binary cuz the file is binary
genome = pickle.load(f)
# Convert loaded genome into required data structure
genomes = [(1, genome)]
# Call game with only the loaded genome
game(genomes, config)
def testrun():
# this gives us the path to our local/working/current directory
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'testconfig.txt')
# Testing the saved model
genome_path = os.path.join(local_dir, 'BestBirdNN')
replay_genome(config_path, genome_path="BestBirdNN")
if __name__ == "__main__":
testrun()