def threeVSTwoKeepawayFeatures(state, size): C = (0.5 * size, 0.5 * size) K1, K2, K3, T1, T2 = state[1:6] # get features as a list of real numbers feats = [getDistance(K1, C),\ getDistance(K1, K2),\ getDistance(K1, K3),\ getDistance(K1, T1),\ getDistance(K1, T2),\ getDistance(K2, C),\ getDistance(K3, C),\ getDistance(T1, C),\ getDistance(T2, C),\ min(getDistance(K2, T1), getDistance(K2, T2)),\ min(getDistance(K3, T1), getDistance(K3, T2)),\ min(getAngle(K2, K1, T1), getAngle(K2, K1, T2)),\ min(getAngle(K3, K1, T1), getAngle(K3, K1, T2)) ] return feats
def draw(self): # draw backdrop self.game.screen.blit(self.imgBackground, (0, 0)) # draw sprites for bub in self.game.bubbleSprites: bub.draw() for pop in self.bubblePopSprites: pop.draw() # draw player aim if self.game.player.speed == 0 and not self.game.gameCompleted and self.game.player.scaleCountIn == -1: for n in range(2,8): sin = math.sin( util.getAngle( self.game.player.rect.centerx , self.game.player.rect.centery , self.game.mouseX , self.game.mouseY ) ) cos = math.cos( util.getAngle( self.game.player.rect.centerx , self.game.player.rect.centery , self.game.mouseX , self.game.mouseY ) ) y = math.ceil( self.game.player.rect.centery + cos*(n*10 + 15) - 4 ) x = math.ceil( self.game.player.rect.centerx + sin*(n*10 + 15) - 3 ) self.game.screen.blit(self.imgPlayerAim, (x, y)) # draw sidebar text.drawText(self.game.screen, str(self.game.gameGoal), 98, 155, 20, COLOR_SIDEBAR_TEXT , 'freesansbold.ttf', text.ALIGN_RIGHT) text.drawText(self.game.screen, self.game.gameOperator, 98, 217, 26, COLOR_SIDEBAR_TEXT, 'freesansbold.ttf' , text.ALIGN_RIGHT) text.drawText(self.game.screen, str(self.game.gameShots), 98, 283, 20, COLOR_SIDEBAR_TEXT, 'freesansbold.ttf' , text.ALIGN_RIGHT) self.game.screen.blit(self.imgPowerBar, (18, 322), self.rectPower) # draw sidebar text (drawing each text twice gives a nice bold look) for n in range(2): text.drawText(self.game.screen, self.game.strings["goal"], 16, 128, 11, COLOR_SIDEBAR_LABEL, 'freesansbold.ttf', text.ALIGN_LEFT) for n in range(2): text.drawText(self.game.screen, self.game.strings["operator"], 15, 192, 11, COLOR_SIDEBAR_LABEL, 'freesansbold.ttf', text.ALIGN_LEFT) for n in range(2): text.drawText(self.game.screen, self.game.strings["shots_left"], 16, 256, 11 , COLOR_SIDEBAR_LABEL, 'freesansbold.ttf', text.ALIGN_LEFT) for n in range(2): text.drawText(self.game.screen, self.game.strings["power"], 16, 320, 11, COLOR_SIDEBAR_LABEL, 'freesansbold.ttf', text.ALIGN_LEFT) # draw game completed overlay window if self.game.gameCompleted and self.game.player.speed < 2: if self.game.gameShots == 0 and self.game.bubblesLeft > 1: self.game.screen.blit(self.imgGameOver, (self.game.rectPlayfield.x + (self.game.rectPlayfield.height/2) -(self.imgGameOver.get_rect().width/2) , self.game.rectPlayfield.y + (self.game.rectPlayfield.height/2) - (self.imgGameOver.get_rect().height/2) ), self.gameCompletedRect) else: self.game.screen.blit(self.imgGameCompleted, (self.game.rectPlayfield.x + (self.game.rectPlayfield.height/2) -(self.imgGameCompleted.get_rect().width/2) , self.game.rectPlayfield.y + (self.game.rectPlayfield.height/2) - (self.imgGameCompleted.get_rect().height/2) ), self.gameCompletedRect)
def fourVSThreeKeepawayFeatures(state, size): C = (0.5 * size, 0.5 * size) K1, K2, K3, K4, T1, T2, T3 = state[1:8] # get features as a list of real numbers feats = [getDistance(K1, C),\ getDistance(K1, K2),\ getDistance(K1, K3),\ getDistance(K1, K4),\ getDistance(K1, T1),\ getDistance(K1, T2),\ getDistance(K1, T3),\ getDistance(K2, C),\ getDistance(K3, C),\ getDistance(K4, C),\ getDistance(T1, C),\ getDistance(T2, C),\ getDistance(T3, C),\ min([getDistance(K2, T1), getDistance(K2, T2), getDistance(K2, T3)]),\ min([getDistance(K3, T1), getDistance(K3, T2), getDistance(K3, T3)]),\ min([getDistance(K4, T1), getDistance(K4, T2), getDistance(K4, T3)]),\ min([getAngle(K2, K1, T1), getAngle(K2, K1, T2), getAngle(K2, K1, T3)]),\ min([getAngle(K3, K1, T1), getAngle(K3, K1, T2), getAngle(K3, K1, T3)]),\ min([getAngle(K4, K1, T1), getAngle(K4, K1, T2), getAngle(K4, K1, T3)]),\ ] return feats
def handleEvents(self, engine, events): for event in events: if event.type == pygame.QUIT: engine.exit() if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: mousex, mousey = pygame.mouse.get_pos() angle = getAngle(self.player.rect.x, self.player.rect.y, mousex, mousey) p = self.player.shoot(angle) if p: self.bullet_sprites.add(p) self.sprites.add(p) self.player.control(event) if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: engine.addState(PauseGameState(self.sprites)) if event.key == pygame.K_r: engine.changeState(Level())
def computeVelocity(self): CHANCE = 0.01 if self.VOs is None: #no need to change velocity self.newVelocity = self.prefVelocity return if self.VOs == MOVE_BACK: self.newVelocity = AVOID_VELOCITY return if util.decideForward(CHANCE): #self.newVelocity=np.dot(random.uniform(0,1),self.newVelocity) self.newVelocity = self.getRandomMove() #self.newVelocity=np.add(np.dot(0.5,self.prefVelocity),np.dot(0.5,[random.random(),random.random()])) else: #self.newVelocity=np.dot(random.uniform(-1,0),self.newVelocity) #self.newVelocity=np.add(np.dot(-0.5,self.prefVelocity),np.dot(-0.5,[random.random(),random.random()])) self.newVelocity = [0, 0] return #if prefVelocity is outside of VOS, do nothing, newV=prefV #test if prefVelocity is inside any of the VOs inVOs = False for vo in self.VOs: if vo.isPointInVO(self.prefVelocity): inVOs = True break #print 'not in VO' if not inVOs: self.newVelocity = self.prefVelocity return vLeft = [] vRight = [] leftDist = None rightDist = None #points inside has the structure: (dist with prefV, (x,y)) closestPoints = [] for vo in self.VOs: #process left line, find closest point on left line if not util.isValidTriangle(self.prefVelocity, vo.apex, vo.leftLine): #self.location is on the same line of left line #print 'not valid triangle L' dist1 = np.linalg.norm(np.subtract(self.prefVelocity, vo.apex)) dist2 = np.linalg.norm( np.subtract(self.prefVelocity, vo.leftLine)) if (dist1 < dist2): #apex is cloest point vLeft = vo.apex leftDist = dist1 else: #prefVelocity on left line, cloest point is prefVelocity itself vLeft = self.prefVelocity leftDist = 0 else: #print 'valid triangle L' angle = util.getAngle(self.prefVelocity, vo.apex, vo.leftLine) # print 'L ',angle #closest point is apex if (angle >= 90): vLeft = vo.apex leftDist = np.linalg.norm( np.subtract(self.prefVelocity, vo.apex)) else: point, leftDist = util.getNearestPointOnALine( vo.apex, vo.leftLine, self.prefVelocity) vLeft = point #process right line, find closest point on right line if not util.isValidTriangle(self.prefVelocity, vo.apex, vo.rightLine): #self.location is on the same line of left line #print 'not valid triangle R' dist1 = np.linalg.norm(np.subtract(self.prefVelocity, vo.apex)) dist2 = np.linalg.norm( np.subtract(self.prefVelocity, vo.rightLine)) if (dist1 < dist2): #apex is cloest point vRight = vo.apex rightDist = dist1 else: #prefVelocity on left line, cloest point is prefVelocity itself vRight = self.prefVelocity rightDist = 0 else: #print 'valid triangle R' angle = util.getAngle(self.prefVelocity, vo.apex, vo.rightLine) #print 'R ',angle #closest point is apex if (angle >= 90): vRight = vo.apex rightDist = np.linalg.norm( np.subtract(self.prefVelocity, vo.apex)) else: point, rightDist = util.getNearestPointOnALine( vo.apex, vo.rightLine, self.prefVelocity) vRight = point #check if new velocity is crossing with the boundries leftIntersect = util.isIntersect(vo.apex, vo.rightLine, (0, 0), vLeft) rightIntersect = util.isIntersect(vo.apex, vo.leftLine, (0, 0), vRight) #print 'vleft: '+str(vLeft) #print 'vRight: '+str(vRight) #print leftIntersect #print rightIntersect leftIntersect = False rightIntersect = False if (not leftIntersect and not rightIntersect): heapq.heappush(closestPoints, (leftDist, np.array(vLeft).tolist())) heapq.heappush(closestPoints, (rightDist, np.array(vRight).tolist())) #if leftDist < rightDist: # self.newVelocity=vLeft #else: # self.newVelocity=vRight elif leftIntersect: #self.newVelocity=vRight heapq.heappush(closestPoints, (rightDist, np.array(vRight).tolist())) else: heapq.heappush(closestPoints, (leftDist, np.array(vLeft).tolist())) #self.newVelocity=vLeft #for every points in cloestPoints, check if these points are inside any VOs #if true, then pass current point, move to next cloestest points #while list is not empty while (closestPoints): curr = heapq.heappop(closestPoints) #print 'curr is: '+str(curr) inVO = False for vo in self.VOs: if vo.isPointInVO(curr[1]): inVO = True #print 'inVO=True' break #inVO=False if not inVO: self.newVelocity = curr[1] return #return None #self.newVelocity=[0,0] #return #if all points are in VO: #find intersection points between VOs intersections = [] for vo in self.VOs: for other in self.VOs: if vo is other: continue intersections += vo.getIntersectionPoints(other) points = [] for pt in intersections: heapq.heappush( points, (np.linalg.norm(np.subtract(pt, self.prefVelocity)), pt)) #print 'points is:', #print points while (points): curr = heapq.heappop(points) #print 'curr intersection is: '+str(curr) inVO = False for vo in self.VOs: if vo.isPointInVO(curr[1]): inVO = True break #inVO=False if not inVO: self.newVelocity = curr[1] return ''' #apply random number to this dangerous point print 'use random point' while(True): newPoint=(curr[1][0]+random.uniform(-0,0),curr[1][1]+random.uniform(-0,0)) newPointInVO=False for vo in self.VOs: if vo.isPointInVO(newPoint): newPointInVO=True break if not newPointInVO: self.newVelocity=newPoint return #self.newVelocity=curr[1] #return ''' print 'applying random' if util.decideForward(0.3): #self.newVelocity=np.dot(random.uniform(0,1),self.newVelocity) self.newVelocity = self.getRandomMove() else: #self.newVelocity=np.dot(random.uniform(-1,0),self.newVelocity) self.newVelocity = [0, 0] #print 'no good new velocity, move back 1 step' return
def computeVelocityFor2(self): if self.VOs is None: #no need to change velocity self.newVelocity = self.prefVelocity return #if prefVelocity is outside of VOS, do nothing, newV=prefV vo = self.VOs[0] if not vo.isPointInVO(self.prefVelocity): #print 'not in VO' self.newVelocity = self.prefVelocity return vLeft = [] vRight = [] leftDist = None rightDist = None for vo in self.VOs: #process left line, find closest point on left line if not util.isValidTriangle(self.prefVelocity, vo.apex, vo.leftLine): #self.location is on the same line of left line #print 'not valid triangle L' dist1 = np.linalg.norm(np.subtract(self.prefVelocity, vo.apex)) dist2 = np.linalg.norm( np.subtract(self.prefVelocity, vo.leftLine)) if (dist1 < dist2): #apex is cloest point vLeft = vo.apex leftDist = dist1 else: #prefVelocity on left line, cloest point is prefVelocity itself vLeft = self.prefVelocity leftDist = 0 else: #print 'valid triangle L' angle = util.getAngle(self.prefVelocity, vo.apex, vo.leftLine) # print 'L ',angle #closest point is apex if (angle >= 90): vLeft = vo.apex leftDist = np.linalg.norm(vo.apex) else: point, leftDist = util.getNearestPointOnALine( vo.apex, vo.leftLine, self.prefVelocity) vLeft = point #process right line, find closest point on right line if not util.isValidTriangle(self.prefVelocity, vo.apex, vo.rightLine): #self.location is on the same line of left line #print 'not valid triangle R' dist1 = np.linalg.norm(np.subtract(self.prefVelocity, vo.apex)) dist2 = np.linalg.norm( np.subtract(self.prefVelocity, vo.rightLine)) if (dist1 < dist2): #apex is cloest point vRight = vo.apex rightDist = dist1 else: #prefVelocity on left line, cloest point is prefVelocity itself vRight = self.prefVelocity rightDist = 0 else: #print 'valid triangle R' angle = util.getAngle(self.prefVelocity, vo.apex, vo.rightLine) #print 'R ',angle #closest point is apex if (angle >= 90): vRight = vo.apex rightDist = np.linalg.norm(vo.apex) else: point, rightDist = util.getNearestPointOnALine( vo.apex, vo.rightLine, self.prefVelocity) vRight = point leftIntersect = util.isIntersect(vo.apex, vo.rightLine, (0, 0), vLeft) rightIntersect = util.isIntersect(vo.apex, vo.leftLine, (0, 0), vRight) if (not leftIntersect and not rightIntersect): if leftDist < rightDist: self.newVelocity = vLeft else: self.newVelocity = vRight elif leftIntersect: self.newVelocity = vRight else: self.newVelocity = vLeft #project prefered velocity to line(self.position, minPoint) #print 'newVelocity:', #print self.newVelocity return
def update(self): # Update physics and move bubbles collisions = self.game.particleEnvironment.update() for spr in self.game.bubbleSprites: # update bubble rects (contains the visual position of the bubble) spr.rect.centerx = spr.x + self.game.rectPlayfield.left spr.rect.centery = spr.y + self.game.rectPlayfield.top match = False for col in collisions: # if there were collision, check to see if any mathematical matches occurred if col[0] != None and col[1] != None: match = self.matchBubbles(col[0], col[1]) if match == False: if len(collisions) > 0: # play sound if there were collisions if (None,None) in collisions: # indicates a wall bounce self.game.audio.playSound(self.sfxWallBounce) else: self.game.audio.playSound(self.sfxBounce) # update bubbles for bub in self.game.bubbleSprites: bub.update() # wait for bubble popups to finish before game starts if self.game.player.scaleCountIn != -1: return # update power bar moving back and forth if self.game.player.speed == 0 and not self.game.gameCompleted: self.powerCounter += self.powerIncrement * self.game.ticks if self.powerCounter >= 1.1: self.powerCounter = 1.1 self.powerIncrement = -self.powerIncrement elif self.powerCounter <= 0.0: self.powerCounter = 0.0 self.powerIncrement = -self.powerIncrement self.rectPower.width = (int(90 * max(min(self.powerCounter, 1.0), 0.1)) / 8) * 8 # drawing width of power bar. # handle mouse events if self.game.mouseClicked: # skip clicks on button areas if self.rectButtonRestart.collidepoint(self.game.mouseX, self.game.mouseY) or self.rectButtonExit.collidepoint(self.game.mouseX, self.game.mouseY): pass # shoot the ball elif self.game.player.speed == 0 and not self.game.gameCompleted: self.game.gameShots -= 1 if self.game.gameShots == 0: self.game.gameCompleted = True self.game.player.speed = max(min(self.powerCounter, 1.0), 0.1) * MAX_SHOT_FORCE * self.game.ticks self.game.player.angle = util.getAngle(self.game.player.rect.centerx, self.game.mouseY, self.game.mouseX ,self.game.player.rect.centery) self.game.audio.playSound(self.sfxShoot) # update bubble pop sprites for pop in self.bubblePopSprites: pop.update() # animate game completed overlay window if self.game.gameCompleted and self.game.player.speed < 2: if self.game.mouseClicked: self.game.fadeTo(GM_GAME,False) if self.gameCompletedRect.height == 0: # play game completed sound only once if self.game.gameShots == 0 and self.game.bubblesLeft > 1: self.game.audio.playSound(self.sfxGameOver ) else: self.game.audio.playSound(self.sfxComplete ) self.gameCompletedRect.height += 350 * self.game.ticks