我正在尝试在使用python代码显示的图中创建确切的迷宫,但有点打h。 我知道一个人可以将matplotlib与1和0组成的数组进行绘制。但是我还是不明白。
有人可以帮助我,请指导我。谢谢。
Ps:我仍然对python还是陌生的,但是代码的复杂程度无关紧要,我将尽力理解它。非常感谢
答案 0 :(得分:2)
在迷宫之后,我翻译成here的等效结构:
+-+-+-+-+ +-+-+-+-+-+
| | | |
+ + + +-+-+-+ +-+ + +
| | | | | | | |
+ +-+-+ +-+ + + + +-+
| | | | | | |
+ + + + + + + +-+ +-+
| | | | | |
+-+-+-+-+-+-+-+ +-+ +
| | | |
+ +-+-+-+-+ + +-+-+ +
| | | |
+ + + +-+ +-+ +-+-+-+
| | | | | |
+ +-+-+ + +-+ + +-+ +
| | | | | | | |
+-+ +-+ + + + +-+ + +
| | | | | | |
+ +-+ +-+-+-+-+ + + +
| | | | |
+-+-+-+-+-+ +-+-+-+-+
然后修改一些代码以使其更具可读性:
import matplotlib.pyplot as plt
maze = []
with open("maze.txt", 'r') as file:
for line in file:
line = line.rstrip()
row = []
for c in line:
if c == ' ':
row.append(1) # spaces are 1s
else:
row.append(0) # walls are 0s
maze.append(row)
plt.pcolormesh(maze)
plt.axes().set_aspect('equal') #set the x and y axes to the same scale
plt.xticks([]) # remove the tick marks by setting to an empty list
plt.yticks([]) # remove the tick marks by setting to an empty list
plt.axes().invert_yaxis() #invert the y-axis so the first row of data is at the top
plt.show()