亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Python Turtle模塊中繪制網格線?

如何在Python Turtle模塊中繪制網格線?

冉冉說 2024-01-24 15:18:17
所以目前我正在嘗試通過讀取 .txt 文件然后將其顯示在 Python 的 Turtle 庫中來繪制塊迷宮。目前,我的代碼只能繪制出方框,而不能繪制出方框周圍的網格線。無論如何,有沒有辦法解決這個問題,因為我試圖查看文檔,他們只是建議turtle.Turtle.fillcolor,這似乎并不正確。這是我當前的代碼:# import the necessary libraryimport 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 titlebarwindow.setup(700,700)# create penclass Pen(turtle.Turtle):    def __init__(self):        turtle.Turtle.__init__(self)        self.shape("square")        self.color('grey')        self.penup()        self.speed(0)# create maze_listmaze = []# add the maze to mazemaze.append(content)# create conversion from the list to the map in turtledef 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 mazesetup_maze(maze[0])# main game loopwhile True:    pass這就是我正在讀取的當前文本文件的樣子:XXXXXXXXXXXXX.........eXX.XXX.XXX..XX.XsX.X.XX.XX.X......X.XX.XXXXXXXX.XX..........XXXXXXXXXXXXX's'和'e'應該代表起點和終點,尚未實現,因此可以忽略。點代表路徑,X 代表墻壁。當前的地圖(或txt)尺寸為8行x 12列?,F在我的輸出如下所示:我希望它看起來像這樣(指的是網格的添加,而不是相同的圖案迷宮):
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

假設你想要的是:

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

然后我們需要將方形光標的大小從默認大小 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()

如果您正在尋找更像這樣的東西:

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

然后更改行:

self.color('grey')

在上面的代碼中:

self.color('black', 'grey')

最后,如果你愿意:

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

然后我們需要對上面的代碼做一些小修改:


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()


查看完整回答
反對 回復 2024-01-24
  • 1 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號