我有一个子手游戏,我想每把错误的字母都画一个子手,但我不知道如何一次画一只乌龟。
这就是我所拥有的:
def drawsturtle(userInput,word):
然后我必须执行的步骤:
import turtle
hangman = turtle.Turtle()
hangman.circle(45)
hangman.right(90)
hangman.forward(150)
....
我如何编写代码,以使每个步骤中未使用单词的userInput都被绘制?谢谢大家
如果您定义了一个计数变量来跟踪不正确的猜测数,则可以定义一个函数以绘制所需的部分。在下面的示例中,我假设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.
更多详细信息:
turtle.write(arg,move = False,align =“ left”,font =(“ Arial”,8,“ normal”))参数:
arg – object to be written to the TurtleScreen
move – True/False
align – one of the strings “left”, “center” or right”
font – a triple (font name, font size, font type)
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 the move is true, the pen is moved to the bottom-right corner of the text. By default, the move is False.
非常好!!!!!!