[英]How to make the turtle draw one step at a time
我有一個劊子手游戲,我想每個錯誤的字母都畫劊子手的一部分,但我不知道如何一次畫烏龜的一步
這就是我所擁有的:
def drawsturtle(userInput,word):
然后我必須做的步驟:
import turtle
hangman = turtle.Turtle()
hangman.circle(45)
hangman.right(90)
hangman.forward(150)
....
我如何編寫代碼,以便繪制這些步驟之一中的每個用戶輸入? 謝謝大家
如果您定義了一個計數變量來跟蹤錯誤猜測的數量,您可以定義一個函數來繪制所需的部分。 在下面的例子中,我假設了 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
方法寫出錯誤的字母。
參考下面的方法簽名:
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.
更多細節:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.