Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (76 loc) · 2.59 KB
/
Copy pathmain.py
File metadata and controls
95 lines (76 loc) · 2.59 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
import pygame
import random
from utils import decide_flap, next_generation
from model import Bird
pygame.init()
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
running = True
font = pygame.font.SysFont(None, 24) # add a tiny HUD
GROUND_LEVEL = 300
GRAVITY = 0.5
JUMP_VELOCITY = -8
POP_SIZE = 30
# pipes
pipe_width = 60
pipe_gap = 150
pipe_x = WIDTH
pipe_height = random.randint(100, 400)
pipe_speed = 5
# population
population = [[random.uniform(-1, 1) for _ in range(21)] for _ in range(POP_SIZE)]
def check_collision(bird, pipes):
bird_rect = pygame.Rect(bird.x - bird.radius, bird.y - bird.radius, bird.radius*2, bird.radius*2)
for pipe in pipes:
if bird_rect.colliderect(pipe):
return True
return False
for gen in range(100):
running = True
pipe_x = WIDTH
pipe_height = random.randint(100, 400)
birds = [Bird(genome) for genome in population]
fitnesses = [0] * POP_SIZE
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
raise SystemExit
screen.fill((135, 206, 235)) # sky blue
top_pipe = pygame.Rect(pipe_x, 0, pipe_width, pipe_height)
# fix bottom pipe height
bottom_pipe = pygame.Rect(pipe_x, pipe_height + pipe_gap, pipe_width, HEIGHT - (pipe_height + pipe_gap))
pipes = (top_pipe, bottom_pipe)
# pipe movement
pipe_x -= pipe_speed
if pipe_x < -pipe_width:
pipe_x = WIDTH
pipe_height = random.randint(100, 400)
# draw pipes
pygame.draw.rect(screen, (0, 200, 0), top_pipe)
pygame.draw.rect(screen, (0, 200, 0), bottom_pipe)
# HUD
gen_surf = font.render(f"Gen: {gen+1}", True, (0,0,0))
screen.blit(gen_surf, (10, 10))
all_dead = True
for i, bird in enumerate(birds):
if not bird.dead:
all_dead = False
bird.update(GRAVITY)
gap_y = pipe_height + pipe_gap / 2
if decide_flap(bird.genome, bird.y, gap_y):
bird.flap()
if check_collision(bird, pipes):
bird.dead = True
else:
fitnesses[i] += 1
bird.draw(screen)
if all_dead:
running = False
pygame.display.flip()
clock.tick(60)
population = next_generation(population, fitnesses)
print(f"Next gen spawned: {len(population)} genomes")
pygame.quit()