所以我已经将游戏的初始开场定义为主菜单,其中打印了一些可供选择的选项,例如关卡的难度,或者他们可以看到游戏的帮助。当他们去帮助我希望他们可以选择回到主菜单,因此我为什么要做def mainmenu()。然而,在我的helpmenu if语句中,即使我已声明如果他们键入菜单或菜单,它应该回调mainmenu它没有做任何事情。我们有一个必须使用的模块,这就是为什么我必须使用诸如p.next()之类的东西,这实际上意味着它正在监听用户输入,所以只需忽略它:)
这是代码
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!")
答案 0 :(得分:0)
您必须将类实例化为对象:
menu = mainmenu()
menu.mainmen()
答案 1 :(得分:0)
一般来说,在调用类方法之前,必须先实例化该类,然后使用点表示法调用不同的方法。
回到你的案例,变量p是什么? Lennart的答案是如果你的代码不起作用的方法,那么底部的if语句会出现问题。您正在执行语句
helpmenu = p.next()
然后你在if语句中检查helpmenu的值。不知道p是什么以及它的next()方法是什么我不能给出一个非常有用的答案,但让我们说
p.next()
返回“menu”或“exit”。在这种情况下,您应该按如下方式编写代码的最后一部分:
# Note that helpmenu is either "menu" or "exit", so either the if block will be
# executed or the elif block will be executed.
if helpmenu == "Menu" or helpmenu == "menu":
p.clear()
m = mainmenu()
m.mainmen()
elif helpmenu == "Exit" or helpmenu == "exit":
p.clear()
p.write("Hope you play soon!")
话虽如此,我建议你在helpmenu = p.next()之后插入一个print语句,这样你就可以检查该行之后helpmenu的值。
我在您的代码中注意到的其他内容:
我希望这有帮助,如果您需要任何进一步的帮助,请告诉我,我会尽力帮助我。