【问题标题】:Is it possible to move to a point without drawing a line using turtle python module?是否可以使用turtle python模块移动到一个点而不画线?
【发布时间】:2021-04-23 05:31:44
【问题描述】:
import turtle

t = turtle.Turtle()
t.setx(0)
t.sety(300)
turtle.clear() #tried turtle.reset() too
for i in range(3):
   t.fd(100)
   t.right(90)
t.goto(0,300)
turtle.done()

所以我一直想知道是否可以移动到一个点或一组轴而不用乌龟画一条线。默认情况下,它设置为 (0,0),但我希望向上移动屏幕,比如点 (0,300),但在移动到该点之前,它会绘制一条不需要的线。我尝试设置坐标,然后使用 clear() 和 reset() 函数,但似乎都没有产生所需的输出。请问有什么提示吗?谢谢。

【问题讨论】:

    标签: python python-3.x turtle-graphics python-turtle


    【解决方案1】:

    使用penup()。本质上,它举起了乌龟的笔。

    import turtle
    
    t = turtle.Turtle()
    t.penup()
    t.goto(0, 300)
    #Now you can set pen down if you wish with turtle.pendown()
    

    【讨论】:

    • 谢谢希德,我很感激。上面的代码有效,但是创建了另一个海龟,它向上移动到 (0,300) 而不画线,而另一个海龟在 (0,0) 处。如果我放下笔并执行一个动作,比如向前移动,(0,300) 处的乌龟是静止的,而 (0,0) 处的乌龟则执行该动作。请问可能是什么问题?
    【解决方案2】:

    您的代码中只有一个小错误:只需将 turtle.clear() 替换为 t.clear() 即可明确清除 t 所做的行:

    import turtle
    
    t = turtle.Turtle()
    t.setx(0)
    t.sety(300)
    t.clear()
    for i in range(3):
       t.fd(100)
       t.right(90)
    t.goto(0,300)
    turtle.done()
    

    此外,您可以在必要时使用t.penup() 方法“抬起”笔,并在必要时使用t.pendown() 将其放回到画布上。

    附注t.penup()也可以是t.pu()t.pendown()可以是t.pd()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2018-12-11
      相关资源
      最近更新 更多