所以目前我正在尝试通过读取 .txt 文件然后将其显示在 Python 的 Turtle 库中来绘制块迷宫。目前,我的代码只能绘制出方框,而不能绘制出方框周围的网格线。无论如何,有没有办法解决这个问题,因为我试图查看文档,他们只是建议turtle.Turtle.fillcolor,这似乎并不正确。
这是我当前的代码:
# import the necessary library
import turtle
# read the .txt file here!
with open("map01.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
# create the map here!
window = turtle.Screen()
window.bgcolor("white") # set the background as white(check if this is default)
window.title("PIZZA RUNNERS") # create the titlebar
window.setup(700,700)
# create pen
class Pen(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("square")
self.color('grey')
self.penup()
self.speed(0)
# create maze_list
maze = []
# add the maze to maze
maze.append(content)
# create conversion from the list to the map in turtle
def setup_maze(level):
for y in range(len(level)):
for x in range(len(level[y])):
# get the character at each x,y coordinate
character = level[y][x]
# calculate the screen x, y coordinates
screen_x = -288 + (x * 24)
screen_y = 288 - (y * 24)
# check if it is a wall
if character == "X":
pen.goto(screen_x, screen_y)
pen.stamp()
# create class instances
pen = Pen()
# set up the maze
setup_maze(maze[0])
# main game loop
while True:
pass
这就是我正在读取的当前文本文件的样子:
XXXXXXXXXXXX
X.........eX
X.XXX.XXX..X
X.XsX.X.XX.X
X.X......X.X
X.XXXXXXXX.X
X..........X
XXXXXXXXXXXX
's'和'e'应该代表起点和终点,尚未实现,因此可以忽略。点代表路径,X 代表墙壁。当前的地图(或txt)尺寸为8行x 12列。
现在我的输出如下所示:
我希望它看起来像这样(指的是网格的添加,而不是相同的图案迷宫):
慕沐林林
相关分类