【问题标题】:Is there a way to get turtles to move at specific speeds (not integer numbers)?有没有办法让海龟以特定的速度(不是整数)移动?
【发布时间】:2020-06-07 02:17:34
【问题描述】:

我正在做一个项目,其中不同的海龟围绕椭圆形轨道比赛,但我将它们与在轨道上移动所需的实际世界纪录时间进行比较。因此,我正在寻找一种方法让海龟以更特定的速度移动。我已经完成了turtle.speed(),但它只需要整数值。也许是一种让他们每秒移动一定数量的像素的方法?谢谢!

【问题讨论】:

    标签: python turtle-graphics python-turtle


    【解决方案1】:

    我们可以关闭动画并使用定时器事件来控制海龟的速度。在下面的示例中,海龟以大约每秒 100 个像素的速度移动(即每 50 毫秒移动 5 个像素):

    from turtle import Screen, Turtle
    
    screen = Screen()
    screen.tracer(False)
    
    turtle = Turtle('turtle')
    
    def move():
        if turtle.xcor() < screen.window_width()/2:
            turtle.forward(5)  # move forward 5 pixels
            screen.update()
            screen.ontimer(move, 50)  # repeat in 50 milliseconds
    
    move()
    
    screen.exitonclick()
    

    可以对其他海龟进行编程,使其在同一程序中以不同的速度移动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2023-02-17
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      相关资源
      最近更新 更多