编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
import turtle
import random
import time

def move():
    t.forward(30)
def 画正方形(i):
    x,y=t.position()
    x+=i
    y+=i
    new_x=x
    new_y=y
    x+=30
    t.setx(x)
    y+=30
    t.sety(y)
    x-=30
    t.setx(x)
    y-=40
    t.sety(y)
new_x=0
new_y=0
i=0
wn=turtle.Screen()
wn.setup=(random.randint(600,800),random.randint(700,900))
t=turtle.Turtle()
t.color('black')
t.width(1)
t.speed(10)
t.goto(0,0)
t.hideturtle()
turtle.listen()

turtle.onkey(move,'k')

for i in range(100):
    if new_x<=-600:
        i=-100
    画正方形(i)
    time.sleep(0.5)
    t.clear()
    
turtle.mainloop()