-
Notifications
You must be signed in to change notification settings - Fork 2
/
gameAI.py
394 lines (329 loc) · 13.7 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
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
import math
import numpy as np
import pygame
import random
import time
from constants import *
import constants
from datetime import date, datetime
all_sprites = pygame.sprite.Group()
bullets = pygame.sprite.Group()
pills = pygame.sprite.Group()
def getaction(act):
if act == [1, 0, 0, 0]:
return "move_left"
if act == [0, 1, 0, 0]:
return "move_right"
if act == [0, 0, 1, 0]:
return "fire"
if act == [0, 0, 0, 1]:
return "idle"
class Game_Score:
def __init__(self):
self.asteroids_hit = 1
self.bullets_used = 1
self.score = 0
self.accuracy = 1
def asteroid_hit(self):
"""Increments the asteroids hit at that instance, along with score and accuracy"""
self.asteroids_hit += 1
self.update_score()
self.update_accuracy()
def bullet_fired(self):
"""Increments the bullets fired by the ship"""
self.bullets_used += 1
self.update_score()
self.update_accuracy()
def update_score(self):
self.score = (self.asteroids_hit * 100) - (self.bullets_used * 2)
def update_accuracy(self):
"""Updates the accuracy of the game"""
if self.bullets_used > 0:
self.accuracy = self.asteroids_hit / self.bullets_used
else:
self.accuracy = 0
def get_accuracy(self):
"""Returns the accuracy of the player at that moment"""
self.update_accuracy()
return round(self.accuracy, 2)
def get_score(self):
"""Returns the score of the player at that moment"""
self.update_score()
return int(self.score)
class Fuel_Pill(pygame.sprite.Sprite):
def __init__(self, position, direction):
super().__init__()
self.image = pygame.image.load("resources/bolt_gold.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (20, 30))
self.rect = self.image.get_rect(center=position)
self.position = pygame.Vector2(position)
self.direction = direction
self.distance = 0
self.type = "Fuel Pill"
def update(self):
self.position += self.direction * PILL_SPEED
self.distance += PILL_SPEED
self.rect.center = self.position
if self.distance > PILL_RANGE:
self.kill()
class Health_Pill(pygame.sprite.Sprite):
def __init__(self, position, direction):
super().__init__()
self.image = pygame.image.load("resources/pill_green.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (20, 20))
self.rect = self.image.get_rect(center=position)
self.position = pygame.Vector2(position)
self.direction = direction
self.distance = 0
self.type = "Health Pill"
def update(self):
self.position += self.direction * PILL_SPEED
self.distance += PILL_SPEED
self.rect.center = self.position
if self.distance > PILL_RANGE:
self.kill()
class Healthbar(pygame.sprite.Sprite):
def __init__(self, x, y, w, h, maxh, over, below):
super().__init__()
self.x = x
self.y = y
self.w = w
self.h = h
self.hp = maxh
self.max = maxh
self.over = over
self.below = below
def draw(self, screen):
font_score = pygame.font.SysFont('Bauhaus 93', 30)
pygame.draw.rect(screen, self.below, (self.x, self.y, self.w, self.h))
pygame.draw.rect(screen, self.over, (self.x, self.y, self.hp, self.h))
health_text = font_score.render('H', True, (255, 255, 255))
screen.blit(health_text, (5, 10))
fuel_text = font_score.render('F', True, (255, 255, 255))
screen.blit(fuel_text, (screen.get_width() - 118, 10))
# noinspection PyTypeChecker,PyGlobalUndefined
class Rocket(pygame.sprite.Sprite):
def __init__(self, rocket_img, screen):
pygame.sprite.Sprite.__init__(self)
self.original_image = rocket_img
self.image = self.original_image.copy()
self.rect = self.image.get_rect(center=(screen.get_width() // 2, screen.get_height() - 100))
self.angle = 90
self.dx = 0.2
self.position = pygame.Vector2(self.rect.center)
self.x = pygame.Vector2(0, 2)
self.deceleration = 0.95
def update(self):
"""Allows the player to control the movement of his spaceship on the x-axis"""
global action
if action == "move_left":
self.x.x -= self.dx + np.random.randint(1, 30)/100
if action == "move_right":
self.x.x += self.dx + np.random.randint(1, 30)/100
self.position += self.x
self.x *= self.deceleration
if self.position.x < 20:
self.position.x = 20
if self.position.x > constants.SCREEN_WIDTH - 20:
self.position.x = constants.SCREEN_WIDTH - 20
self.image = pygame.transform.rotate(self.original_image, self.angle)
self.rect = self.image.get_rect(center=self.position)
def shoot(self):
"""The player shoots a bullet from his controllable spaceship upwards"""
bullet_dir = pygame.Vector2(math.cos(math.radians(self.angle)), -math.sin(math.radians(self.angle)))
bullet = Bullet(self.position, bullet_dir)
all_sprites.add(bullet)
bullets.add(bullet)
class Bullet(pygame.sprite.Sprite):
def __init__(self, position, direction):
super().__init__()
self.image = pygame.image.load("resources/bullet.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (6, 6))
self.rect = self.image.get_rect(center=position)
self.position = pygame.Vector2(position)
self.direction = direction
self.distance = 0
def update(self):
self.position += self.direction * BULLET_SPEED
self.distance += BULLET_SPEED
self.rect.center = self.position
if self.distance > BULLET_RANGE:
self.kill()
# noinspection PyTypeChecker
class Asteroid(pygame.sprite.Sprite):
def __init__(self, speed, screen, ast_imgs):
super().__init__()
self.image = random.choice(ast_imgs)
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, screen.get_width())
self.rect.y = -50
self.direction = pygame.Vector2(random.uniform(-0.5, 0.5), 1).normalize()
self.position = pygame.Vector2(self.rect.topleft)
self.speed = pygame.Vector2(speed)
def update(self):
self.position.x += self.direction.x * self.speed.x
self.position.y += self.direction.y * self.speed.y
self.rect.center = self.position
if self.rect.top > constants.SCREEN_HEIGHT + 20 or self.rect.left < -20 or self.rect.right > constants.SCREEN_WIDTH + 20:
self.kill()
def shoot(self):
"""The asteroid drops a health/fuel pill which acts as a randomised reward"""
pill_dir = pygame.Vector2(math.cos(math.radians(90)), math.sin(math.radians(90)))
pill_type = random.randint(1, 100) % 2
if pill_type:
pill = Fuel_Pill(self.position, pill_dir)
else:
pill = Health_Pill(self.position, pill_dir)
all_sprites.add(pill)
pills.add(pill)
class GameAI:
def __init__(self):
pygame.init()
pygame.font.init()
self.bullet_cooldown = 0.55
self.gamedate = date.today()
self.gametime = 0
self.last_fired = 0
self.death_reason = ""
self.reward = 0
self.end = 0
self.player_accuracy = 0
self.asteroids_hit = 0
self.font_score = pygame.font.SysFont('Bauhaus 93', 30)
self.screen = pygame.display.set_mode((600, 600))
self.screen.fill(BLACK)
self.clock = pygame.time.Clock()
self.run = True
self.actions_list = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
self.rocket_img = pygame.image.load("resources/ship.png").convert()
self.rocket_img = pygame.transform.scale(self.rocket_img, (40, 40))
self.rocket_img.set_colorkey(BLACK)
self.rocket_img = pygame.transform.rotate(self.rocket_img, -90)
self.ast_img1 = pygame.image.load("resources/ast1.png")
self.ast_img1 = pygame.transform.scale(self.ast_img1, (40, 40))
self.ast_img1.set_colorkey(BLACK)
self.ast_img2 = pygame.image.load("resources/ast2.png")
self.ast_img2 = pygame.transform.scale(self.ast_img2, (35, 35))
self.ast_img2.set_colorkey(BLACK)
self.ast_img3 = pygame.image.load("resources/ast3.png")
self.ast_img3 = pygame.transform.scale(self.ast_img3, (40, 40))
self.ast_img3.set_colorkey(BLACK)
self.ast_img4 = pygame.image.load("resources/ast4.png")
self.ast_img4 = pygame.transform.scale(self.ast_img4, (35, 35))
self.ast_img4.set_colorkey(BLACK)
self.pill_green = pygame.image.load("resources/bolt_gold.png")
self.pill_green = pygame.transform.scale(self.pill_green, (20, 20))
self.pill_green.set_colorkey(BLACK)
# noinspection SpellCheckingInspection
self.ast_imgs = [self.ast_img1, self.ast_img2, self.ast_img3, self.ast_img4]
self.reset_game()
def reset_game(self):
"""Resets all the important variables to ensure a new game iteration data is recorded"""
self.start = time.time()
self.end = 0
self.gametime = datetime.now().time().strftime("%H:%M:%S")
all_sprites.empty()
self.ship = Rocket(self.rocket_img, self.screen)
self.asteroids = pygame.sprite.Group()
self.pills = pygame.sprite.Group()
self.health = Healthbar(20, 10, 100, 15, 100, "green", "red")
self.fuel = Healthbar(self.screen.get_width() - 105, 10, 100, 15, 100, "yellow", "black")
self.game_score = Game_Score()
all_sprites.add(self.ship)
all_sprites.add(self.asteroids)
all_sprites.add(self.pills)
self.reward = 0
self.start = time.time()
self.get_states()
constants.ASTEROID_SPEED = 2
def get_states(self):
"""Returns a list of the important parameters"""
states_to_return = [self.ship.rect.centerx, self.health.h, self.fuel.hp, self.game_score.accuracy, self.reward]
asteroids_info = []
for asteroid in self.asteroids:
asteroids_info.extend([
asteroid.rect.centerx,
asteroid.rect.centery,
asteroid.speed.x,
asteroid.speed.y
])
while len(asteroids_info) < 40:
asteroids_info.extend([0, 0, 0, 0])
states_to_return.extend(asteroids_info[:40])
return states_to_return
def spawn_asteroids(self):
"""Instantiates and adds an asteroid to concerned groups"""
asteroid = Asteroid(constants.ASTEROID_SPEED, self.screen, self.ast_imgs)
all_sprites.add(asteroid)
self.asteroids.add(asteroid)
all_sprites.update()
def can_fire_bullet(self):
"""Checks if the player can fire a bullet"""
if time.time() - self.last_fired >= self.bullet_cooldown + np.random.randint(0, 20)/100:
self.last_fired = time.time()
return True
return False
def play_action(self, act):
"""Plays the action suggested by the agent"""
self.game_over = False
self.asteroids_hit = self.game_score.asteroids_hit
if self.health.hp <= 0:
self.death_reason = "Spacecraft Health 0"
self.ship.kill()
self.end = time.time()
self.game_over = True
elif self.fuel.hp <= 0:
self.death_reason = "Spacecraft Fuel 0"
self.ship.kill()
self.end = time.time()
self.game_over = True
self.clock.tick(constants.FPS)
global action
action = getaction(act)
for i in pygame.event.get():
if i.type == pygame.QUIT:
pygame.quit()
if action == 'fire' and self.can_fire_bullet():
self.ship.shoot()
self.game_score.bullet_fired()
if action == 'move_left' or action == 'move_right':
self.fuel.hp -= 0.1 * (constants.ASTEROID_SPEED/15)
if action == 'idle':
pass
self.screen.fill(BLACK)
all_sprites.update()
all_sprites.draw(self.screen)
self.fuel.draw(self.screen)
self.health.draw(self.screen)
for asteroid in self.asteroids:
if self.ship.rect.colliderect(asteroid.rect):
asteroid.kill()
self.reward -= 15
self.health.hp -= 1.5 * constants.ASTEROID_SPEED
for bullet in bullets:
for asteroid in self.asteroids:
if asteroid.rect.colliderect(bullet.rect):
self.game_score.asteroid_hit()
bullet.kill()
if self.game_score.asteroids_hit % (random.randint(1, 100)) == 0:
asteroid.shoot()
asteroid.kill()
self.spawn_asteroids()
self.reward += 10
constants.ASTEROID_SPEED += 0.15
for pill in pills:
if self.ship.rect.colliderect(pill.rect):
self.reward += 10
if pill.type == "Fuel Pill":
pill.kill()
self.fuel.hp += 10
if self.fuel.hp > self.fuel.max:
self.fuel.hp = self.fuel.max
if pill.type == "Health Pill":
pill.kill()
self.health.hp += 20
if self.health.hp > self.health.max:
self.health.hp = self.health.max
pygame.display.flip()
self.game_score.update_score()
return self.reward, self.game_over, self.game_score.get_score()