-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameAI.py
366 lines (295 loc) · 8.4 KB
/
gameAI.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import pygame
import neat
import random
import os
import pygame.freetype
from collections import deque
WIDTH = 500
HEIGHT = 600
FPS = 30
ACCELERATION = 1
BACKGROUND = (25,25,25)
OBSTCOLOR = (192,192,192)
TEXTCOLOR = (255,255,255)
ROTATION = 15
THICKNESS = 20
SPEED = 5
GAP = 100
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game AI")
generation = 0
class copter:
"""docstring for copter"""
def __init__(self, x, y):
''' initialize the copter object and
sets positional param '''
self.x, self.y = x, y
self.velocity = 0.5
self.tick_cnt = 0
self.tilt = 0
self.image = pygame.image.load("img/copter.png")
def draw(self):
''' draw the copter on window '''
rotImage = pygame.transform.rotate(self.image, self.tilt)
win.blit(rotImage, (self.x, self.y))
def move(self):
''' move the copter '''
self.tick_cnt += 1
# s = v*t + 1/2*a*t^2
displ = (self.velocity * self.tick_cnt +
(1/2) * (ACCELERATION) * (self.tick_cnt) ** 2)
# stop accelerating after certain point, (kinda terminal velocity)
if displ > 15:
displ = 15
# add tilt to the copter
# tilt up if going up
if displ < 0:
self.tilt = ROTATION
# else just point down
else:
self.tilt = -ROTATION
# update the position of the copter
self.y += displ
def jump(self):
''' make the copter jump'''
self.velocity = -4
self.tick_cnt = 0
def rect(self):
'''returns the rect for the copter'''
# return pygame.mask.from_surface(self.image)
cRect = self.image.get_rect()
cRect.topleft = (self.x, self.y)
return cRect
class obstacle(object):
"""docstring for obstacle"""
def __init__(self,x, y):
self.x, self.y = x, y
self.width = random.randrange(100, 500)
self.image = pygame.Rect(self.x, self.y, self.width, THICKNESS)
self.varPassedEnd = False
self.varPassedCopter = False
self.varPassedScreen = False
def draw(self):
'''draws the obstacle rectangle
at x,y with defined thickness and width'''
self.image = pygame.Rect(self.x, self.y, self.width, THICKNESS)
pygame.draw.rect(win, OBSTCOLOR, self.image)
def move(self):
'''move obstacle left by speed'''
self.x -= SPEED
def collision(self, copter):
'''returns True if obstacle and
copter are overlapping'''
return copter.rect().colliderect(self.image)
# print(copterMask)
# # obstacleMask = pygame.mask.from_surface(self.image)
# pygame.sprite.collide_mask
# # rect to surface
# pygame.Surface((a.w, a.h))
def passedEnd(self):
'''fn to tell if end of the screen has been passed'''
if self.varPassedEnd:
return True
if (self.x + self.width) < WIDTH:
self.varPassedEnd = True
return True
return False
def passedCopter(self):
'''returns True if obstaces has passed copter or width/4
'''
if self.varPassedCopter:
return True
if (self.x + self.width) < (WIDTH / 4):
self.varPassedCopter = True
return True
return False
def passedScreen(self):
'''if copter has passed the whole screen
'''
if self.varPassedScreen:
return True
if (self.x + self.width) < 0:
self.varPassedScreen = True
return True
return False
def drawScreen(copters, obstacles, score):
'''draws copters and obstacles and
add labels for scroe and generation
'''
global generation
# clear screen
win.fill(BACKGROUND)
# draw all obstacles in the deque
for obst in obstacles:
obst.draw()
# draw every copter
for heli in copters:
heli.draw()
font = pygame.freetype.SysFont(None, 20)
# label for score
font.render_to(win, (10, 10), 'Score: ' + str(score), TEXTCOLOR)
# label for generation
font.render_to(win, (10, 40), 'Gen: ' + str(generation), TEXTCOLOR)
#label for flying ccopters
font.render_to(win, (10, 70), 'Flying: ' + str(len(copters)), TEXTCOLOR)
pygame.display.update()
def eval_genomes(genomes, config):
''' runs for every generation of copters,
run the copter game for every copter
from the population simultaneously,
taking their actions from neural network
and updating their fitness according to thier
performance
'''
# vriable definations to be used in fn
global generation
generation += 1
score = 0
run = True
fpsClock = pygame.time.Clock()
obstacles = deque()
obstacles.append(obstacle(500, 250))
nnets = []
copters = []
gl = []
# create copter, neuralnet for every genome in generation
for genomeId, genome in genomes:
# set initial fitness for copters to 0
genome.fitness = 0
nnet = neat.nn.FeedForwardNetwork.create(genome, config)
nnets.append(nnet)
copters.append(copter(WIDTH / 4, HEIGHT / 2))
gl.append(genome)
# run till no copter is left flying
while len(copters) > 0 and run:
fpsClock.tick(FPS)
# to stop execution
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
break
# which obstacle the copter need to make decision on
obstIndex = 0
for obstIndex, obst in enumerate(obstacles):
if not obst.passedCopter():
break
# move copter and predict if copter need to jump or not
for i, heli in enumerate(copters):
# increase fitness for copter for every frame survived
gl[i].fitness += 0.1
heli.move()
# inputs y position of copter and vertical distance b/w copter and obstacle
predict = nnets[i].activate((heli.y, (heli.y - obstacles[obstIndex].y)))
if predict[0] > 0.5:
heli.jump()
# advance the game
addObst = False
removeObst = False
for obst in obstacles:
obst.move()
# remove copter if collidede with obstacle
for i, heli in enumerate(copters):
if obst.collision(heli):
gl[i].fitness -= 1
gl.pop(i)
nnets.pop(i)
copters.pop(i)
# add obstacle if needed
if not obst.varPassedEnd and obst.passedEnd():
addObst = True
# ineffiecient implementation for getting gap
cordY = obst.y
rdm = cordY
while rdm > (cordY-GAP) and rdm < (cordY+GAP):
rdm = random.randrange(100, 500)
# if copter passed this obstacle
if not obst.varPassedCopter and obst.passedCopter():
score += 1
for genome in gl:
genome.fitness += 10
# remove obstacle that passed the screen
if not obst.varPassedScreen and obst.passedScreen():
removeObst = True
# add obstacle
if addObst:
obstacles.append(obstacle(500, rdm))
# remove obstacle
if removeObst:
obstacles.popleft()
# remove copters that fallen down
# or gone to space
for i, heli in enumerate(copters):
if heli.y >= HEIGHT:
nnets.pop(i)
copters.pop(i)
gl.pop(i)
elif heli.y < 0:
nnets.pop(i)
copters.pop(i)
gl.pop(i)
# draws copters, obstacles and add score on screen
drawScreen(copters, obstacles, score)
def run(config_file):
# Load configuration.
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_file)
# Create the population, which is the top-level object for a NEAT run.
p = neat.Population(config)
# Add a stdout reporter to show progress in the terminal.
p.add_reporter(neat.StdOutReporter(True))
# Run for up to 50 generations.
winner = p.run(eval_genomes, 50)
if __name__ == '__main__':
# load the config file fot neat
run(os.path.join(os.path.dirname(__file__), 'neatConfig.txt'))
################################################
'''
# code used for testing the copter game
copter1 = copter(WIDTH / 4, HEIGHT / 2)
obstrn = deque()
obstrn.append(obstacle(500, 250))
run = True
while run:
# to stop execution press 'q' on keyboard
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
run = False
elif event.key == pygame.K_j:
copter1.jump()
# clear screen
win.fill(BACKGROUND)
copter1.move()
# if copter hit ground
if copter1.y >= HEIGHT:
run = False
for obst in obstrn:
obst.move()
obst.draw()
# stop the game if copter collided with obstacle
if obst.collision(copter1):
run = False
# create new obstacle, to keep them adjacent
if obstrn[-1].passedEnd():
# code to get obstcles with a certain gap b/w them
# cordY = obstrn[-1].y
# rdm = random.randrange(100, cordY)
# if rdm > (cordY - GAP):
# rdm += (2 * GAP)
# ineffiecient implementation for getting gap
cordY = obstrn[-1].y
rdm = cordY
while rdm > (cordY-GAP) and rdm < (cordY+GAP):
rdm = random.randrange(100, 500)
obstrn.append(obstacle(500, rdm))
copter1.draw()
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()
quit()
'''
################################################