def OnCheking(node): """ Changes the given node color to the "cheking color" unless it is the start or end node """ if node in (start_node, end_node): return node.ChangeColor(colors.NodeColors.cheking.value)
def SetEnd(node): """ Sets the given node as End Node and resets the last end node to normal (if there is any) """ global end_node if end_node != None: end_node.Reset() node.Reset() node.ChangeColor(colors.NodeColors.end.value) end_node = node
def SetStart(node): """ Sets the given node as Start Node and resets the last start node to normal (if there is any) """ global start_node if start_node != None: start_node.Reset() node.Reset() node.ChangeColor(colors.NodeColors.start.value) start_node = node
def OnVisited(node): if node in (start_node, end_node): return node.ChangeColor(colors.NodeColors.visited.value) #We don't want to flip too fast because the visualization will be slow call_time = time.time() while True: #I didn't use time.sleep here beause it will set pygame as not responding if time.time() - call_time >= delay_time: pygame.display.flip() return
def FindPath(): """ Start the algorithm then draw the path """ if not pathfinder.GetIsWeighted(algorithm): Reset(False, True) else: Reset(False, False) path = algorithm(node_list, start_node, end_node) if path == None: return for node in path: if node in (start_node, end_node): continue node.ChangeColor(colors.NodeColors.path.value)
def Reset(remove_obstacles=False, remove_weight=False): """ Rsets all the nodes to normal nodes except the start and end nodes """ global remaining_nodes_to_flip remaining_nodes_to_flip = 0 screen.fill(background_color) for column in node_list: for node in column: if node.is_obstacle: if remove_obstacles: node.Reset() node.Draw() elif node.is_weight: if remove_weight: node.Reset() else: node.ResetDistances() node.ChangeColor(colors.NodeColors.normal.value) elif node not in [start_node, end_node]: node.Reset() else: node.ResetDistances() node.Draw()