问题描述
所以目前我正试图通过读取.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行乘12列。
现在,我的输出如下所示:
我希望它看起来像这样(指的是添加网格,而不是相同的图案迷宫):
解决方法
假设您想要的是:
然后,我们需要将方形光标的大小从默认大小20调整为平铺大小24:
from turtle import Screen,Turtle
TILE_SIZE = 24
CURSOR_SIZE = 20
class Pen(Turtle):
def __init__(self):
super().__init__()
self.shape('square')
self.shapesize(TILE_SIZE / CURSOR_SIZE)
self.color('grey')
self.penup()
self.speed('fastest')
def setup_maze(level):
''' Conversion from the list to the map in turtle. '''
maze_height,maze_width = len(level),len(level[0])
for y in range(maze_height):
for x in range(maze_width):
# get the character at each x,y coordinate
character = level[y][x]
# check if it is a wall
if character == 'X':
# calculate the screen x,y coordinates
screen_x = (x - maze_width) * TILE_SIZE
screen_y = (maze_width - y) * TILE_SIZE
pen.goto(screen_x,screen_y)
pen.stamp()
screen = Screen()
screen.setup(700,700)
screen.title("PIZZA RUNNERS")
maze = []
with open("map01.txt") as file:
for line in file:
maze.append(line.strip())
pen = Pen()
setup_maze(maze)
screen.mainloop()
如果您正在寻找类似的东西:
然后更改该行:
self.color('grey')
在上面的代码中:
self.color('black','grey')
最后,如果您愿意:
然后,我们需要对上面的代码进行一些小的更改:
class Pen(Turtle):
def __init__(self):
super().__init__()
self.shape('square')
self.shapesize(TILE_SIZE / CURSOR_SIZE)
self.pencolor('black')
self.penup()
self.speed('fastest')
def setup_maze(level):
''' Conversion from the list to the map in turtle. '''
maze_height,y coordinate
character = level[y][x]
# check if it is a wall or a path
pen.fillcolor(['white','grey'][character == 'X'])
# calculate the screen x,y coordinates
screen_x = (x - maze_width) * TILE_SIZE
screen_y = (maze_width - y) * TILE_SIZE
pen.goto(screen_x,screen_y)
pen.stamp()