将pygame中的矩阵可视化形成网格
Visualizing a matrix in pygame to form a grid
所以我对寻路和 pygame 都不熟悉,但我正在尝试将矩阵可视化为 pygame 中的网格。然而,当我尝试时,盒子越来越远,它会无限循环。所以我的问题是我怎样才能让它显示像我的矩阵一样的 4x4 研磨,每个正方形间隔适当?代码被分成 2 个文件。主要问题在第二个,我把第一个放在这里只是为了上下文。理论也很好,我不一定需要代码解决方案,只是想解决这个问题。
P.s。对于任何熟悉 pygame 的人来说,无论如何都可以从探路者那里获得 Grid/grid.node() 信息?我认为这会让这更容易
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()
您的 visualizeGrid
功能比您实际需要的要多得多。我不确定为什么你有所有这些变量(v、w、x、y、z),你只需要一个 x
和一个 y
坐标。如果你只想显示网格,这里有一个简单的工作解决方案:
import pygame
gridDisplay = pygame.display.set_mode((200, 200))
pygame.display.get_surface().fill((200, 200, 200)) # background
matrix = [[1 ,1 ,0 ,1],
[1 ,0 ,0 ,1],
[1 ,1 ,0 ,1],
[1 ,1 ,1 ,1]]
# we use the sizes to draw as well as to do our "steps" in the loops.
grid_node_width = 10
grid_node_height = 10
def createSquare(x, y, color):
pygame.draw.rect(gridDisplay, color, [x, y, grid_node_width, grid_node_height ])
def visualizeGrid():
y = 0 # we start at the top of the screen
for row in matrix:
x = 0 # for every row we start at the left of the screen again
for item in row:
if item == 0:
createSquare(x, y, (255, 255, 255))
else:
createSquare(x, y, (0, 0, 0))
x += grid_node_width # for ever item/number in that row we move one "step" to the right
y += grid_node_height # for every new row we move one "step" downwards
pygame.display.update()
visualizeGrid() # call the function
while True:
pass # keeps the window open so you can see the result.
有关
get the Grid/grid.node() information from the pathfinder
docs 提到您可以使用 grid.node(index_x, index_y)
从您的 grid
获取节点:
matrix = [
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]
grid = Grid(matrix=matrix)
start = grid.node(0, 0) # getting the first node in the 3x3 Matrix
end = grid.node(2, 2) # getting the last node in the 3x3 Matrix
所以我对寻路和 pygame 都不熟悉,但我正在尝试将矩阵可视化为 pygame 中的网格。然而,当我尝试时,盒子越来越远,它会无限循环。所以我的问题是我怎样才能让它显示像我的矩阵一样的 4x4 研磨,每个正方形间隔适当?代码被分成 2 个文件。主要问题在第二个,我把第一个放在这里只是为了上下文。理论也很好,我不一定需要代码解决方案,只是想解决这个问题。
P.s。对于任何熟悉 pygame 的人来说,无论如何都可以从探路者那里获得 Grid/grid.node() 信息?我认为这会让这更容易
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()
您的 visualizeGrid
功能比您实际需要的要多得多。我不确定为什么你有所有这些变量(v、w、x、y、z),你只需要一个 x
和一个 y
坐标。如果你只想显示网格,这里有一个简单的工作解决方案:
import pygame
gridDisplay = pygame.display.set_mode((200, 200))
pygame.display.get_surface().fill((200, 200, 200)) # background
matrix = [[1 ,1 ,0 ,1],
[1 ,0 ,0 ,1],
[1 ,1 ,0 ,1],
[1 ,1 ,1 ,1]]
# we use the sizes to draw as well as to do our "steps" in the loops.
grid_node_width = 10
grid_node_height = 10
def createSquare(x, y, color):
pygame.draw.rect(gridDisplay, color, [x, y, grid_node_width, grid_node_height ])
def visualizeGrid():
y = 0 # we start at the top of the screen
for row in matrix:
x = 0 # for every row we start at the left of the screen again
for item in row:
if item == 0:
createSquare(x, y, (255, 255, 255))
else:
createSquare(x, y, (0, 0, 0))
x += grid_node_width # for ever item/number in that row we move one "step" to the right
y += grid_node_height # for every new row we move one "step" downwards
pygame.display.update()
visualizeGrid() # call the function
while True:
pass # keeps the window open so you can see the result.
有关
get the Grid/grid.node() information from the pathfinder
docs 提到您可以使用 grid.node(index_x, index_y)
从您的 grid
获取节点:
matrix = [
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]
grid = Grid(matrix=matrix)
start = grid.node(0, 0) # getting the first node in the 3x3 Matrix
end = grid.node(2, 2) # getting the last node in the 3x3 Matrix