如何让乌龟一次画一步

时间:2014-04-24 02:42:38

标签: python function turtle-graphics

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

这就是我所拥有的:

def drawsturtle(userInput,word):

然后我要做的步骤:

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

如何编写代码,以便每个userInput不在word中的哪一步中被绘制? 谢谢大家

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

一旦有代码检测到猜到错误的字母,您可以调用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

相关问题