我试图让一条线遵循 Python 中的随机路径。我必须使用SimpleGUI来做到这一点。到目前为止,我已经做了足够多的事情来使该行遵循随机路径,但是在 for 循环之后,代码重新启动。我对 SimpleGUI 不太熟悉,所以我确信代码重新启动是有充分理由的,但我不知道如何修复它。我已经提供了下面的代码,在此先感谢!
import simplegui
import random
def draw_handler(canvas):
x=300 #Should be the center; this is the center of 600x600
y=300
for i in range(1000):
direction=random.randint(1,4) #1=up,2=left,3=down,4=right
if (direction==1):
canvas.draw_line([x,y],[x,y-3],3,"Black")
y=y-3
if (direction==2):
canvas.draw_line([x,y],[x-3,y],3,"Black")
x=x-3
if (direction==3):
canvas.draw_line([x,y],[x,y+3],3,"Black")
y=y+3
if (direction==4):
canvas.draw_line([x,y],[x+3,y],3,"Black")
x=x+3
frame = simplegui.create_frame('Testing', 600, 600)
frame.set_canvas_background("White")
frame.set_draw_handler(draw_handler)
frame.start()