运行代码时,Turtle 图形脚本不断崩溃

我正在创建一个加载图像并将其转换为 1 和零的项目,然后它将使用乌龟绘制它。但是,每次我运行它时,它都会告诉我在第一列完成后它已停止工作。如果问题出在我电脑的处理能力上,我想知道是否有办法切换到 GPU 来完成任务。任何帮助将不胜感激。谢谢


def ShowMaze(possibleRoutes):

    turtle.delay(0)

    for x in range(0,len(Maze)):

        for y in range(0,len(Maze[0])):

            if Maze[x][y]==3:

                Maze[x][y]=0


    for x in range(0,len(Maze)):

        turtle.forward(-5)

        turtle.right(90)

        turtle.forward(5/len(Maze[0]))

        turtle.left(90)

        for y in range(0,len(Maze[0])):

            if Maze[x][y]==1:

                turtle.fillcolor("black")

                turtle.begin_fill()

            elif Maze[x][y]==0:

                turtle.fillcolor("white")

                turtle.begin_fill()

            elif Maze[x][y]==4:

                turtle.fillcolor("green")

                turtle.begin_fill()

            elif Maze[x][y]==5:

                turtle.fillcolor("red")

                turtle.begin_fill()


            for i in range(0,4):

                turtle.forward(5/len(Maze[0]))

                turtle.left(90)


            turtle.end_fill()

            turtle.forward(5/len(Maze[0]))

    input()

    for ii in range(1,len(possibleRoutes)-1):

        turtle.pu()

        turtle.home()

        turtle.forward(-250)

        turtle.forward((250/len(Maze))*possibleRoutes[ii][1])

        turtle.right(90)

        turtle.forward((250/len(Maze))*possibleRoutes[ii][0]+(250/len(Maze)))

        turtle.left(90)

        turtle.fillcolor("blue")

        turtle.pd()

        turtle.begin_fill()

        for x in range(0,4):

            turtle.forward(250/len(Maze[0]))

            turtle.left(90)

        turtle.end_fill()


繁花不似锦
浏览 268回答 1
1回答

慕妹3242003

这段代码一团糟。您将一个名为 的 JPEG 迷宫图像输入Maze到一个二维数组中并将其传递给ShowMaze(Maze)以表明您已正确读取它。但是全局ShowMaze()访问Maze并认为它的论点是从未计算过迷宫中的ShowMaze(possibleRoutes)哪个地方possibleRoutes?另外:X 和 Y 的意义Maze似乎颠倒了;迷宫的行list无缘无故地有一层额外的包裹;包含死代码;你不是把它读成 1 和 0,而是四种不同的颜色代码;绘图代码似乎没有希望。我已经重新编写了您的代码,只需将迷宫读入列表列表,然后使用标记而不是绘图将其与乌龟一起显示,以简化和加速代码:from turtle import Screen, Turtlefrom PIL import ImageCURSOR_SIZE = 20PIXEL_SIZE = 5COLORS = {0: 'white', 1: 'black', 4: 'green', 5: 'red'}def ShowMaze(maze):&nbsp; &nbsp; height, width = len(maze), len(maze[0])&nbsp; &nbsp; screen = Screen()&nbsp; &nbsp; screen.setup(width * PIXEL_SIZE, height * PIXEL_SIZE)&nbsp; &nbsp; screen.setworldcoordinates(0, height, width, 0)&nbsp; &nbsp; turtle = Turtle('square', visible=False)&nbsp; &nbsp; turtle.shapesize(PIXEL_SIZE / CURSOR_SIZE)&nbsp; &nbsp; turtle.penup()&nbsp; &nbsp; screen.tracer(False)&nbsp; &nbsp; for y in range(height):&nbsp; &nbsp; &nbsp; &nbsp; for x in range(width):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color = maze[y][x]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if color in COLORS:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turtle.fillcolor(COLORS[color])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turtle.fillcolor("orange")&nbsp; # error color&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turtle.stamp()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turtle.forward(1)&nbsp; &nbsp; &nbsp; &nbsp; turtle.goto(0, turtle.ycor() + 1)&nbsp; &nbsp; screen.tracer(True)&nbsp; &nbsp; screen.mainloop()image = Image.open('ExampleMazePicture.JPG') # Can be many different formats.width, height = image.size&nbsp; # Get the width and height of the Maze for iterating overpixels = image.load()maze = []for y in range(0, width, 4):&nbsp; &nbsp; print("Row:", y)&nbsp; &nbsp; row = []&nbsp; &nbsp; for x in range(0, width, 4):&nbsp; &nbsp; &nbsp; &nbsp; node = -1&nbsp; &nbsp; &nbsp; &nbsp; pixel = pixels[x, y]&nbsp; &nbsp; &nbsp; &nbsp; if pixel >= (200, 200, 200):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node = 0&nbsp; &nbsp; &nbsp; &nbsp; elif pixel[0] > 200 and pixel[1] < 200 and pixel[2] < 200:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node = 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("End")&nbsp; &nbsp; &nbsp; &nbsp; elif pixel[0] < 50 and pixel[1] > 200 and pixel[2] < 50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node = 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Start")&nbsp; &nbsp; &nbsp; &nbsp; elif pixel <= (50, 50, 50):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node = 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(pixel)&nbsp; &nbsp; &nbsp; &nbsp; row.append(node)&nbsp; &nbsp; maze.append(row)ShowMaze(maze)基于使用“图 1.6:Picobot 的迷宫”的输出。从此页面作为输入:希望这能为您最终尝试开发的程序提供一个起点。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python