|
1 |
| -#AI-driven AI Flappy Bird |
| 1 | +# AI-driven AI Flappy Bird |
2 | 2 | import pygame
|
3 | 3 | import neat
|
4 | 4 | import os
|
|
10 | 10 |
|
11 | 11 | # Bird images
|
12 | 12 | BIRD_IMAGES = [
|
13 |
| - pygame.transform.scale2x(pygame.image.load(os.path.join("images", "bird1.png"))), |
14 |
| - pygame.transform.scale2x(pygame.image.load(os.path.join("images", "bird2.png"))), |
15 |
| - pygame.transform.scale2x(pygame.image.load(os.path.join("images", "bird3.png"))) |
| 13 | + pygame.transform.scale2x(pygame.image.load( |
| 14 | + os.path.join("images", "bird1.png"))), |
| 15 | + pygame.transform.scale2x(pygame.image.load( |
| 16 | + os.path.join("images", "bird2.png"))), |
| 17 | + pygame.transform.scale2x(pygame.image.load( |
| 18 | + os.path.join("images", "bird3.png"))) |
16 | 19 | ]
|
17 | 20 |
|
18 | 21 | # Pipe image
|
19 |
| -PIPE_IMAGE = pygame.transform.scale2x(pygame.image.load(os.path.join("images", "pipe.png"))) |
| 22 | +PIPE_IMAGE = pygame.transform.scale2x( |
| 23 | + pygame.image.load(os.path.join("images", "pipe.png"))) |
20 | 24 |
|
21 | 25 | # Base image
|
22 |
| -BASE_IMAGE = pygame.transform.scale2x(pygame.image.load(os.path.join("images", "base.png"))) |
| 26 | +BASE_IMAGE = pygame.transform.scale2x( |
| 27 | + pygame.image.load(os.path.join("images", "base.png"))) |
23 | 28 |
|
24 | 29 | # Background image
|
25 |
| -BG_IMAGE = pygame.transform.scale2x(pygame.image.load(os.path.join("images", "bg.png"))) |
| 30 | +BG_IMAGE = pygame.transform.scale2x( |
| 31 | + pygame.image.load(os.path.join("images", "bg.png"))) |
26 | 32 |
|
27 | 33 |
|
28 | 34 | class Bird:
|
@@ -85,7 +91,8 @@ def draw(self, win):
|
85 | 91 | self.image_count = self.ANIMATION_TIME * 2
|
86 | 92 |
|
87 | 93 | rotated_image = pygame.transform.rotate(self.image, self.tilt)
|
88 |
| - new_rect = rotated_image.get_rect(center=self.image.get_rect(topleft=(self.x, self.y)).center) |
| 94 | + new_rect = rotated_image.get_rect( |
| 95 | + center=self.image.get_rect(topleft=(self.x, self.y)).center) |
89 | 96 | win.blit(rotated_image, new_rect.topleft)
|
90 | 97 |
|
91 | 98 | def get_mask(self):
|
@@ -215,7 +222,8 @@ def main(genomes, config):
|
215 | 222 | bird.move()
|
216 | 223 | ge[x].fitness += 0.1
|
217 | 224 |
|
218 |
| - output = nets[x].activate((bird.y, abs(bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom))) |
| 225 | + output = nets[x].activate((bird.y, abs( |
| 226 | + bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom))) |
219 | 227 |
|
220 | 228 | if output[0] > 0.5:
|
221 | 229 | bird.jump()
|
@@ -279,4 +287,3 @@ def run_neat():
|
279 | 287 | pygame.init()
|
280 | 288 | STAT_FONT = pygame.font.SysFont("comicsans", 50)
|
281 | 289 | run_neat()
|
282 |
| - |
|
0 commit comments