def do(self, state): if not isinstance(state, State): print("AI says: You should give me a State object!") return self.time += 1 self.state = state blocked_rects = [ self.goal1.rect, self.goal2.rect, self.state.player1.rect ] player_centre_pos = self.player.get_centre_pos() self.astar = AStar(player_centre_pos, self.state.ball.get_centre_pos(), self.astar_size, blocked_rects) path = self.astar.get_shortest_path() self.player.relax = 1 - 2 * abs(1.0 * player_centre_pos[1] / general.height - 0.5) if len(path) >= 2: path_ = general.Array(path[1]) - [0, 0] path_[1] = player_centre_pos[1] if path_[0] < 200 or path_[0] > 400: path_[0] = player_centre_pos[0] self.go_to(path_) ## SHOOT: if self.state.ball.get_centre_pos()[1] < general.height - 30: if self.state.ball.get_centre_pos()[1] - player_centre_pos[1] > 5: self.state.ball.getShooted(self.player, 1)
def update(self): ## distances: self.players_dist = self.distance(self.player1, self.player2) self.player1_ball_dist = self.distance(self.player1, self.ball) self.player2_ball_dist = self.distance(self.player2, self.ball) ## relative velocities: self.players_relvel = self.distance(self.player1, self.player2) self.player1_ball_relvel = self.distance(self.player1, self.ball) self.player2_ball_relvel = self.distance(self.player2, self.ball) ## check in what area of the field each creature is: area_size = general.Array([general.width, general.height / 2.0 ]) * general.global_zoom self.areas = [0, 0] self.areas[0] = pygame.Rect([0, 0], area_size) self.areas[1] = pygame.Rect( general.Array([0, general.height / 2.0]) * general.global_zoom, area_size) self.what_area_player1 = self.is_in_area(self.player1, self.areas) self.what_area_player2 = self.is_in_area(self.player2, self.areas) self.what_area_ball = self.is_in_area(self.ball, self.areas)
def do(self, state): if not isinstance(state, State): print("AI says: You should give me a State object!") return self.time += 1 self.state = state blocked_rects = [ self.goal1.rect, self.goal2.rect, self.state.player1.rect ] self.astar = AStar(self.player.get_centre_pos(), self.state.ball.get_centre_pos(), self.astar_size, blocked_rects) path = self.astar.get_shortest_path() #for pos in path: # general.drawCircle(general.surface, pygame.Color(0,0,0), pos, 20,0) #p = self.go_to(path[0]) self.player.relax = 1 - 2 * abs(1.0 * self.player.get_centre_pos()[1] / general.height - 0.5) if len(path) >= 2: self.go_to(general.Array(path[1]) - [0, 0]) #general.drawCircle(general.surface, pygame.Color(0,0,0), path[1], 4,0) #self.astar.draw_grid() #self.astar.draw_blocks() #self.go_to(general.Array(state.ball.get_centre_pos()) - [0,20]) ## SHOOT: if self.state.ball.get_centre_pos()[1] < general.height - 30: if self.state.ball.get_centre_pos( )[1] - self.player.get_centre_pos()[1] > 5: self.state.ball.getShooted(self.player, 1)