我想用python3和while语句编写一个同心正方形的代码,但是做不到。帮个小忙?谢谢!好的,这是我的尝试:
import turtle
t = turtle.Turtle()
x=0
y=0
z=0
a=-0
b=0
c=-0
d=0
e=0
while x < 100:
t.pendown()
x=x-10
y=y+10
t.goto(x,y)
z=z-10
a=a-10
t.goto(z,a)
b=b+10
c=c-10
t.goto(b,c)
d=d+10
e=e+10
t.goto(d,e)
t.setposition(x,y)
turtle.done()我想花点时间,所以是的.谢谢!
发布于 2020-06-30 06:13:24
你需要使用penup和pendown来停止并开始绘制一条轨迹。
import turtle
t = turtle.Turtle()
x=0
y=0
z=0
a=-0
b=0
c=-0
d=0
e=0
while x < 100:
t.penup()
x=x-10
y=y+10
t.goto(x,y)
t.pendown()
z=z-10
a=a-10
t.goto(z,a)
b=b+10
c=c-10
t.goto(b,c)
d=d+10
e=e+10
t.goto(d,e)
t.setposition(x,y)
turtle.done()发布于 2020-06-30 06:15:43
你就快到了,只需添加一个笔尖:
while x < 100:
t.penup() # <- penup here
x=x-10
y=y+10
t.goto(x,y)
z=z-10
a=a-10
t.pendown() # <- pendown here
t.goto(z,a)
b=b+10
c=c-10
t.goto(b,c)
d=d+10
e=e+10
t.goto(d,e)
t.setposition(x,y)
turtle.done()发布于 2020-06-30 06:34:15
也许是一种更干净的方式:
import turtle
squares = 5 # Choose how many squares
scale = 20 # Chose the scale of the squares
t = turtle.Turtle()
for i in range(squares+1):
for _ in range(4):
t.pendown()
t.forward(scale*i)
t.right(90)
t.penup()
t.setpos(t.xcor()-scale/2,t.ycor()+scale/2)
turtle.done()输出:

https://stackoverflow.com/questions/62647376
复制相似问题