如何让乌龟一步一个脚印地画出来

2024-06-26 00:23:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这就是我所拥有的:

def drawsturtle(userInput,word):

然后我要做的步骤是:

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

如何编写代码,以便绘制每个用户输入(不是word中的输入)的其中一个步骤? 谢谢大家


Tags: importright游戏def字母步骤wordforward
2条回答

如果您定义了一个count变量来跟踪错误猜测的数量,那么您可以定义一个函数来提取所需的部分。在下面的例子中,我假设了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)

一旦有代码检测到猜测的字母不正确,就可以调用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

相关问题 更多 >