-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.py
168 lines (132 loc) · 5.02 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import pygame
import os
import math
import sys
import neat
SCREEN_WIDTH = 1244
SCREEN_HEIGHT = 1016
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
TRACK = pygame.image.load(os.path.join("Assets", "track.png"))
class Car(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.original_image = pygame.image.load(os.path.join("Assets", "car.png"))
self.image = self.original_image
self.rect = self.image.get_rect(center=(490, 820))
self.vel_vector = pygame.math.Vector2(0.8, 0)
self.angle = 0
self.rotation_vel = 5
self.direction = 0
self.alive = True
self.radars = []
def update(self):
self.radars.clear()
self.drive()
self.rotate()
for radar_angle in (-60, -30, 0, 30, 60):
self.radar(radar_angle)
self.collision()
self.data()
def drive(self):
self.rect.center += self.vel_vector * 6
def collision(self):
length = 40
collision_point_right = [int(self.rect.center[0] + math.cos(math.radians(self.angle + 18)) * length),
int(self.rect.center[1] - math.sin(math.radians(self.angle + 18)) * length)]
collision_point_left = [int(self.rect.center[0] + math.cos(math.radians(self.angle - 18)) * length),
int(self.rect.center[1] - math.sin(math.radians(self.angle - 18)) * length)]
# Die on Collision
if SCREEN.get_at(collision_point_right) == pygame.Color(2, 105, 31, 255) \
or SCREEN.get_at(collision_point_left) == pygame.Color(2, 105, 31, 255):
self.alive = False
# Draw Collision Points
pygame.draw.circle(SCREEN, (0, 255, 255, 0), collision_point_right, 4)
pygame.draw.circle(SCREEN, (0, 255, 255, 0), collision_point_left, 4)
def rotate(self):
if self.direction == 1:
self.angle -= self.rotation_vel
self.vel_vector.rotate_ip(self.rotation_vel)
if self.direction == -1:
self.angle += self.rotation_vel
self.vel_vector.rotate_ip(-self.rotation_vel)
self.image = pygame.transform.rotozoom(self.original_image, self.angle, 0.1)
self.rect = self.image.get_rect(center=self.rect.center)
def radar(self, radar_angle):
length = 0
x = int(self.rect.center[0])
y = int(self.rect.center[1])
while not SCREEN.get_at((x, y)) == pygame.Color(2, 105, 31, 255) and length < 200:
length += 1
x = int(self.rect.center[0] + math.cos(math.radians(self.angle + radar_angle)) * length)
y = int(self.rect.center[1] - math.sin(math.radians(self.angle + radar_angle)) * length)
# Draw Radar
pygame.draw.line(SCREEN, (255, 255, 255, 255), self.rect.center, (x, y), 1)
pygame.draw.circle(SCREEN, (0, 255, 0, 0), (x, y), 3)
dist = int(math.sqrt(math.pow(self.rect.center[0] - x, 2)
+ math.pow(self.rect.center[1] - y, 2)))
self.radars.append([radar_angle, dist])
def data(self):
input = [0, 0, 0, 0, 0]
for i, radar in enumerate(self.radars):
input[i] = int(radar[1])
return input
def remove(index):
cars.pop(index)
ge.pop(index)
nets.pop(index)
def eval_genomes(genomes, config):
global cars, ge, nets
cars = []
ge = []
nets = []
for genome_id, genome in genomes:
cars.append(pygame.sprite.GroupSingle(Car()))
ge.append(genome)
net = neat.nn.FeedForwardNetwork.create(genome, config)
nets.append(net)
genome.fitness = 0
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
SCREEN.blit(TRACK, (0, 0))
if len(cars) == 0:
break
for i, car in enumerate(cars):
ge[i].fitness += 1
if not car.sprite.alive:
remove(i)
for i, car in enumerate(cars):
output = nets[i].activate(car.sprite.data())
if output[0] > 0.7:
car.sprite.direction = 1
if output[1] > 0.7:
car.sprite.direction = -1
if output[0] <= 0.7 and output[1] <= 0.7:
car.sprite.direction = 0
# Update
for car in cars:
car.draw(SCREEN)
car.update()
pygame.display.update()
# Setup NEAT Neural Network
def run(config_path):
global pop
config = neat.config.Config(
neat.DefaultGenome,
neat.DefaultReproduction,
neat.DefaultSpeciesSet,
neat.DefaultStagnation,
config_path
)
pop = neat.Population(config)
pop.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
pop.add_reporter(stats)
pop.run(eval_genomes, 50)
if __name__ == '__main__':
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'config.txt')
run(config_path)