-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
153 lines (124 loc) · 4.03 KB
/
main.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
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
try:
import pygame
import os
from configs import *
import colors
from ghost import Ghost
from events import *
from copter import Copter, COPTER_SPRITE
import neat
import math
except ImportError:
print("Please ....fulfil requirements")
pygame.init()
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("The Copter")
BACKGROUND_IMAGE = pygame.transform.scale(
pygame.image.load('Assets/bg.png'), (WIDTH, HEIGHT))
def render_text(text):
DISPLAY_TEXT = pygame.font.SysFont(
'comicsans', 35).render(str(text), True, colors.RED)
padding = 15
WIN.blit(DISPLAY_TEXT, (WIDTH - DISPLAY_TEXT.get_width() - padding, padding))
BG_movement = 0
COUNTER = 0
GEN = 0
def drawBG():
global BG_movement
WIN.blit(BACKGROUND_IMAGE, (BG_movement, 0))
WIN.blit(BACKGROUND_IMAGE, (BG_movement + WIDTH, 0))
BG_movement -= BACKGROUND_SPEED
if BG_movement == -WIDTH:
BG_movement = 0
def draw_window(ghosts, copters, score):
drawBG()
score_text = pygame.font.SysFont(
'comicsans', 35).render(str(score), True, colors.RED)
WIN.blit(score_text, (WIDTH - score_text.get_width() - 15, 15))
gen_text = pygame.font.SysFont(
'comicsans', 35).render("Gen : "+str(GEN), True, colors.RED)
WIN.blit(gen_text, (10, 10))
for copter in copters:
copter.draw(WIN)
for ghost in ghosts:
ghost.draw(WIN)
pygame.display.update()
# TODO: make feed forward netowrk map on right side
# TODO: make readme
#
def main(genomes, config):
global GEN
GEN += 1
nets = []
ge = []
copters = []
for _, g in genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
nets.append(net)
copters.append(Copter())
g.fitness = 0
ge.append(g)
clock = pygame.time.Clock()
run = True
ghosts = []
score = 0
pygame.time.set_timer(SPAWN_EVENT, SPAWN_TIME)
i = 0
pressed = {}
global COUNTER
while run:
clock.tick(FPS)
COUNTER += 1
if len(copters) == 0:
run = False
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == SPAWN_EVENT:
ghosts.append(Ghost())
for a, ghost in enumerate(ghosts):
ghost.move()
if ghost.rect.x < 0 - GHOST_WIDTH:
ghosts.remove(ghost)
score += 1
for g in ge:
g.fitness += 3
continue
for x, copter in enumerate(copters):
if copter.collision(ghost):
if len(ghosts) != 0:
ghosts.pop()
ge[x].fitness -= 1
copters.pop(x)
ge.pop(x)
nets.pop(x)
if copter.collison_with_boundary():
ge[x].fitness -= 2
copters.pop(x)
ge.pop(x)
nets.pop(x)
# if COUNTER % 100 == 0:
# COUNTER = 0
for x, copter in enumerate(copters):
if ghost is not None:
output = nets[x].activate((copter.rect.y, abs(
copter.rect.x - ghost.rect.x), abs(copter.rect.y - ghost.rect.y)))
copter.update(output)
if score > 60:
break
draw_window(ghosts, copters, score)
def run(config_file):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file)
p = neat.Population(config)
# for what is happening
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
winner = p.run(main, 50)
if __name__ == '__main__':
local = os.path.dirname(__file__)
config_file = os.path.join(local, "config-feedforward.txt")
run(config_file)