【问题标题】:How to make the turtle draw one step at a time如何让乌龟一步一步画
【发布时间】:2014-04-24 02:42:38
【问题描述】:

我有一个刽子手游戏,我想每写错一个字母就画出刽子手的一部分,但我不知道如何一次画出一只乌龟的一步

这就是我所拥有的:

def drawsturtle(userInput,word):

那么我必须做的步骤:

import turtle
hangman = turtle.Turtle()
hangman.circle(45)
hangman.right(90)
hangman.forward(150)
....

我如何编写代码,以便绘制不是这些步骤之一的每个用户输入? 谢谢大家

【问题讨论】:

    标签: python function turtle-graphics


    【解决方案1】:

    如果您定义了一个计数变量来跟踪错误猜测的数量,您可以定义一个函数来提取所需的部分。在下面的示例中,我假设了 3 个错误的猜测。我添加了一个 3 秒的暂停,以便您可以看到输出。

    import turtle, time
    
    hangman = turtle.Turtle()
    
    def draw_hangman(count):
        if count >= 1:
            hangman.circle(45)
        if count >= 2:   
            hangman.right(90)
        if count == 3:
            hangman.forward(150)
        time.sleep(3)
    
    draw_hangman(3)
    

    【讨论】:

      【解决方案2】:

      一旦你有代码检测到一个不正确的字母被猜到,你可以调用turtle.write方法写出不正确的字母。

      参考下面的方法签名:

       turtle.write(arg, move=False, align="left", font=("Arial", 8, "normal"))
          Parameters: 
      
              arg – object to be written to the TurtleScreen
              move – True/False
              align – one of the strings “left”, “center” or right”
              font – a triple (fontname, fontsize, fonttype)
      
          Write text - the string representation of arg - at the current turtle position according to align (“left”, “center” or right”) and with the given font. If move is true, the pen is moved to the bottom-right corner of the text. By default, move is False.
      

      更多详情:

      https://docs.python.org/2.7/library/turtle.html

      【讨论】:

      • 我想画一个刽子手的一部分,每次它猜错一个字母,当他猜错字母时我得到了,但不能让乌龟只画一个像头或手臂这样的部分。 .
      猜你喜欢
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2021-01-12
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多