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

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

運行代碼時,Turtle 圖形腳本不斷崩潰

運行代碼時,Turtle 圖形腳本不斷崩潰

繁花不似錦 2021-11-02 20:09:41
我正在創建一個加載圖像并將其轉換為 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()
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

這段代碼一團糟。您將一個名為 的 JPEG 迷宮圖像輸入Maze到一個二維數組中并將其傳遞給ShowMaze(Maze)以表明您已正確讀取它。但是全局ShowMaze()訪問Maze并認為它的論點是從未計算過迷宮中的ShowMaze(possibleRoutes)哪個地方possibleRoutes?另外:X 和 Y 的意義Maze似乎顛倒了;迷宮的行list無緣無故地有一層額外的包裹;包含死代碼;你不是把它讀成 1 和 0,而是四種不同的顏色代碼;繪圖代碼似乎沒有希望。


我已經重新編寫了您的代碼,只需將迷宮讀入列表列表,然后使用標記而不是繪圖將其與烏龜一起顯示,以簡化和加速代碼:


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)

基于使用“圖 1.6:Picobot 的迷宮”的輸出。從此頁面作為輸入:

http://img1.sycdn.imooc.com//61812aa5000199b304830497.jpg

希望這能為您最終嘗試開發的程序提供一個起點。


查看完整回答
反對 回復 2021-11-02
  • 1 回答
  • 0 關注
  • 315 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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