-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.py
278 lines (260 loc) · 10.5 KB
/
World.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
import sys, pygame
import numpy as np
import random
from scipy import misc
import matplotlib.pyplot as plt
#from Panoptir import *
#from Slogun import *
#from Slobeam import *
from Bot import *
from Move import *
from GameObject import *
from Trainer import *
from pygame import key as key
import pygame.freetype
from globals import *
from Breeding import *
from rlglue.environment.Environment import Environment
from rlglue.environment import EnvironmentLoader
from rlglue.types import Observation
from rlglue.types import Action
from rlglue.types import Reward_observation_terminal
class World(Environment):
def env_init(self):
return "VERSION RL-Glue-3.0"
def __init__(self,p1Type,p2Type):
self.font = pygame.freetype.SysFont('',18)
self.clock = pygame.time.Clock()
self.killMe = set()
self.spawnMe = set()
self.players = set()
self.bullets = pygame.sprite.Group()
self.everybody = pygame.sprite.Group()
#setup players
Trainer.world = self
self.trainers = (Trainer(True,True),Trainer(False))
if self.trainers[0].mon == []:
for i in range(1):
backupMon = VarMon([SharpWalk(),BounceShot(),Shark()],'CreatureSprite.png',p1Type)
self.trainers[0].mon.append(backupMon)
self.trainers[0].setCurMon(0)
if p1Type == 2:
self.agent = self.trainers[0].getCurMon()
self.createEnemyTrainer()
self.effects = pygame.sprite.Group()
self.background = pygame.image.load("floor.jpg").convert()
self.background = pygame.transform.scale(self.background,size)
self.portraitBackground = pygame.image.load('border.jpg').convert()
self.portraitBackground = pygame.transform.scale(self.portraitBackground,[size[0],int(size[1]/3.0)])
self.man = pygame.image.load('CreatureVert.jpg').convert()
self.man = pygame.transform.scale(self.man,[int(size[0]*(1/8.0)),int(size[1]/4.0)])
screen.blit(self.background,[0,0])
self.updateGUI(0,0)
def updateGUI(self,score1,score2):
screen.blit(self.portraitBackground,[0,size[1]])
screen.blit(self.man,[0+int(self.man.get_width()/2.55),size[1]+int(self.man.get_height()/7.5)])
scoreDisp1,_ = self.font.render(str(score1),(255,255,255))
scoreDisp2,_ = self.font.render(str(score2),(255,255,255))
screen.blit(scoreDisp1,[0,size[1]])
screen.blit(scoreDisp2,[size[0]-100,size[1]])
pygame.display.update()#the only time portrait area is updated
def createEnemyTrainer(self):
moveList = []
for i in range(1):
print i
for s in MOVETYPES:
if not Move.moveDict[s] == None:
moveList.append(random.sample(Move.moveDict[s],1)[0]())
backupMon = VarMon(moveList,'CreatureSprite.png',p2Type,VarBot)
self.trainers[1].mon.append(backupMon)
self.trainers[1].setCurMon(0)
def start(self):
print Move.moveDict
pass
def env_start(self):
self.start()
returnObs=Observation()
returnObs.intArray=[1]
return returnObs
def getInput(self):
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
pixels = np.array(pygame.surfarray.array2d(screen))
plt.imshow(pixels)
plt.show()
if event.key == pygame.K_b:
breedable = Breeding(self.trainers[0].getBreedableMon())
self.trainers[0].setBreedableMon(breedable)
print self.trainers[0].mon
if event.key == pygame.K_6:
self.trainers[0].save()
'''
if event.key == pygame.K_7:
self.trainers[0].load()
'''
curPressed = key.get_pressed()
if curPressed[pygame.K_ESCAPE]:
pygame.event.post(pygame.event.Event(pygame.QUIT))
def step(self):
'''
single step of logic and rendering, currently called 60 times per second
'''
self.clock.tick(fps)
self.fps = self.clock.get_fps()
#print self.clock.get_fps()
self.getInput()
for t in self.trainers:
t.step()
for p in self.players:
p.step(self)
if (p.rect.left < 0 and p.velo[0] < 0) or (p.rect.right > width and p.velo[0]>0):
p.velo[0] = 0
if (p.rect.top < 0 and p.velo[1] < 0) or (p.rect.bottom > height and p.velo[1]>0):
p.velo[1] = 0
for b in self.bullets:
b.step()
if getattr(b,'bounce',False):
if (b.rect.left < 0 and b.velo[0] < 0) or (b.rect.right > width and b.velo[0]>0):
b.velo[0] *= -1
if (b.rect.top < 0 and b.velo[1] < 0) or (b.rect.bottom > height and b.velo[1]>0):
b.velo[1] *= -1
self.environmentalActions()
self.resolveConflicts()
self.render()
def environmentalActions(self):
'''control anything neither player can directly influence
currently: invisible walls contain players to the field
'''
for p in self.players:
if (p.rect.left < 0 ):
p.rect.left = 0
if (p.rect.right > width ):
p.rect.right = width
if (p.rect.top < 0):
p.rect.top = 0
if (p.rect.bottom > height):
p.rect.bottom = height
def env_step(self,action):
self.agent.botAction = action
self.step()
pixels = pygame.surfarray.array2d(screen)
theObs=Observation()
theObs.intArray=misc.imresize(pixels,(84,84)).flatten().tolist()
returnRO=Reward_observation_terminal()
returnRO.r=1 #reward goes here
returnRO.o=theObs
returnRO.terminal= 0
return returnRO
def resolveConflicts(self):
'handle conflicts between the results of all state changes'
self.resolveCollisions()
self.resolveEffects()
self.resolveDeath()
def resolveCollisions(self):
'''
handle collisions between objects
types of collision handled by dynamic properties
currently (default):
damage - how much does collision hurt? (0)
passThrough - does collision move you? (yes)
NOTE: COLL MOVE CURRENTLYONLY FOR CIRCLES
breakable - does collision kill you? (no)
NOTE: currently collision only occur between players and stuff. Thus non-player entities can only collide with players and not each other!
'''
for p in self.players:
collSprites = pygame.sprite.spritecollide(p,self.everybody,False,pygame.sprite.collide_rect)
for c in collSprites:
if p is c: #sprite always contained in the group
continue
p.damageToTake += getattr(c,'damage',0)
if not getattr(c,'passThrough',True):
dx = p.rect.centerx-c.rect.centerx + np.random.rand() -.5
dy = p.rect.centery-c.rect.centery + np.random.rand() -.5
delta = pygame.math.Vector2(dx,dy) #cur distance between sprite centers
desiredDis = p.radius + c.radius + 3 # how far away sprite must be to avoid collision
delta.scale_to_length(desiredDis)
p.rect.centerx += delta.x
p.rect.centery += delta.y
if getattr(c,'breakable',False):
c.deathAnimation(self)
self.killMe.add(c)
#print p.health
if p.health < 0:
self.killMe.add(p)
if not p.alive():
self.spawnMe.add(p)
def resolveEffects(self):
'handle spell effects, and other time dependent game state'
curTime = pygame.time.get_ticks()
for e in self.effects:
if curTime > e.killTime:
self.killMe.add(e)
def resolveDeath(self):
'handle the removal of sprites and other objects from the game world'
for k in self.killMe:
#if k in self.players:
# self.players.remove(k)
k.kill()
for p in self.spawnMe:
print 'respawn!'
for t in self.trainers:
if p in t.mon:
print 'player' + str(1-t.num) + ' points!'
self.trainers[1-t.num].score += 100
if t.num == 0: #player
p.fainted = True
nextMon = (ind for ind in range(len(t.mon)) if not t.mon[ind].fainted)
try:
t.switchToMon(nextMon.next())
except StopIteration:
t.revive()
t.setCurMon(0)
print t.mon[0].alive()
else: #opponent
lost = t.loseCurMon()
if lost:
self.createEnemyTrainer()
self.updateGUI(self.trainers[0].score,self.trainers[1].score)
for play in self.players:
play.playerChange(self)
self.killMe.clear()
self.spawnMe.clear()
def render(self):
'was done in main'
screen.blit(self.background,[0,0])
for p in self.players:
screen.blit(p.frame,p.rect.topleft)
self.everybody.draw(screen)
self.effects.draw(screen)
fpsDisp,_ = self.font.render(str(self.fps),(255,255,255))
screen.blit(fpsDisp,[0,0])
pygame.display.update(gameArea)
if __name__=="__main__":
pygame.init()
pygame.freetype.init()
gameArea = pygame.Rect([0,0],size)
if len(sys.argv) > 1:
useGlue = (sys.argv[1] == 'True')
else:
useGlue = False
black = 0,0,0
screen = pygame.display.set_mode([size[0],int(size[1]*(4/3.0))])#,pygame.FULLSCREEN)
count = 0
if len(sys.argv) > 2:
p1Type = int(sys.argv[2])
else:
p1Type = 0
if len(sys.argv) > 3:
p2Type = int(sys.argv[3])
else:
p2Type = 1
if useGlue:
EnvironmentLoader.loadEnvironment(World(p1Type,2))
else:
world = World(p1Type,p2Type)
world.start()
while True or count < 300:
world.step()
count += 1