因此,我定义了我的游戏作为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' - 这暗示着你根本不需要它是一种方法)。 –