如何在 Python Turtle 模块中绘制网格线?
How to plot out grid lines in Python Turtle module?
所以目前我正在尝试通过读取 .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 列。
现在我的输出是这样的:
我希望它看起来像这样(指的是添加网格,而不是相同的图案迷宫):
假设你想要的是:
然后我们需要将方形光标的大小从其默认大小 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, 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 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()
所以目前我正在尝试通过读取 .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 列。
现在我的输出是这样的:
我希望它看起来像这样(指的是添加网格,而不是相同的图案迷宫):
假设你想要的是:
然后我们需要将方形光标的大小从其默认大小 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, 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 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()