如何在Python Turtle模块中绘制网格线?

所以目前我正在尝试通过读取 .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列。


现在我的输出如下所示:

https://img1.sycdn.imooc.com/65b0b9e300013d4806540685.jpg

我希望它看起来像这样(指的是网格的添加,而不是相同的图案迷宫):

https://img1.sycdn.imooc.com/65b0b9f300014e7302260158.jpg


冉冉说
浏览 41回答 1
1回答

慕沐林林

假设你想要的是:然后我们需要将方形光标的大小从默认大小 20 调整为图块大小 24:from turtle import Screen, TurtleTILE_SIZE = 24CURSOR_SIZE = 20class 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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python