-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.py
166 lines (134 loc) · 5.19 KB
/
car.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
from map import *
carImage = pygame.image.load(CAR_FILE)
carImageResized = pygame.transform.scale(carImage, (CAR_WIDTH, CAR_HEIGHT))
def rot_center(image, rect, angle):
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image,rot_rect
class Car:
def __init__(self):
self.posX = 1160
self.posY = WINDOW_SIZE_H - 100
self.speed = .5
self.angle = 90
self.nnet = NNet(3, 2, 2)
self.clock = pygame.time.Clock()
self.fitness = 0
self.right = False
self.left = False
self.isAccelerating = True
self.carDrawed = carImageResized
self.frontDistanceX = self.posX
self.frontDistanceY = self.posY
self.frontTotalDistance = 0
self.coordenatesLineLeft = [self.posX, self.posY]
self.coordenatesLineCenter = [self.posX, self.posY]
self.coordenatesLineRight = [self.posX, self.posY]
self.linesColor = LINES_COLOR
self.linesThickness = 2
self.longitudeLineLeft = 0
self.longitudeLineCenter = 0
self.longitudeLineRight = 0
self.isGrowingLineLeft = True
self.isGrowingLineCenter = True
self.isGrowingLineRight = True
self.map = None
@staticmethod
def getAngleInRadians(angle):
angleInRadians = angle * math.pi / 180
return angleInRadians
@staticmethod
def getAngle(angle):
howManyThreeSixtiesAre = angle // 360
angleBetweeenZeroAndThreeSixty = angle - howManyThreeSixtiesAre * 360
howManyNinetiesAre = angleBetweeenZeroAndThreeSixty // 90
conjugatedAngle = 0
if howManyNinetiesAre == 0:
conjugatedAngle = angleBetweeenZeroAndThreeSixty - howManyNinetiesAre * 90
elif howManyNinetiesAre == 1:
conjugatedAngle = 180 - angleBetweeenZeroAndThreeSixty
elif howManyNinetiesAre == 2:
conjugatedAngle = angleBetweeenZeroAndThreeSixty - 180
elif howManyNinetiesAre == 3:
conjugatedAngle = 360 - angleBetweeenZeroAndThreeSixty
section = howManyNinetiesAre + 1
return [conjugatedAngle, section]
def reset(self):
self.nnet = NNet(3, 2, 2)
if len(weightsOfCars) == GENERATION_SIZE:
self.nnet.weightsInputHidden = weightsOfCars[self.map.numberOfCar - 1][0][0]
self.nnet.weightsHiddenOutput = weightsOfCars[self.map.numberOfCar - 1][1][0]
self.posX = 1160
self.posY = WINDOW_SIZE_H - 100
self.angle = 90
self.fitness = 0
self.right = False
self.left = False
self.carDrawed = carImageResized
self.frontDistanceX = self.posX
self.frontDistanceY = self.posY
self.frontTotalDistance = 0
self.coordenatesLineLeft = [self.posX, self.posY]
self.coordenatesLineCenter = [self.posX, self.posY]
self.coordenatesLineRight = [self.posX, self.posY]
self.longitudeLineLeft = 0
self.longitudeLineCenter = 0
self.longitudeLineRight = 0
self.isGrowingLineLeft = True
self.isGrowingLineCenter = True
self.isGrowingLineRight = True
def draw(self, window, mapDraw):
self.fitness += self.clock.tick(FPS)
self.map = mapDraw
# Change direction
results = self.nnet.getOutputs([
self.longitudeLineLeft,
self.longitudeLineCenter,
self.longitudeLineRight
])
if results[0] >= .5:
self.left = True
else:
self.left = False
if results[1] >= .5:
self.right = True
else:
self.right = False
self.accelerate()
if self.left:
self.angle += .75
if self.right:
self.angle -= .75
oldRect = self.carDrawed.get_rect(center=(self.posX, self.posY))
shipImg, newRect = rot_center(self.carDrawed, oldRect, self.angle)
carRect = window.blit(shipImg, newRect)
return carRect
def initLines(self):
self.isGrowingLineLeft = True
self.isGrowingLineCenter = True
self.isGrowingLineRight = True
self.coordenatesLineLeft = [self.posX, self.posY]
self.coordenatesLineCenter = [self.posX, self.posY]
self.coordenatesLineRight = [self.posX, self.posY]
self.longitudeLineLeft = 0
self.longitudeLineCenter = 0
self.longitudeLineRight = 0
def accelerate(self):
if self.isAccelerating:
angleInDegrees = self.getAngle(self.angle)
xComponent = math.cos(self.getAngleInRadians(angleInDegrees[0])) * self.speed
yComponent = math.sin(self.getAngleInRadians(angleInDegrees[0])) * self.speed
if angleInDegrees[1] == 1:
yComponent *= -1
xComponent *= 1
elif angleInDegrees[1] == 2:
yComponent *= -1
xComponent *= -1
elif angleInDegrees[1] == 3:
yComponent *= 1
xComponent *= -1
elif angleInDegrees[1] == 4:
yComponent *= 1
xComponent *= 1
self.posY += yComponent
self.posX += xComponent