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

Turtle graphics script keeps crashing when code is run

我正在创建一个加载图像并将其转换为 1 和零的项目,然后使用 turtle 绘制它。然而,每次我 运行 它告诉我它在第一列完成后停止工作。如果问题出在我的计算机的处理能力上,我想知道是否有办法切换到 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()
im = Image.open('D:/MazeSolver/ExampleMazePicture.JPG') # Can be many different formats.
pix = im.load()
size=250
Maze=[]
length=im.size[0]  # Get the width and hight of the Maze for iterating over
for x in range(0,size,8):
    print("Row",x)
    row=[]
    for y in range(0,size,2):
        pix = im.load()
        if pix[x,y]>=(200,200,200):
            node=0
        elif pix[x,y][0]>200 and pix[x,y][2]<200 and pix[x,y][1]<200:
            node=4
            print("End")
        elif pix[x,y][1]>200 and pix[x,y][0]<50 and pix[x,y][2]<50:
            node=5
            print("Start")
        elif pix[x,y]<=(50,50,50):
            node=1
        else:
            print(pix[x,y])
        row.append(node)
    Maze.append([row])
ShowMaze(Maze)

这段代码一团糟。您将名为 Maze 的 JPEG 迷宫图像输入二维数组并将其传递给 ShowMaze(Maze) 以表明您已正确读取它。但是 ShowMaze() 全局访问 Maze 并认为它的论点是 ShowMaze(possibleRoutes) 其中 possibleRoutes 从未计算过迷宫?另外:Maze 的 X 和 Y 意义似乎颠倒了;迷宫的行有一层额外的 list 包裹着它们,没有明显的原因;包含死代码;你读的不是 1 和 0,而是四种不同的颜色代码;绘图代码似乎没有希望。

我修改了你的代码,简单地将迷宫读入列表列表,然后使用 stamping 而不是 drawing[=33] 与海龟一起显示它=] 来简化和加速代码:

from turtle import Screen, Turtle
from PIL import Image

CURSOR_SIZE = 20
PIXEL_SIZE = 5

COLORS = {0: 'white', 1: 'black', 4: 'green', 5: 'red'}

def ShowMaze(maze):
    height, width = len(maze), len(maze[0])

    screen = Screen()
    screen.setup(width * PIXEL_SIZE, height * PIXEL_SIZE)
    screen.setworldcoordinates(0, height, width, 0)

    turtle = Turtle('square', visible=False)
    turtle.shapesize(PIXEL_SIZE / CURSOR_SIZE)
    turtle.penup()

    screen.tracer(False)

    for y in range(height):
        for x in range(width):
            color = maze[y][x]
            if color in COLORS:
                turtle.fillcolor(COLORS[color])
            else:
                turtle.fillcolor("orange")  # error color

            turtle.stamp()
            turtle.forward(1)

        turtle.goto(0, turtle.ycor() + 1)

    screen.tracer(True)
    screen.mainloop()

image = Image.open('ExampleMazePicture.JPG') # Can be many different formats.
width, height = image.size  # Get the width and height of the Maze for iterating over
pixels = image.load()
maze = []

for y in range(0, width, 4):
    print("Row:", y)

    row = []

    for x in range(0, width, 4):

        node = -1
        pixel = pixels[x, y]

        if pixel >= (200, 200, 200):
            node = 0
        elif pixel[0] > 200 and pixel[1] < 200 and pixel[2] < 200:
            node = 4
            print("End")
        elif pixel[0] < 50 and pixel[1] > 200 and pixel[2] < 50:
            node = 5
            print("Start")
        elif pixel <= (50, 50, 50):
            node = 1
        else:
            print(pixel)

        row.append(node)

    maze.append(row)

ShowMaze(maze)

基于使用 "Figure 1.6: Picobot’s maze." from this page 作为输入的输出:

希望这会为您最终尝试开发的程序提供一个起点。