因此,我定義了我的遊戲作爲mainmenu的初始開放,並在其中打印了幾個選項,可以選擇一些選項,比如關卡的級別,或者他們可以看到遊戲的幫助。當他們去幫忙時,我希望他們可以選擇回到主菜單,因此我做了main mainmenu()。然而,在我的幫助菜單中,即使我已經聲明,如果他們鍵入菜單或菜單,它應該在mainmenu上回撥,它不會執行任何操作。我們有一個模塊,我們必須使用,這就是爲什麼我要使用的東西,如p.next()這實際上意味着它監聽用戶輸入,這樣就忽略這個問題:)如何調用一個類內的def
這裏的代碼
def mainmen():
p.write("Welcome to 'The Great Escape'!\n")
p.write("\nPlease type what dificulty you would like to play the game,\nthe options are Easy, Medium or Hard\n")
p.write("\nHowever, if you need help please type Help for instructions\n")
p.write(">>>")
dificulty = p.next()
if dificulty == "easy" or dificulty == "Easy":
p.clear()
p.write("The Great Escape")
easy()
elif dificulty == "medium" or dificulty == "Medium":
p.clear()
p.write("The Great Escape")
medium()
elif dificulty == "hard" or dificulty == "Hard":
p.clear()
p.write("The Great Escape")
hard()
elif dificulty == "help" or dificulty == "Help":
p.clear()
p.write("Welcome to 'The Great Escape' instructions\n")
p.write("\nTo complete the level you must move your Turtle around the\nline without touching the line itself\n")
p.write("\nControls\n")
p.write("Forward - 'W'\n")
p.write("Left - 'A'\n")
p.write("Backwards - 'S'\n")
p.write("Right - 'D'\n")
p.write("\nPlease type 'Menu' to go back to the main menu,\nor 'Exit' to quit the game\n")
p.write(">>>")
**`here is my help menu if statements, I want it so that if they type Menu or menu they get taken back to the main menu.`**
helpmenu = p.next()
if helpmenu == "Menu" or helpmenu == "menu":
p.clear()
mainmenu() **<<This should call on the mainmenu but it doesn't??**
elif helpmenu == "Exit" or helpmenu == "exit":
p.clear()
p.write("Hope you play soon!")
我確定這是重複的,但我找不到其他問題... –
我也推薦一本好的Python書籍來幫助您學習基礎知識。現在看來最好的一個,至少對那些免費的人來說,就是Zed Shaws「以艱難的方式學習Python」。 http://learnpythonthehardway.org/ –
你甚至不需要上課。只需將mainmenu()定義爲頂層函數並簡化代碼即可。 (請注意,如何在方法內部不使用'self' - 這暗示着你根本不需要它是一種方法)。 –