【问题标题】:Re-Running a turtle program重新运行一个海龟程序
【发布时间】:2021-06-22 13:25:42
【问题描述】:

我制作了一个简单的 python 程序,它使用 turtle 模块创建随机生成的螺旋线。我对python还是很陌生,我想知道在乌龟离中心很远并且螺旋完成后如何重新启动程序。这是我的代码:

import turtle
import random

root = turtle.Screen()
turtle = turtle.Turtle()

colors = ["yellow", "gold", "orange", "red", "maroon", "violet", "magenta", "purple", "navy", "blue", "skyblue", "cyan", "turquoise", "lightgreen", "green", "darkgreen", "chocolate", "brown", "gray", "white"]

def spiral():
    endColors = [random.choice(colors), random.choice(colors), random.choice(colors)]
    angle = random.randint(60, 300)
    distance = turtle.distance(0, 0)
    
    print("Colors used are:", endColors)
    print("The turning angle is: ", angle, "deg")
    print("The distance is: ", distance)

    turtle.speed(0)
    turtle.hideturtle()
    root.bgcolor("black")
    for i in range(2000):
        turtle.forward(i)
        turtle.right(angle)
        turtle.color(endColors[i%3])
    root.mainloop()
spiral()

我曾尝试在 if 语句中使用 turtle.distance 函数,但它不起作用。

【问题讨论】:

    标签: python random turtle-graphics python-turtle


    【解决方案1】:

    您可以通过使用全局标志变量并安装一个海龟timer 来执行此操作,该海龟调用一个函数,该函数通过查看变量定期检查螺旋是否已完成绘制——如果是,则重置事物并画另一个。在下面的代码中,执行此操作的函数名为check_status()。我还在每个完成后添加了一个短暂的停顿,以使其保持可见足够长的时间以供欣赏。 ;¬)

    import random
    import time
    import turtle
    
    root = turtle.Screen()
    turtle = turtle.Turtle()
    
    colors = ["yellow", "gold", "orange", "red", "maroon", "violet", "magenta",
              "purple", "navy", "blue", "skyblue", "cyan", "turquoise", "lightgreen",
              "green", "darkgreen", "chocolate", "brown", "gray", "white"]
    
    MAX_DIST = 100
    PAUSE = 2  # Secs
    
    
    def spiral():
        global spiral_done
        spiral_done = False  # Global flag.
    
        endColors = [random.choice(colors), random.choice(colors), random.choice(colors)]
        angle = random.randint(60, 300)
    
        print("Colors used are:", endColors)
        print("The turning angle is: ", angle, "deg")
    
        turtle.speed(0)
        turtle.hideturtle()
        root.bgcolor("black")
        for i in range(2000):
            turtle.forward(i)
            turtle.right(angle)
            turtle.color(endColors[i%3])
            if turtle.distance(0, 0) > MAX_DIST:  # Stop if far away.
                break
    
        time.sleep(PAUSE)
        spiral_done = True
    
    
    def check_status():
        if spiral_done:
            root.reset()
            spiral()  # Draw another.
        root.ontimer(check_status, 250)  # Check every 1/4 second.
    
    
    def main():
        global spiral_done
        spiral_done = True  # Initialize global flag.
        check_status()  # Start watching its value.
        spiral()
        root.mainloop()
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

      【解决方案2】:

      我的解决方案与@martineau 的类似,只是我使用turtle.reset() 而不是screen.reset() 和更简单的时序逻辑:

      from turtle import Screen, Turtle
      from random import choice, randint
      from itertools import count
      
      COLORS = [
          'yellow', 'gold', 'orange', 'red', 'maroon',
          'violet', 'magenta', 'purple', 'navy', 'blue',
          'skyblue', 'cyan', 'turquoise', 'lightgreen', 'green',
          'darkgreen', 'chocolate', 'brown', 'gray', 'white'
      ]
      
      RADIUS = 100
      
      def spiral():
          turtle.hideturtle()
          turtle.speed('fastest')
      
          triColors = [choice(COLORS), choice(COLORS), choice(COLORS)]
          angle = randint(60, 300)
      
          print("Colors used are:", triColors)
          print("The turning angle is: ", angle, "deg")
          print("The radius is: ", RADIUS)
      
          for distance in count():  # spiral forever until a specific radius is achieved
              turtle.pencolor(triColors[distance % 3])
              turtle.forward(distance)
      
              if turtle.distance(0, 0) > RADIUS:
                  break
      
              turtle.right(angle)
      
          turtle.reset()
          screen.ontimer(spiral)
      
      screen = Screen()
      screen.bgcolor('black')
      
      turtle = Turtle()
      
      spiral()
      
      screen.mainloop()
      

      【讨论】:

        猜你喜欢
        • 2012-05-17
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        • 2023-01-25
        相关资源
        最近更新 更多