【问题标题】:Draw Letters In Turtle在乌龟上画字母
【发布时间】:2017-04-28 21:52:15
【问题描述】:

我刚开始在 python 中学习乌龟。 我试图写我的名字“Idan”(大写字母),但我被困在“D”上。

这是我目前的代码:

import turtle

t = turtle.Turtle()

t.up()
t.backward(400)
t.left(90)
t.down()
t.forward(250)
t.right(90)
t.forward(62.5)
t.backward(62.5 * 2)
t.forward(62.5)
t.right(90)
t.forward(250)
t.right(90)
t.forward(62.5)
t.backward(62.5 * 2)
t.up()
t.backward(100)
t.right(90)
t.down()
t.forward(250)
for x in range(180):
    t.forward(1)
    t.right(1)

但是当我运行它时,它会给我一个小半圈。

有没有办法让它做一个半圆来创建一个“D”

【问题讨论】:

标签: python python-2.7 turtle-graphics


【解决方案1】:

您遇到的主要问题是,当乌龟面朝上而不是向右时,您的半圆开始了。在 for 循环之前添加一个 t.right(90) 调用,你会更接近你想要的(你会得到类似 P 的东西)。

之后,您需要调整循环内的 forward 调用,以使圆达到您想要的大小(或者按照评论中的 martineau 建议,使用 t.circle 而不是您自己的循环)。您可能需要进行一些计算来确定正确的距离(例如,将所需的直径乘以 math.pi 以得到周长,然后除以您用于构造它的线段数)。

【讨论】:

    【解决方案2】:

    不完美,尝试和错误,但它写了名字:

    import turtle
    turtle.setup(900,400)
    wn = turtle.Screen()
    wn.title("Turtle writing my name: IDAN")
    wn.bgcolor("lightgreen")
    turtle.width(30)
    # turtle.pensize(30)
    
    t = turtle.Turtle()
    t.up()
    t.backward(360)
    t.left(90)
    t.backward(80)
    t.down()
    t.forward(250)
    t.right(90)
    t.forward(62.5)
    t.backward(62.5 * 2)
    t.forward(62.5)
    t.right(90)
    t.forward(250)
    t.right(90)
    t.forward(62.5)
    t.backward(62.5 * 2)
    t.up()
    t.backward(100)
    t.right(90)
    t.down()
    t.forward(250)
    t.right(90)
    for x in range(180):
    

    . t.forward(2.2) # adjust HERE to get different size of circle

        t.right(1)
    t.up()
    t.right(90)
    t.forward(230)
    t.right(90)
    t.forward(4*62.5)
    t.right(65)
    t.down()
    t.forward(250)
    t.right(180)
    t.forward(275)
    t.left(135)
    t.forward(250)
    t.right(180)
    t.forward(120)
    t.right(70)
    t.forward(110)
    t.right(90)
    t.up()
    t.forward(115)
    t.left(90)
    t.forward(1.5*62.5)
    t.left(90)
    t.down()
    t.forward(250)
    t.right(145)
    t.forward(320)
    t.left(145)
    t.forward(250)
    
    wn.mainloop()
    

    【讨论】:

      【解决方案3】:

      在解决这样的问题时,我会提出以下建议:

      • 提前仔细规划您的块字体,如果需要,在纸上。这 设计越简单,编码就越容易。

      • 在你的代码中逻辑地分隔你的字母——如果不是函数的话 此时,至少分成单独的注释块。

      • 让每个字母都以海龟指向同一个方向开始 并根据需要进行调整,不要指望先前的结果状态 信。

      • 使用虚拟坐标系来简化您的绘图逻辑和 允许字母出现在不同大小的窗口中。如果没有 像setworldcoordinates() 这样的函数然后简单地通过乘法器 高度和宽度。

      • 在您的设计中假设您以后想要添加更多字母。

      • 考虑一下如果你想拼写“DIAN”会发生什么—— 您需要更改多少代码以及如何将其保持在 最低限度。

      我下面的示例使用了您可能还没有学过的功能,并且不打算作为您使用的解决方案——它旨在说明上述想法。 IE。尝试更改窗口大小、边框大小、宽度和高度、重新排列字母等,看看结果如何变化但仍然有效:

      from turtle import Turtle, Screen
      
      NAME = "IDAN"
      
      BORDER = 1
      BOX_WIDTH, BOX_HEIGHT = 6, 10  # letter bounding box
      WIDTH, HEIGHT = BOX_WIDTH - BORDER * 2, BOX_HEIGHT - BORDER * 2  # letter size
      
      def letter_A(turtle):
          turtle.forward(HEIGHT / 2)
          for _ in range(3):
              turtle.forward(HEIGHT / 2)
              turtle.right(90)
              turtle.forward(WIDTH)
              turtle.right(90)
          turtle.forward(HEIGHT)
      
      def letter_D(turtle):
          turtle.forward(HEIGHT)
          turtle.right(90)
          turtle.circle(-HEIGHT / 2, 180, 30)
      
      def letter_I(turtle):
          turtle.right(90)
          turtle.forward(WIDTH)
          turtle.backward(WIDTH / 2)
          turtle.left(90)
          turtle.forward(HEIGHT)
          turtle.right(90)
          turtle.backward(WIDTH / 2)
          turtle.forward(WIDTH)
      
      def letter_N(turtle):
          turtle.forward(HEIGHT)
          turtle.goto(turtle.xcor() + WIDTH, BORDER)
          turtle.forward(HEIGHT)
      
      LETTERS = {'A': letter_A, 'D': letter_D, 'I': letter_I, 'N': letter_N}
      
      wn = Screen()
      wn.setup(800, 400)  # arbitrary
      wn.title("Turtle writing my name: {}".format(NAME))
      wn.setworldcoordinates(0, 0, BOX_WIDTH * len(NAME), BOX_HEIGHT)
      
      marker = Turtle()
      
      for counter, letter in enumerate(NAME):
          marker.penup()
          marker.goto(counter * BOX_WIDTH + BORDER, BORDER)
          marker.setheading(90)
      
          if letter in LETTERS:
              marker.pendown()
              LETTERS[letter](marker)
      
      marker.hideturtle()
      
      wn.mainloop()
      

      输出

      【讨论】:

        【解决方案4】:

        这可能有效: 它对我有用

        import turtle as t    
        
        def letterD():
            t.pendown()
            t.setheading(90)
            t.forward(108)
            t.setheading(0)
            for i in range(4):
                t.forward(20)
                t.right(30)
            t.setheading(-90)
            t.forward(13)
            for i in range(4):
                t.forward(20)
                t.right(30)
            t.setheading(0)
            t.penup()
            t.forward(65)
        

        【讨论】:

          【解决方案5】:

          例子:

          import turtle
          
          t = turtle.Pen()
          t.circle(50, 180)
          
          # this draws a 180 degree, 50 radius circle
          

          【讨论】:

            【解决方案6】:

            我使用 Google Colab,所以这是该程序的代码

            !pip3 install ColabTurtle
            
            import ColabTurtle.Turtle as l    
            l.initializeTurtle()    
            l.width(6)    
            l.penup()    
            l.right(270)    
            l.forward(300)    
            l.right(180)    
            l.forward(100)    
            l.left(180)    
            l.pendown()    
            l.forward(100)    
            l.right(90)    
            l.forward(150)    
            l.penup()    
            l.right(90)    
            l.forward(130)    
            l.pendown()    
            l.right(90)    
            l.forward(150)    
            l.left(90)    
            l.forward(100)    
            l.left(90)    
            l.forward(150)    
            l.penup()    
            l.right(90)    
            l.forward(30)    
            l.right(45)    
            l.pendown()    
            l.forward(200)   
            l.penup()    
            l.left(45)    
            l.left(90)    
            l.forward(150)    
            l.left(90)    
            l.left(45)    
            l.pendown()    
            l.forward(200)    
            l.penup()    
            l.left(45)    
            l.left(90)    
            l.forward(200)    
            l.left(90)
            

            输出

            【讨论】:

              【解决方案7】:

              您可以改用此代码:

              import turtle
              turtle = turtle.Turtle()
              turtle.write("IDAN", font=("Arial", 32, "normal"))
              

              【讨论】:

              • 你能解释一下这个写命令是如何画半圆的吗?
              • 此代码将写入文本“IDAN”。但是,您不能单独使用此代码制作半圆。你也可以用你想写的任何单词或句子替换“IDAN”,但它只能在键盘上写字母、数字或符号
              猜你喜欢
              • 1970-01-01
              • 2013-10-30
              • 2022-01-20
              • 2020-10-13
              • 1970-01-01
              • 1970-01-01
              • 2016-11-27
              • 2017-08-09
              • 2023-03-28
              相关资源
              最近更新 更多