class mainWindow(): times=1 timestart=time.clock() def __init__(self): gamedata = pickle.load(open('C:/gamedata.p', 'rb')) blockHeights = gamedata['blockHeights'] vis_map = get_visibility_map(blockHeights) self.astar = AStar(blockHeights, vis_map.tolist()) self.start_point = (0,0) self.vis_map = vis_map blocks = np.array(self.vis_map) self.blocks = blocks.transpose() self.end_point = (40,25) self.root = Tkinter.Tk() self.frame = Tkinter.Frame(self.root, width=1024, height=768) self.frame.pack() self.canvas = Tkinter.Canvas(self.frame, width=1024,height=768) self.canvas.place(x=-2,y=-2) self.root.after(100,self.start) # INCREASE THE 0 TO SLOW IT DOWN self.root.mainloop() def start(self): try: self.im = Image.fromarray(np.uint8(cm.gist_yarg(self.blocks)*255)) if self.start_point[0] < self.blocks.shape[1]-1: self.start_point = (self.start_point[0]+1, self.start_point[1]) path = self.astar.get_path(self.start_point[0], self.start_point[1], self.end_point[0], self.end_point[1]) self.im.putpixel(self.start_point,(0,255,0)) for x in path: self.im.putpixel((x[0], x[1]),(255,0,0)) self.im.putpixel(self.end_point,(0,0,255)) newy, newx = self.blocks.shape scale = 8 self.im = self.im.resize((newx*scale, newy*scale)) self.photo = ImageTk.PhotoImage(image=self.im) self.canvas.create_image(0,0,image=self.photo,anchor=Tkinter.NW) self.root.update() self.times+=1 if self.times%33==0: print "%.02f FPS"%(self.times/(time.clock()-self.timestart)) self.root.after(2000,self.start) except Exception as e: print e self.root.after(10,self.start)
def solve_a_star(self): world = deepcopy(self.world) x = int(self.robot_x / self.cell_width) y = int(self.robot_y / self.cell_height) world[x][y] = -1 a_star = AStar(world, start_point=(x, y), end_point=(self.goal_x, self.goal_y)) a_star.a_star() return a_star.get_path()
def get_order(self, commander, bot): exact_target = commander.level.findRandomFreePositionInBox((commander.game.team.flag.position - 5.0, commander.game.team.flag.position + 5.0)) pomastar = AStar(commander.gamedata['blockHeights'], commander.twodpom) path = pomastar.get_path(bot.position.x, bot.position.y, exact_target.x, exact_target.y) try: if len(path) > 10: exact_target = Vector2(*path[len(path)/2]) target = commander.level.findRandomFreePositionInBox((exact_target-5.0, exact_target+5.0)) else: target = commander.game.team.flag.position # print target if not target: raise "no target found" except: print 'couldnt convert target to vec2' target = flagScoreLocation = commander.game.team.flagScoreLocation targetMin = target - Vector2(2.0, 2.0) targetMax = target + Vector2(2.0, 2.0) goal = commander.level.findRandomFreePositionInBox([targetMin, targetMax]) if not goal: return None, None if (goal - bot.position).length() > 8.0: return (orders.Charge, commander.defender, goal), {'description' : 'rushing to defend'} else: return (orders.Defend, commander.defender, (commander.middle - bot.position)), {'description' : 'defending'}
def generation_and_solution(root, solution_window): """ Maze's internal loop for solving and stepping.""" global hard_exit # Generate maze and its entrances m = Maze() m.generator = AldousBroder(solution_window.maze_width, solution_window.maze_height) m.generate() m.generate_entrances() # MazeUI initalization solution_window.initialize_maze(m) # Make entrance and exit into accessible areas solution_window.maze_grid[m.start[0], m.start[1]] = 0 solution_window.maze_grid[m.end[0], m.end[1]] = 0 solver = AStar(solution_window.maze_grid, m.start, m.end) new_elem = next(solver) while True: if solution_window.generate_new: # Clear canvas from old maze and stop new execution. # solution_window.canvas.delete("all") solution_window.start = False return if hard_exit: break root.update() # If start hasn't been pressed don't start solving if not solution_window.start: continue # If pause is pressed, skip calculation. If next is pressed calculate until next iteration if solution_window.pause: if solution_window.next: solution_window.next = False else: continue if new_elem is not None: # Recolor discovery path solution_window.recolor_point(new_elem[0], new_elem[1], (51, 109, 204)) solution_window.update_maze() new_elem = next(solver) else: # Recolor endpoint solution_window.recolor_point(m.end[0], m.end[1], (51, 109, 204)) solution_window.update_maze() path = solver.get_path() # Draw solution solution_window.draw_final_path(path, (53, 165, 24)) solution_window.update_maze()