[英]drawing an X with python Turtle graphics
I have a really simple task to draw an X in python using Turtles, but i cannot seem to understand how the setposition() method works.我有一个非常简单的任务来使用 Turtles 在 python 中绘制一个 X,但我似乎无法理解 setposition() 方法是如何工作的。
my code currently draws the first line correct, but then the second line skews up too much no matter what i try.我的代码当前绘制的第一行是正确的,但是无论我尝试什么,第二行都会倾斜太多。
t.right(45)
t.pendown()
t.setposition(50,-50)
t.penup()
t.left(90)
t.setposition(0,-50)
t.pendown()
t.setposition(50,50)
Your first line starts at (0,0)
and got to (50, -50)
, thus goes 50 units in X and Y direction.你的第一行从(0,0)
开始到(50, -50)
,因此在X和Y方向上有 50 个单位。 But you second ist going from (0,-50)
to (50,50)
, thus goes 50 units in X direction and 100 units in Y direction.但是你第二个是从(0,-50)
到(50,50)
,因此在X方向上有 50个单位,在Y方向上有100 个单位。 Additionally your right and left have no effect.此外,您的左右手没有任何影响。
One possible solution would be:一种可能的解决方案是:
t.setposition(-50,50)
t.pendown()
t.setposition(50,-50)
t.penup()
t.setposition(-50,-50)
t.pendown()
t.setposition(50,50)
Another approach you can take is to avoid setposition()
altogether and think like a turtle.您可以采取的另一种方法是完全避免setposition()
并像乌龟一样思考。 That is, crawl forward, backward and turn rather than teleport:也就是说,向前、向后和转弯而不是传送:
import turtle as t
t.right(45)
t.forward(70)
t.backward(140)
t.forward(70)
t.left(90)
t.forward(70)
t.backward(140)
t.hideturtle()
t.done()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.