如何让海龟一步一步绘图
我有一个猜字游戏,每当玩家猜错一个字母,我就需要画出绞刑架的一部分,但我不知道怎么一步一步地用海龟绘图来实现。
这是我现在的代码:
def drawsturtle(userInput,word):
接下来我需要做的步骤是:
import turtle
hangman = turtle.Turtle()
hangman.circle(45)
hangman.right(90)
hangman.forward(150)
....
我该怎么写代码,让每次用户输入的字母如果不在正确的单词里,就画出这些步骤呢?谢谢大家!
2 个回答
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.
想了解更多细节,可以查看这里:
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)