-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarShip.py
510 lines (447 loc) · 20.5 KB
/
StarShip.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import pygame
from SpaceShipSprite import SpaceShipSprite
import sys
from Generators import ObstacleGenerator
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE, K_UP,K_DOWN, K_LEFT, K_RIGHT, K_SPACE, K_a, K_d,K_s,K_p
from Obstacle import Obstacle
import numpy as np
import copy
import pandas as pd
import cv2
import time
class StarShipGame:
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
liveObstacles = []
deadObstacles = 0
liveProjectiles =[]
def __init__(self,graphics,screen_size =(400, 400)):
pygame.font.init()
pygame.init()
self.action=0
StarShipGame.screen = pygame.display.set_mode(screen_size)
self.done = False
self.playing = False
self.reward = 0
self.graphics = graphics
self.score =0
self.key =0
#background
self.font = pygame.font.SysFont('Arial', 25, True, False)
self.spaceShipSprite = SpaceShipSprite(StarShipGame.screen,r'Assets\imgs\triangleShip.png',startPosition=((screen_size[0]/2)-60,screen_size[1]/2))
self.obstacleGenerator = ObstacleGenerator(StarShipGame.screen,r'Assets\imgs\brick.png')
self.screen_size = screen_size
self.FPS =0
self.clock = pygame.time.Clock()
pygame.display.set_caption("StarShip")
self.obstacleGenerator.generate(0);
StarShipGame.liveObstacles = self.obstacleGenerator.liveObstacles
self.save=False
self.REM_STEP = 3
self.ROWS = 1
self.COLS = 62
self.image_memory = np.zeros((self.REM_STEP, self.ROWS, self.COLS))
self.state_size = (self.REM_STEP, self.ROWS, self.COLS)
self.state_shape = (1,self.REM_STEP,self.ROWS * self.COLS)
self.x =0
self.time_multipliyer= 1
self.timer1 = 0
self.counter = 0
self.action_space = 6
self.plot =False
def vectorize_func(self,m):
return m/255
def imshow(self, image, rem_step=0):
cv2.imshow("SpS", image[rem_step,...])
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
return
def render(self):
return (copy.deepcopy(pygame.surfarray.array3d(self.screen)))
def render_wrap(self):
temp = self.render()
temp=np.reshape(temp,(-1,3))
temp= pd.DataFrame(temp,columns=['r','g','b'])
conditions = [
(temp == 255)
]
temp2=pd.Series()
temp2['r'] = 0
temp2['g'] = 0
temp2['b'] = 0
temp.loc[(temp['r'] >1) & (temp['g']>1) & (temp['b']>1)] = [0,0,0]
temp = np.where(temp['r'] == 0 & temp['g']==3 & temp['b'] == 0 ,[22,33,44], temp.tolist())
temp = np.where(temp == [255,255,255] ,[0,0,0], temp).tolist()
return temp.values
def getPixelsOnScreen(self):
img= self.render()
img_rgb = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img_rgb_resized = cv2.resize(img_rgb, (self.COLS, self.ROWS), interpolation=cv2.INTER_CUBIC)
img_rgb_resized[img_rgb_resized > 5] = 255
img_rgb_resized = np.transpose(img_rgb_resized / 255)
self.image_memory = np.roll(self.image_memory, 1, axis = 0)
self.image_memory[0,:,:] = img_rgb_resized
self.imshow(self.image_memory,0)
return np.expand_dims(self.image_memory, axis=0)
def getEnvStateOnScreen(self):
obs_collection=[]
count = 1
agentX,agentY,agent_X_offset,agent_Y_offset, agent_CX_offset,agent_CY_offset,agent_w,agent_h= self.Obj_state(self.spaceShipSprite)
for obs in self.obstacleGenerator.liveObstacles :
x,y,x_offset,y_offset, Cx_offset,Cy_offset,w,h= self.Obj_state(obs)
label = "obs_" + str(count) + "_"
# obs_collection.append(obs.obs_ID)
obs_collection.append(x)
obs_collection.append(y)
obs_collection.append(Cx_offset)
obs_collection.append(Cy_offset)
# obs_collection.append(w)
# obs_collection.append(h)
count+=1
count=1
livep =[]
for proj in self.obstacleGenerator.liveProjectiles:
x,y,x_offset,y_offset, cx_offset,cy_offset,w,h= self.Obj_state(proj)
label = "live_projectile_" + str(count) + "_"
livep.append(x)
livep.append(y)
# livep[label+"X_offset"]=x_offset
# livep[label+"Y_offset"]=y_offset
# livep.append(x_offset//count)
# livep.append(y_offset//count)
# livep.append(w)
# livep.append(h)
count+=1
count=1
state = [
# (self.obstacleGenerator.deadObstacles),
(len(self.obstacleGenerator.liveProjectiles)),
(self.spaceShipSprite.firedAt//100000),
(0 if self.obstacleGenerator.p_out_of_bounds else 1),
(self.obstacleGenerator.hits),
(self.obstacleGenerator.fails),
(self.reward),
self.spaceShipSprite.currentAmmo,
(pygame.time.get_ticks()- self.timer1)//100000,
# (self.spaceShipSprite.health/self.spaceShipSprite.maxhp),
# "live_obstacles_num":len(self.obstacleGenerator.liveObstacles),
agentX,
agentY,
agent_CX_offset,
agent_CY_offset,
# "agent_X_offset":agent_X_offset,
# "agent_Y_offset":agent_Y_offset,
# agent_w,
# agent_h
]
for v in (obs_collection):
state.append(v)
livep_len = len(livep)
livep_len = (int)(livep_len/2)
# x = tf.placeholder("float", shape=None)
if livep_len < self.spaceShipSprite.maxProjectiles_on_screen:
for i in range(livep_len,self.spaceShipSprite.maxProjectiles_on_screen):
label = "live_projectile_placeHolder_" + str(i) + "_"
livep.append( agentX)
livep.append(agentY-(14/400))
# livep[label+"X_offset"]=0
# # livep[label+"Y_offset"]=0
# livep.append(agent_CY_offset//i)
# livep.append(agent_CY_offset//i)
# livep.append(-9999)
# livep.append(-9999)
for v in (livep):
state.append(v)
# state =[ *state.values()]
self.image_memory = np.roll(self.image_memory, 1, axis = 0)
self.image_memory[0,:,:] = state
#np.select(conditions, choices, default=0)
return np.expand_dims(self.image_memory, axis=0)
def getEnvStateOnScreen2(self):
obs_collection=[]
count = 0
agentX,agentY,agent_X_offset,agent_Y_offset, agent_CX_offset,agent_CY_offset,agent_w,agent_h= self.Obj_state(self.spaceShipSprite)
for obs in self.obstacleGenerator.liveObstacles :
x,y,x_offset,y_offset, Cx_offset,Cy_offset,w,h= self.Obj_state(obs)
label = "obs_" + str(count) + "_"
# obs_collection.append(obs.obs_ID/5)
obs_collection.append(x)
obs_collection.append(y)
# obs_collection[label+"CX"]=Cx_offset
# obs_collection[label +"CY"]=Cy_offset
obs_collection.append(w)
obs_collection.append(h)
count+=1
# count=0
# livep ={}
# for proj in self.obstacleGenerator.liveProjectiles:
# x,y,x_offset,y_offset, cx_offset,cy_offset,w,h= self.Obj_state(proj)
# label = "live_projectile_" + str(count) + "_"
# livep [label+"X"]=x
# livep[label +"Y"]=y
# # livep[label+"X_offset"]=x_offset
# # livep[label+"Y_offset"]=y_offset
# # livep[label+"CX_offset"]=cx_offset
# # livep[label+"CY_offset"]=cy_offset
# livep[label+"width"]=w
# livep[label+"height"]=h
# count+=1
count=0
state = [ (self.obstacleGenerator.deadObstacles),
(len(self.obstacleGenerator.liveProjectiles)),
(self.spaceShipSprite.firedAt/1000),
(self.obstacleGenerator.p_out_of_bounds),
(self.obstacleGenerator.hits),
(self.obstacleGenerator.fails),
(self.counter),
(self.score),
# "agent_X_offset":agent_X_offset,
# "agent_Y_offset":agent_Y_offset,
# "agent_CX_offset":agent_CX_offset,
# "agent_CY_offset":agent_CY_offset,
self.spaceShipSprite.currentAmmo,
(pygame.time.get_ticks()- self.timer1)/1000,
(self.spaceShipSprite.health),
self.spaceShipSprite.damage,
self.reward,
# "live_obstacles_num":len(self.obstacleGenerator.liveObstacles),
agentX,
agentY,
agent_w,
agent_h ]
for m, (k, v) in enumerate(obs_collection.items()):
state[k]=v
# livep_len = len(livep)
# livep_len = (int)(livep_len/4)
# if livep_len < self.spaceShipSprite.maxProjectiles_on_screen:
# for l in range(livep_len,self.spaceShipSprite.maxProjectiles_on_screen):
# label = "live_projectile_placeHolder_" + str(l) + "_"
# livep[label+"X"]=0
# livep[label +"Y"]=0
# # livep[label+"X_offset"]=0
# # livep[label+"Y_offset"]=0
# # livep[label+"CX_offset"]=0
# # livep[label+"CY_offset"]=0
# livep[label+"width"]=0
# livep[label+"height"]=0
# for t, (k, v) in enumerate(livep.items()):
# state[k]=v
state =[ *state.values()]
return state
def check_shipalive(self):
if self.spaceShipSprite.lives <1 and self.spaceShipSprite.health==0:
print("You died !")
self.done= True
return True
if ObstacleGenerator.fails>0:
self.done =True
return False
def checkHits(self):
i = self.spaceShipSprite
if any((( i.currentPos[0] + i.image_size[0] >= x.currentPos[0]) and (i.currentPos[1] >= x.currentPos[1] - i.image_size[1]) and (i.currentPos[0] <= x.currentPos[0] + x.image_size[0]) and (i.currentPos[1] <= x.currentPos[1] + x.image_size[1])) for x in StarShipGame.liveObstacles):
#if collision
y =next(x for x in StarShipGame.liveObstacles if (( i.currentPos[0] + i.image_size[0] >= x.currentPos[0]) and (i.currentPos[1] >= x.currentPos[1] - i.image_size[1]) and (i.currentPos[0] <= x.currentPos[0] + x.image_size[0]) and (i.currentPos[1] <= x.currentPos[1] + x.image_size[1])))
alive = (i.takeDamage(y))
y.dispose()
StarShipGame.liveObstacles.remove(y)
self.obstacleGenerator.liveObstacles =StarShipGame.liveObstacles
self.obstacleGenerator.updateList()
self.obstacleGenerator.generate(delay=0,num=1)
StarShipGame.liveObstacles =self.obstacleGenerator.liveObstacles
ObstacleGenerator.deadObstacles+=1
if alive :
self.reward-=1
else:
self.reward-=2
self.done = True
def draw_score(self,color ='white'):
self.x += self.reward
scoreboard = "<<<Score>>> " + "{:.2f}".format(self.x) + "/-hits : " + str(self.obstacleGenerator.hits) + "/fails : " + str(ObstacleGenerator.fails) + "/liveObs : " + str(len(self.obstacleGenerator.liveObstacles)) +"/Missiles live: "+ str(len(self.obstacleGenerator.liveProjectiles)) +"/key : " + str((self.key)) +"/Action : " + str((self.action))+ "/deadObs : " + str(self.obstacleGenerator.deadObstacles) + "/Ship-lives : " + str((self.spaceShipSprite.lives))+ "/Ship-health: " + str((self.spaceShipSprite.health))
ls = scoreboard.split("/");
xmov=0
if color == "black":
color = StarShipGame.BLACK
else :
color = StarShipGame.WHITE
# for i in ls:
# scoreboardf = self.font.render(i,True, pygame.Color(color))
# StarShipGame.screen.blit(scoreboardf, [50,100+xmov])
# xmov +=25
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Write some Text
font = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (50,30)
fontScale = 1
fontColor = (255,255,255)
lineType = 2
for i in ls:
cv2.putText(img,i,
bottomLeftCornerOfText,
font,
fontScale,
fontColor,
lineType)
bottomLeftCornerOfText = (bottomLeftCornerOfText[0],bottomLeftCornerOfText[1] + 50)
cv2.imshow('input',img)
def draw_all(self,xmov=.10):
#clear/fill screen
self.draw_score()
StarShipGame.screen.fill(pygame.Color(StarShipGame.BLACK))
# display image on screen
# screen.blit(image, [600, 100])
self.spaceShipSprite.draw()
self.obstacleGenerator.drawAll()
def Obj_state(self,obs):
aCenterX = (obs.currentPos[0])# + (obs.image_size[0]/2)
aCenterY =(obs.currentPos[1])# + (obs.image_size[1]/2)
formatdCX = aCenterX*(1/self.screen_size[0])
formatdCY =aCenterY*(1/self.screen_size[1])
offsetCX = 0.5 - formatdCX
offsetCY = 0.5 - formatdCY
w= obs.image_size[0]*(1/self.screen_size[0])
h= obs.image_size[1]*(1/self.screen_size[1])
return formatdCX,formatdCY, *self.getOffsets_formatted(obs), offsetCX,offsetCY,w,h
def getOffsets_formatted(self,obs):
aCenterX = (obs.offset[0]) + (obs.image_size[0]/2)
aCenterY =(obs.offset[1]) + (obs.image_size[1]/2)
offsetX = aCenterX*(1/self.screen_size[0])
offsetY =aCenterY*(1/self.screen_size[1])
return offsetX, offsetY
def game_loop(self):
self.obstacleGenerator.updateAll()
StarShipGame.liveProjectiles=self.spaceShipSprite.liveProjectiles
ObstacleGenerator.liveProjectiles=StarShipGame.liveProjectiles
hit = self.obstacleGenerator.checkHits()
if hit:
self.reward+=1
hit=False
if ObstacleGenerator.p_out_of_bounds:
self.reward-=.5
ObstacleGenerator.p_out_of_bounds=0
self.checkHits()
if (ObstacleGenerator.fails) > 0 :
self.reward -= (ObstacleGenerator.fails) - (self.time_multipliyer+1.4)
self.done=True
for event in pygame.event.get():
if event.type == QUIT: # terminates the game when game window is closed
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE: # terminates the game when esc is pressed
pygame.quit()
sys.exit()
if event.key == K_s:
self.save=True
if event.key == K_p:
self.plot=True
def play(self):
self.clock.tick(self.FPS)
self.playing = True
while True: #begin gameloop
# handle mouse and keyboard events
print(self.reward)
self.reward =0
self.game_loop()
keys = pygame.key.get_pressed()
self.spaceShipSprite.move(keys)
#termination condition
if self.check_shipalive() or ObstacleGenerator.fails>0 or Obstacle.fails > 0:
ObstacleGenerator.reset()
self.spaceShipSprite.liveProjectiles=[]
self.done=True
self.reward += int(self.clock.get_time()/1000)
pygame.quit()
sys.exit()
if self.graphics:
self.draw_all()
pygame.display.update()
pygame.quit()
def reset(self):
pygame.init()
self.spaceShipSprite = SpaceShipSprite(StarShipGame.screen,r'Assets\imgs\triangleShip.png',startPosition=(self.screen_size[0]/2,self.screen_size[1]/2))
SpaceShipSprite.liveProjectiles=[]
ObstacleGenerator.obs_ID=0
self.obstacleGenerator = ObstacleGenerator(StarShipGame.screen,r'Assets\imgs\brick.png')
StarShipGame.liveObstacles = []
StarShipGame.deadObstacles = 0
StarShipGame.liveProjectiles =[]
ObstacleGenerator.fails=0
Obstacle.fails=0
self.score =0
self.counter =0
self.hit=0
self.fails=0
self.x =0
self.action=0
self.key=0
self.score =0
self.image_memory = np.zeros((self.REM_STEP, self.ROWS, self.COLS))
self.state_size = (self.REM_STEP, self.COLS)
self.reward= 0
self.time_multipliyer =1
StarShipGame.fails =0
self.done=False
self.timer1 = pygame.time.get_ticks()
self.clock = pygame.time.Clock()
self.obstacleGenerator.generate(0);
StarShipGame.liveObstacles = self.obstacleGenerator.liveObstacles
for i in range(self.REM_STEP):
state = self.getEnvStateOnScreen()
# for i in obs_collection:
# state.append(i)
state=np.reshape(state,(self.state_size))
return state
def step(self,action):
self.clock.tick(self.FPS)
self.counter += 1
self.done=False
self.reward =0
key = "nothing"
#key pressed
#left
if action == 0:
self.reward -= .01
key="left"
#right
elif action == 1:
self.reward -= .01
key="right"
#Up
elif action == 2:
self.reward -= .01
key="up"
#Down
elif action == 3:
self.reward -= .01
key="down"
#Space-Shoot
elif action == 4:
self.reward -=.02
key="space"
elif action == 5:
self.reward -= .01
self.key=key
self.action=action
action_status = self.spaceShipSprite.move(key) #0 for successfull action, -1 for failure aka shooting without ammo
if action_status == -1:
self.reward -= .03
self.game_loop()
obs_collection=[]
n=len(self.obstacleGenerator.liveObstacles)
if n < 5:
x=5-n
self.obstacleGenerator.generate(delay=0,num=x)
StarShipGame.liveObstacles =self.obstacleGenerator.liveObstacles
state = self.getEnvStateOnScreen()
state=np.reshape(state,(self.state_size))
if self.done:
return self.reward, state ,self.done
if self.graphics:
self.draw_all() # generates new frame
pygame.display.update()
return self.reward, state ,self.done
if __name__ == '__main__':
env = StarShipGame(graphics=True)
env.FPS =99999
env.play()