【问题标题】:Pick up pen when turtle is not at coordinate乌龟不在坐标时拿起笔
【发布时间】:2019-07-05 05:54:45
【问题描述】:

我的程序绘制的图像已经附加了坐标。我希望我的乌龟不在坐标处时能够拿起笔。现在乌龟在到达坐标之前继续写。

代码:

with open('output.txt', 'r') as f:
    data = ast.literal_eval(f.read())

tony = turtle.Turtle()

for z in data:
    position = tony.pos()
    tony.goto(z)

输出

1:

如您所见,乌龟甚至在到达坐标之前就继续绘制。

这是我认为可能可行但我不确定如何实施的方法。

for z in data:
     position = tony.pos()
     while position in z == False:
         tony.penup()

for z in data:
     position = tony.pos()
     while position in z == True:
        tony.pendown()
        print("True")

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

    我创建了一个函数来检测海龟的位置是否在坐标列表中。然后使用ontimer 函数每毫秒调用一次该函数。我还必须放慢我的海龟速度,以便程序在一毫秒内检查位置

    代码:

    tony = turtle.Turtle()
    tony.color("white", "cyan")
    tony.speed(5.5)
    
    def on_canvas():
        position = tony.pos()
        if position in data:
            tony.pendown()
            print("This is a coordinate")
        else:
            tony.penup()
            print("This is not a coordinate")
    
    
    for z in data:
        playground.ontimer(on_canvas, 1)
        tony.goto(z)
    
    turtle.done()
    
    

    【讨论】:

      【解决方案2】:

      也许尝试在移动之前拿起笔并在之后放下它:

      with open('output.txt', 'r') as f:
          data = ast.literal_eval(f.read())
      
      tony = turtle.Turtle()
      
      for z in data:
          tony.penup()
          tony.goto(z)
          tony.pendown()
      

      【讨论】:

      • 也许可以尝试添加信号以拾取并放下笔或尝试计算距离并在阈值内拿起笔
      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 2017-08-10
      • 2021-11-09
      • 2015-01-30
      • 2021-09-26
      • 2013-07-25
      • 2017-08-09
      • 2023-03-28
      相关资源
      最近更新 更多