gpt4 book ai didi

python - turtle 图形脚本在代码运行时不断崩溃

转载 作者:行者123 更新时间:2023-12-01 08:21:45 25 4
gpt4 key购买 nike

我正在创建一个项目,加载图像并将其转换为 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)

最佳答案

这段代码一团糟。您将名为 Maze 的 JPEG 迷宫图像输入到二维数组中,并将其传递给 ShowMaze(Maze) 以表明您已正确读取它。但是 ShowMaze() 全局访问 Maze 并认为它的参数是 ShowMaze(possibleRoutes),其中通过迷宫的 possibleRoutes 是从来没有计算过?另外:Maze 的 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)

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

enter image description here

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

关于python - turtle 图形脚本在代码运行时不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54599912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com