-
Notifications
You must be signed in to change notification settings - Fork 16
/
gamestates.py
276 lines (228 loc) · 11.8 KB
/
gamestates.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
'''The MIT License (MIT)
Copyright (c) 2017 ActiveState Software Inc.
Written by Pete Garcin @rawktron
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''
import pygame
import utils
from utils import *
from actors import *
from brain import Brain
import math
import leaderboard
import gameover
# GameState object will return a new state object if it transitions
class GameState(object):
def update(self, screen, event_queue, dt, clock, joystick, netmodel, vizmodel):
return self
class Play(GameState):
def __init__(self, trainingMode):
if utils.trainedBrain:
self.brain = utils.trainedBrain
else:
self.brain = Brain()
self.enemyspeed = 16
self.enemyBullets = pygame.sprite.Group()
self.userBullets = pygame.sprite.Group()
self.userGroup = pygame.sprite.Group()
self.enemies = pygame.sprite.Group()
self.player = Player(self.userBullets)
self.enemy = Enemy(self.enemyBullets, self.brain,self.enemyspeed)
self.userGroup.add(self.player)
self.enemies.add(self.enemy)
self.player.lives = 3
self.score = 0
self.spawntimer = 0
self.spawnbreak = 8
self.trainingMode = trainingMode
def update(self, screen, event_queue, dt, clock, joystick, netmodel, vizmodel):
self.player.update(screen, event_queue, dt,joystick)
self.enemies.update(screen, event_queue, dt, (self.player.x,self.player.y), (self.player.velx,self.player.vely), self.trainingMode, netmodel)
# Spawn new enemies
self.spawntimer += dt
if self.spawntimer > self.spawnbreak:
self.spawnbreak = max(2,self.spawnbreak-0.5)
self.enemyspeed = max(0,self.enemyspeed+2)
self.enemies.add(Enemy(self.enemyBullets, self.brain,self.enemyspeed))
self.spawntimer = 0
if not(self.player.blinking):
player_hit = pygame.sprite.spritecollide(self.player,self.enemyBullets, True)
for bullet in player_hit:
self.brain.record_hit(bullet)
if not (self.trainingMode):
self.player.TakeDamage(20)
self.player.playanim("hit",(bullet.rect.x,bullet.rect.y))
if not(self.player.blinking and self.player.blinkon):
self.userGroup.draw(screen)
self.enemies.draw(screen)
self.enemyBullets.update(dt)
self.enemyBullets.draw(screen)
self.userBullets.update(dt)
self.userBullets.draw(screen)
enemies_hit = pygame.sprite.groupcollide(self.enemies,self.userBullets,False,True)
for enemy, bullets in enemies_hit.items():
enemy.TakeDamage(10)
for b in bullets:
enemy.playanim("hit",(b.rect.x,b.rect.y))
self.score += 50
## Update enemy animation frames
for enemy in self.enemies:
if enemy.anim:
if enemy.anim.playing:
enemy.anim.update(screen,(enemy.x+enemy.animoffset[0],enemy.y+enemy.animoffset[1]),dt)
else:
enemy.anim = None
# Effects go here TODO make them a sprite layer
if self.player.anim:
if self.player.anim.playing:
self.player.anim.update(screen,(self.player.x+self.player.animoffset[0],self.player.y+self.player.animoffset[1]),dt)
else:
self.player.anim = None
self.brain.draw(screen,vizmodel)
displaytext("FPS:{:.2f}".format(clock.get_fps()) , 16, 60, 20, WHITE, screen)
displaytext("Score: "+str(self.score), 16, 200, 20, WHITE, screen)
displaytext("Health: "+str(self.player.health), 16, 350, 20, WHITE, screen)
displaytext("Lives: "+str(self.player.lives) , 16, 500, 20, WHITE, screen)
displaytext("Neural Net Visualization", 16, 960, 20, WHITE, screen)
for event in event_queue:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
if (self.trainingMode):
self.brain.learn()
utils.trainedBrain = self.brain
if (netmodel == 1):
self.brain.train() # Train the tensorflow version
return Menu(self.brain)
if self.trainingMode:
self.brain.learn()
if not(self.player.alive()):
if (self.trainingMode):
self.brain.learn()
utils.trainedBrain = self.brain
return Menu(None)
else:
return GameOver(self.score)
return self
class GameOver(GameState):
def __init__(self,score):
self.score = score
self.name = ""
gameover.pressed = ""
def update(self,screen,event_queue,dt,clock, joystick, netmodel, vizmodel):
nextState = self
self.name = gameover.enter_text(event_queue,screen, 8)
for event in event_queue:
if event.type == pygame.KEYUP:
if event.key == pygame.K_RETURN:
self.name = gameover.pressed
leaderboard.StoreScore(self.name,self.score)
nextState = Leaderboard(self.name)
return nextState
class Leaderboard(GameState):
def __init__(self,name):
self.name = name
self.highscores = leaderboard.GetScores()
def update(self,screen,event_queue,dt,clock,joystick, netmodel, vizmodel):
nextState = self
leaderboard.DisplayLeaderBoard(screen,self.highscores,self.name)
for event in event_queue:
if event.type == pygame.KEYDOWN:
nextState = Menu(None)
return nextState
# Draws the menu on screen.
# This is a class that is just instantiated
# While that object exists, it processes stuff
# Only one "GameState" object can exist at one time
class Menu(GameState):
def __init__(self, brain):
self.menu_selection = 2
self.brain = brain
self.logo = pygame.image.load("art/neuro-blast_logo.png")
self.intel = pygame.image.load("art/Intel-logo_blue.png")
self.activestate = pygame.image.load("art/as-logo.png")
self.intel = pygame.transform.smoothscale(self.intel,(int(self.intel.get_width()/2),int(self.intel.get_height()/2)))
self.activestate = pygame.transform.smoothscale(self.activestate,(int(self.activestate.get_width()/2),int(self.activestate.get_height()/2)))
def update(self, screen, event_queue, dt,clock,joystick, netmodel, vizmodel):
# Logos/titles
screen.blit(self.logo,(screen.get_width() / 4 - 265,screen.get_height() * 3 / 4-500))
screen.blit(self.intel,(screen.get_width() / 4 - 300,screen.get_height()-130))
screen.blit(self.activestate,(screen.get_width() - 980,screen.get_height() - 130))
nextState = self
displaytext('Play', 32, screen.get_width() / 4 - 20, screen.get_height() * 3 / 4
- 80, WHITE, screen)
displaytext('Train', 32, screen.get_width() / 4 - 20, screen.get_height() * 3 / 4
- 40, WHITE, screen)
displaytext('Exit', 32, screen.get_width() / 4 - 20, screen.get_height() * 3 / 4,
WHITE, screen)
displaytext(u'\u00bb', 32, screen.get_width() / 4 - 60, screen.get_height() * 3 / 4
- 40*self.menu_selection, WHITE, screen)
# Each game state processes its own input queue in its own way to avoid messy input logic
for event in event_queue:
if event.type == pygame.KEYDOWN or event.type == pygame.JOYBUTTONDOWN:
if (event.type == pygame.KEYDOWN and (event.key == pygame.K_DOWN)) or (event.type == pygame.JOYBUTTONDOWN and (event.button == 1)) or (event.type == pygame.JOYAXISMOTION and (event.axis == 1 or event.value >= DEADZONE)):
self.menu_selection -= 1
if self.menu_selection == -1:
self.menu_selection = 2
if (event.type == pygame.KEYDOWN and (event.key == pygame.K_UP)) or (event.type == pygame.JOYBUTTONDOWN and (event.button == 0)) or (event.type == pygame.JOYAXISMOTION and (event.axis == 1 or event.value <= -DEADZONE)):
self.menu_selection += 1
if self.menu_selection == 3:
self.menu_selection = 0
if (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN) or (event.type == pygame.JOYBUTTONDOWN and event.button == 11):
if self.menu_selection == 2:
nextState = Play(False)
elif self.menu_selection == 1:
nextState = Play(True)
else:
nextState = None
if (event.type == pygame.KEYDOWN and event.key == pygame.K_x):
self.ExportModel()
if (event.type == pygame.KEYDOWN and event.key == pygame.K_d):
self.DumpData()
if (event.type == pygame.KEYDOWN and event.key == pygame.K_w):
self.DumpWeights()
return nextState
def ExportModel(self):
import keras.backend as K
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import utils
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import build_signature_def, predict_signature_def
from tensorflow.contrib.session_bundle import exporter
print ("EXPORTING MODEL...")
export_path = 'exported_brain'
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'inputs': self.brain.keras.input},
outputs={'outputs': self.brain.keras.output})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.TRAINING],
signature_def_map={'predict': signature})
builder.save()
print ("...done!")
def DumpWeights(self):
f = open('weights.csv', 'w')
self.brain.model.dump(f)
f.close()
def DumpData(self):
f = open('traindata.csv', 'w')
for k,v in self.brain.mapShots.iteritems():
# Convert our tuple to a numpy array
if k in self.brain.mapHits:
a = list(v)
myList = ','.join(map(str, a))
output = str(self.brain.mapHits[k])
f.write(myList+","+output+"\n")
f.close() # you can omit in most cases as the destructor will call it