Question: Python Programming If I wanted to create an A* Pathfinding algorithm for the code generated how would I go by doing that? Blue square represents
| Python Programming If I wanted to create an A* Pathfinding algorithm for the code generated how would I go by doing that? Blue square represents my end point. Red and Black represents obstacles. The starting point should be in the middle button of the square. I am unsure how to start or even how to do it in general. Ive seen many examples for JS, Java, and C++, but having a hard time with python. I would really appreciate someone who can get me from point A to B and avoid obstacles. In short: The output is a grid that shoes white blocks. As I drag my mouse over the blocks I can create walls that are black and obstacles that are red. I want to create a separate entity with a new color like green and have it avoid these obstacles like a maze and get to the target point which can be created with the color blue. TO create each color I use shift+click to create walls, left click to create obstacles, and blue to create an end point. I want my program to create a line that finds that fastest path and avoids all obstacles and walls to reach the blue targeted using A* path finding after the map is created. |
from tkinter import *
class Cell():
FILLED_COLOR_BG = "red"
EMPTY_COLOR_BG = "white"
FILLED_COLOR_BORDER = "green"
EMPTY_COLOR_BORDER = "black"
TARGET_COLOR = "blue"
WALL_COLOR = "black"
def __init__(self, master, x, y, size):
""" Constructor of the object called by Cell(...) """
self.master = master
self.abs = x
self.ord = y
self.size= size
self.fill= False
def _switch(self):
""" Switch if the cell is filled or not. """
self.fill= not self.fill
def draw(self):
""" order to the cell to draw its representation on the canvas """
if self.master != None :
fill = Cell.FILLED_COLOR_BG
outline = Cell.FILLED_COLOR_BORDER
if not self.fill:
fill = Cell.EMPTY_COLOR_BG
outline = Cell.EMPTY_COLOR_BORDER
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)
def draw2(self):
""" order to the cell to draw its representation on the canvas """
if self.master != None :
fill = Cell.TARGET_COLOR
outline = Cell.FILLED_COLOR_BORDER
if not self.fill:
fill = Cell.EMPTY_COLOR_BG
outline = Cell.EMPTY_COLOR_BORDER
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)
def draw3(self):
""" order to the cell to draw its representation on the canvas """
if self.master != None :
fill = Cell.WALL_COLOR
outline = Cell.FILLED_COLOR_BORDER
if not self.fill:
fill = Cell.EMPTY_COLOR_BG
outline = Cell.EMPTY_COLOR_BORDER
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)
class CellGrid(Canvas):
def __init__(self,master, rowNumber, columnNumber, cellSize, *args, **kwargs):
Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber, *args, **kwargs)
self.cellSize = cellSize
self.grid = []
for row in range(rowNumber):
line = []
for column in range(columnNumber):
line.append(Cell(self, column, row, cellSize))
self.grid.append(line)
#memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
#bind click action
self.bind("", self.handleMouseClick)
self.bind("", self.handleMouseClick2)
#walls
self.bind("", self.handleMouseClick3)
#bind moving while clicking
self.bind("", self.handleMouseMotion)
self.bind("", self.handleMouseMotion)
#bind release button action - clear the memory of midified cells.
self.bind("", lambda event: self.switched.clear())
self.draw()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
def _eventCoords(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
cell._switch()
cell.draw()
#add the cell to the list of cell switched during the click
self.switched.append(cell)
def handleMouseClick2(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
cell._switch()
cell.draw2()
#add the cell to the list of cell switched during the click
self.switched.append(cell)
def handleMouseClick3(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
cell._switch()
cell.draw3()
#add the cell to the list of cell switched during the click
self.switched.append(cell)
def handleMouseMotion(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
if event.state == 256:
if cell not in self.switched:
cell._switch()
cell.draw()
self.switched.append(cell)
elif event.state == 257:
if cell not in self.switched:
cell._switch()
cell.draw3()
self.switched.append(cell)
if __name__ == "__main__" :
app = Tk()
grid = CellGrid(app, 15, 20, 40)
grid.pack()
app.mainloop()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
