So I'm new to both pathfinding and and pygame but I'm trying to visualize the matrix into a grid in pygame. However, when I try the boxes get further and further away and it infinitely loops. So my question is how can i Get this to display a 4x4 grind like my matrix, with each square properly spaced? The code was split into 2 files. the main problem is in the second, I put the first here just for context. Also just theory is fine too I don't necessarily need a code solution just wann wrap my head around this.
P.s. To any familiar with pygame is there anyway to get the Grid/grid.node() information from the pathfinder? thatd make this a bit easier i think
from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
matrix = [
[1,1,0,1],
[1,0,0,1],
[1,1,0,1],
[1,1,1,1]
]
#creates a grid from the matrix
grid = Grid(matrix=matrix)
start = grid.node(0,0)
end = grid.node(3,0)
class setup:
def createFinder(self):
#create a new instance of a finder
self.finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
#The find_path function returns 2 values, the amounrs of times it runs to --
#-- find a path(runs) and the length of the finished path(path)
self.path, self.runs = self.finder.find_path(start, end, grid)
print(self.path)
def showPath(self):
print('operations', self.runs, 'path length:', len(self.path))
print(grid.grid_str(path=self.path, start=start, end=end))
from Pathfinder import *
import pygame
#creating a pygame screen
white = (255, 255, 255)
black = (198,238,78)
class pygameSetup():
def createDisplay(self):
self.gridDisplay = pygame.display.set_mode((800,600))
self.gridDisplay.fill(white)
def createSquare(self,x,y):
pygame.draw.rect(self.gridDisplay, black, [x,y,10,10])
def visualizeGrid(self):
x = 0
y = 0
z = 0
w = 0
v = 0
while w <=3:
pygame.display.update()
for i in matrix[z]:
print("The matrix is ", matrix[z],"and Z is: ", i)
v += 1
x += x + 11
if x >= 700:
x = 0
y += 11
if v == 4:
i += 1
z+=1
if z >= 4:
z = 0
v = 0
self.createSquare(x,y)
w+1
pS = pygameSetup()
pS.createDisplay()
pS.visualizeGrid()