-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart-dots.py
72 lines (57 loc) · 1.73 KB
/
smart-dots.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
from Goal import *
from Population import *
from Obstacle import *
goal = Goal()
all_sprites.add(goal)
population = Population()
def draw_text(surf, text, size, x, y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, BLACK)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
def handle_obstacles():
global lvl_up
global obs_index
if lvl_up and obs_index < len(obs_topleft_list):
Obstacle(obs_topleft_list[obs_index])
lvl_up = False
obs_index += 1
def evolve():
population.compute_total_fitness()
if population.generation % 2 == 1:
population.boost_fitness()
else:
population.natural_selection()
def update():
global lvl_up
if not pop_sprites.sprites(): # if all dead:
handle_obstacles()
evolve()
return
winners = pygame.sprite.spritecollide(goal, pop_sprites, False)
for winner in winners:
winner.winner()
lvl_up = True
sprite_map = pygame.sprite.groupcollide(pop_sprites, obs_sprites, False, False)
for loser in sprite_map:
loser.die()
pop_sprites.update()
def events():
global simulation_over
for event in pygame.event.get():
if event.type == pygame.QUIT:
simulation_over = True
def draw():
clock.tick(frame_per_second)
game_display.fill(WHITE)
# obs_sprites.draw(game_display)
all_sprites.draw(game_display)
draw_text(game_display, "GENERATION: " + str(population.generation) +
" SCORE: " + str(population.total_fitness), 30,
resolution[0] / 2, 10)
pygame.display.flip()
while not simulation_over:
draw()
events()
update()