问题是,我做了一个简短的算法,找到它穿过迷宫的路径,并用蓝色标记它访问过的所有正方形。核心程序工作得很好,但问题是GUI只在整个过程完成后显示迷宫及其访问的方块。这通常不会是一个问题,但我需要能够明显地看到算法遍历迷宫,因为它去。问题是,当我在搜索算法的每次迭代中调用UpdateMaze
函数时,它似乎直到整个遍历完成才生效。你知道吗
import tkinter as tk
from tkinter import *
import time
class MazeGUI():
def __init__(self):
self.maze =[
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[4, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 4],
[4, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 4],
[4, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 4],
[4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 4],
[4, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 4],
[4, 0, 1, 2, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 4],
[4, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 4],
[4, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 4],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
]
self.wall = tk.PhotoImage(file = "MazePiece_Wall.gif")
self.space = tk.PhotoImage(file = "MazePiece_Space.gif")
self.edge = tk.PhotoImage(file = "MazePiece_Outer.gif")
self.visited = tk.PhotoImage(file = "MazePiece_Visited.gif")
self.finish = tk.PhotoImage(file = "MazePiece_Finish.gif")
def UpdateMaze(self):
for y in range(len(self.maze)):
for x in range(len(self.maze[y])):
if self.maze[y][x] == 0:
label = Label(root, image=self.space,
width=20, height=20).grid(row=y, column=x)
elif self.maze[y][x] == 1:
label = Label(root, image=self.wall,
width=20, height=20).grid(row=y, column=x)
elif self.maze[y][x] == 2:
label = Label(root, image=self.finish,
width=20, height=20).grid(row=y, column=x)
elif self.maze[y][x] == 3:
label = Label(root, image=self.visited,
width=20, height=20).grid(row=y, column=x)
elif self.maze[y][x] == 4:
label = Label(root, image=self.edge,
width=20, height=20).grid(row=y, column=x)
def Move(Maze,x,y):
if Maze.maze[y][x] == 2:
return True
elif Maze.maze[y][x] == 1:
return False
elif Maze.maze[y][x] == 3:
return False
elif Maze.maze[y][x] == 4:
return False
Maze.maze[y][x] = 3
if ((x < len(Maze.maze)-1 and Move(Maze,x+1, y))
or (y > 0 and Move(Maze,x, y-1))
or (x > 0 and Move(Maze,x-1, y))
or (y < len(Maze.maze)-1 and Move(Maze,x, y+1))):
return True
return False
root = Tk()
Maze = MazeGUI()
root.lift()
StartPosX = 1
StartPosY = 1
Move(Maze,StartPosX,StartPosY)
Maze.UpdateMaze()
root.mainloop()
我认为下面的内容展示了如何做你想做的事情的基本知识。它使用} 方法定期调用
tkinter
通用^{make_move()
方法,该方法最终调用update_maze()
来重新显示每次移动后的迷宫。你知道吗看起来好像什么都没有发生,但过一段时间你应该会看到迷宫显示的变化。由于使用了我介绍的
random
模块来生成下一步应该是什么,所以具体需要多长时间是不可预测的——因为我真的不理解在示例代码的Move()
函数中这样做的逻辑。这是什么可能需要“修复”它,确切地说,无论它是你试图完成。你知道吗注意:我还更改了您的代码,使其在代码格式和类、函数、变量等的命名方面更接近PEP 8 - Style Guide for Python Code,以使其更具可读性(IMO)。你知道吗
对任何感兴趣的人来说,这里是我制作并用于测试的
.gif
图像(因此您可以下载它们来运行代码):MazePiece_Finish.gif
MazePiece_Outer.gif
!MazePiece_Space.gif
MazePiece_Visited.gif
MazePiece_Wall.gif
相关问题 更多 >
编程相关推荐