【问题标题】:Visualizing a matrix in pygame to form a grid在pygame中可视化矩阵以形成网格
【发布时间】:2020-10-21 03:26:07
【问题描述】:

所以我对寻路和 pygame 都是新手,但我正在尝试将矩阵可视化为 pygame 中的网格。但是,当我尝试时,盒子会越来越远,它会无限循环。所以我的问题是我怎样才能让它像我的矩阵一样显示一个 4x4 的研磨,每个正方形的间距合适?代码被分成2个文件。主要问题在第二个,我把第一个放在这里只是为了上下文。理论也很好,我不一定需要代码解决方案,只是想解决这个问题。

附:对于任何熟悉 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()

【问题讨论】:

    标签: python python-3.x matrix pygame path-finding


    【解决方案1】:

    您的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.
    

    关于

    从探路者中获取 Grid/grid.node() 信息

    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
    

    【讨论】:

    • 非常感谢!我试图用所有这些变量强行解决问题,结果让自己感到困惑。您的代码清晰易懂。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 2012-01-03
    • 1970-01-01
    相关资源
    最近更新 更多