Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 海龟图形脚本在代码运行时不断崩溃_Python_Turtle Graphics - Fatal编程技术网

Python 海龟图形脚本在代码运行时不断崩溃

Python 海龟图形脚本在代码运行时不断崩溃,python,turtle-graphics,Python,Turtle Graphics,我正在创建一个项目,加载一个图像并将其转换为1和0,然后使用turtle绘制该图像。但是,每次我运行它时,它都会告诉我,它在第一列完成后已停止工作。如果问题是我的计算机的处理能力,我想知道是否有办法切换到GPU来完成这项任务。任何帮助都将不胜感激。谢谢 def ShowMaze(possibleRoutes): turtle.delay(0) for x in range(0,len(Maze)): for y in range(0,len(Maze[0])):

我正在创建一个项目,加载一个图像并将其转换为1和0,然后使用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)
def显示迷宫(可能的出口):
乌龟。延迟(0)
对于范围(0,len(迷宫))内的x:
对于范围(0,len(迷宫[0])中的y:
如果迷宫[x][y]==3:
迷宫[x][y]=0
对于范围(0,len(迷宫))内的x:
乌龟。前进(-5)
乌龟,对(90)
海龟前进(5/len(迷宫[0]))
乌龟。左(90)
对于范围(0,len(迷宫[0])中的y:
如果迷宫[x][y]==1:
乌龟。黑色(“黑色”)
乌龟,开始填充
elif迷宫[x][y]==0:
乌龟。填充颜色(“白色”)
乌龟,开始填充
elif迷宫[x][y]==4:
海龟。填充颜色(“绿色”)
乌龟,开始填充
elif迷宫[x][y]==5:
海龟。颜色(“红色”)
乌龟,开始填充
对于范围(0,4)内的i:
海龟前进(5/len(迷宫[0]))
乌龟。左(90)
乌龟
海龟前进(5/len(迷宫[0]))
输入()
对于范围内的ii(1,len(可能的出口)-1):
乌龟
海龟之家
乌龟。前进(-250)
乌龟前进((250/列(迷宫))*可能出[ii][1])
乌龟,对(90)
海龟前进((250/len(迷宫))*可能出[ii][0]+(250/len(迷宫)))
乌龟。左(90)
海龟。填充颜色(“蓝色”)
(d)
乌龟,开始填充
对于范围(0,4)内的x:
乌龟前进(250/列(迷宫[0]))
乌龟。左(90)
乌龟
im=Image.open('D:/MazeSolver/ExampleMazePicture.JPG')#可以有许多不同的格式。
pix=im.load()
尺寸=250
迷宫=[]
length=im.size[0]#获取迷宫的宽度和高度以进行迭代
对于范围内的x(0,大小,8):
打印(“行”,x)
行=[]
对于范围内的y(0,大小,2):
pix=im.load()
如果pix[x,y]>=(200200):
节点=0

elif pix[x,y][0]>200和pix[x,y][2]这段代码乱七八糟。您将一个名为
maze
的JPEG迷宫图像输入二维数组,并将其传递给
ShowMaze(maze)
,以显示您已正确读取该图像。但是
ShowMaze()
全局访问
Maze
,并认为它的参数是
ShowMaze(possibleRoutes)
其中从未计算通过迷宫的
possibleRoutes
?另外:
迷宫的X和Y方向感觉似乎颠倒了;迷宫的每一行都有一层额外的
list
包裹着它们,没有明显的原因;包括死代码;你不是把它读成1和0,而是四种不同的颜色代码;绘图代码似乎没有希望了

我修改了您的代码,只需将迷宫读入列表,然后使用图钉(而不是绘图)将其显示为turtle,以简化和加速代码:

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)
从海龟导入屏幕,海龟
从PIL导入图像
光标大小=20
像素大小=5
颜色={0:'白色',1:'黑色',4:'绿色',5:'红色'}
def显示迷宫(迷宫):
高度,宽度=透镜(迷宫),透镜(迷宫[0])
screen=screen()
屏幕设置(宽度*像素大小,高度*像素大小)
屏幕。设置世界坐标(0,高度,宽度,0)
海龟=海龟('square',可见=假)
turtle.shapesize(像素大小/光标大小)
乌龟
屏幕跟踪(假)
对于范围内的y(高度):
对于范围内的x(宽度):
颜色=迷宫[y][x]
如果颜色为彩色:
turtle.fillcolor(颜色[颜色])
其他:
海龟。填充颜色(“橙色”)#错误颜色
乌龟邮票()
乌龟前进(1)
turtle.goto(0,turtle.ycor()+1)
屏幕跟踪(真)
screen.mainloop()
image=image.open('ExampleMazePicture.JPG')#可以有许多不同的格式。
宽度,高度=image.size#获取迷宫的宽度和高度以进行迭代
像素=image.load()
迷宫=[]
对于范围(0,宽度,4)内的y:
打印(“行:”,y)
行=[]
对于范围内的x(0,宽度,4):
节点=-1
像素=像素[x,y]
如果像素>=(200200200):
节点=0
elif像素[0]>200和像素[1]<200和像素[2]<200:
节点=4
打印(“结束”)
elif像素[0]<50和像素[1]>200和像素[2]<50:
节点=5
打印(“开始”)

我无法重现这个问题。您确定您的
ExampleMazePicture.JPG
文件包含彩色图像吗?我这样问是因为您的代码假设它正在处理一个,当它执行类似于
if pix[x,y]>=(200200):
的操作时。还尝试从命令行运行脚本,并注意显示的任何错误消息。非常感谢您的帮助。