Python文本高级游戏-导入错误(无法返回“主”菜单)
所以我已经制作这个小游戏好几天了,作为一个新手练习。我将只包括两个模块,我认为是相关的,如果我应该包括更多的只是告诉我,我会把他们在粘贴箱 这是陷阱。我的游戏从终端启动脚本start.py开始。对于用户输入,我创建了另一个名为prompt.py的模块,因此不必重复代码。但我遇到了一个问题。当我从prompt.py调用函数“load_menu”时,我希望能够通过输入0返回主屏幕(在start.py中的函数“splash_screen”)。因此,如果我想替换prompt.py(在函数“加载菜单”中)中的第53行,并将其放在Python文本高级游戏-导入错误(无法返回“主”菜单),python,python-2.7,module,importerror,python-import,Python,Python 2.7,Module,Importerror,Python Import,所以我已经制作这个小游戏好几天了,作为一个新手练习。我将只包括两个模块,我认为是相关的,如果我应该包括更多的只是告诉我,我会把他们在粘贴箱 这是陷阱。我的游戏从终端启动脚本start.py开始。对于用户输入,我创建了另一个名为prompt.py的模块,因此不必重复代码。但我遇到了一个问题。当我从prompt.py调用函数“load_menu”时,我希望能够通过输入0返回主屏幕(在start.py中的函数“splash_screen”)。因此,如果我想替换prompt.py(在函数“加载菜单”中)
start.splash\u screen()
中,我就做不到。因为当我将import start
放在prompt.py的开头时,我得到了以下错误:
Traceback (most recent call last):
File "start.py", line 3, in <module>
import player
File "/Volumes/DATA HD/Dropbox/Python/ex45/player.py", line 3, in <module>
import prompt
File "/Volumes/DATA HD/Dropbox/Python/ex45/prompt.py", line 7, in <module>
import start
File "/Volumes/DATA HD/Dropbox/Python/ex45/start.py", line 61, in <module>
splash_screen()
File "/Volumes/DATA HD/Dropbox/Python/ex45/start.py", line 22, in splash_screen
action = prompt.menu()
AttributeError: 'module' object has no attribute 'menu'
这里是提示符.py
import sys
import custom_error
import player
import handler
import prompt
import splashscreen
import game
def splash_screen():
print chr(27) + "[2J"
splashscreen.intro()
print "*" * 80
print "***** Welcome to ZOMBIE ADVENTURE *****"
print "*" * 80
print "\nSelect option:"
print "1. Start a new game"
print "2. Load existing game"
print "3. Quit"
while True:
action = prompt.menu()
if action == 1:
create_player = player.CreateNewPlayer()
create_player_args = create_player.generate()
the_player = player.Player(*create_player_args)
print "\nYour name is %s and you're %d old." % (the_player.name, the_player.age)
print "It is %s that you're a man." % str(the_player.male).lower()
print "Your maximum health is %d hitpoints." % the_player.hitpoints
print "\n1. Continue to game"
print "2. Back to main menu"
action = prompt.menu()
if action == 1:
prompt.game_help()
custom_error.errortype(4)
print chr(27) + "[2J"
a_game = game.Engine(the_player, 'Apartment')
a_game.move()
elif action == 2:
handler.load()
else:
custom_error.errortype(3)
custom_error.errortype(2)
splash_screen()
# a_game = game.Engine()
# a_game.launch_game(the_player)
elif action == 2:
handler.load()
elif action == 3:
splash_screen()
else:
custom_error.errortype(0)
splash_screen()
import handler
import sys
import hint
import game
import player
import custom_error
import start
def standard(the_player):
while True:
user_input = str(raw_input("> ")).lower()
if user_input == "hint":
print "hint"
# ziskej soucasnou polohu a pak hint.hints.get(poloha)
elif user_input == "save":
handler.save(the_player)
# ziskej soucasnou polohu a pak game.save(poloha)
elif user_input == "inventory" or user_input == "inv":
print the_player.inventory.keys()
elif user_input == "char":
print "\n---", the_player.name, "---"
print "Age:", the_player.age
print "Is male:", the_player.male
print "Hitpoints:", the_player.hitpoints
print "Current location:", the_player.location, "\n"
elif user_input == "help":
game_help()
elif user_input == "quit":
exit(1)
else:
return user_input
def menu():
while True:
try:
user_input = int(raw_input("\nType a number > "))
return user_input
except ValueError:
custom_error.errortype(1)
def load_menu():
while True:
try:
user_input = int(raw_input("\nType a number or type 0 to QUIT > "))
if user_input == 0:
exit(1)
else:
return user_input
except ValueError:
custom_error.errortype(1)
def load_game():
while True:
try:
user_input = str(raw_input("\nLoad this character? Y/N > ")).lower()
if user_input == "y" or user_input == "n":
return user_input
else:
print "Please type in 'Y' or 'N' only."
except ValueError:
custom_error.errortype(3)
def game_help():
print "\n"
print "-" * 80
print "Type HELP anytime to display this message."
print "-" * 80
print "Type these commands anytime to perform actions:"
print " * HINT: If you're stuck, you can get little help."
print " * SAVE: Save your progress."
print " * INV: Will display contents of your inventory."
print " * CHAR: Shows character stats."
print " * QUIT: Quit game without saving (to save"
print " use the appropriate command.)"
我还包括了其余的文件:
- 我不认为这个词是保留的
您的项目中是否缺少
\uuuuu init\uuuuuu.py
?没有这个结构很难知道
另一种可能是,您可能正在几个地方执行循环导入—在我看来,它就像我在prompt.py和start.py中看到的一样。他们互相输入。虽然可以做一些技巧,但不要因为你认为你想要简单的东西而这样做。相反,重构
我创建了一个测试项目,其中只有start.py和prompt.py,以及包的
\uuuu init\uuuu.py
,这确认了没有保留任何内容,所以至少,您的问题在别处。您能在github、bitbucket或类似的东西上发布完整的项目吗?请看下面我的答案。好吧,我还是个新手,所以我可能做错了(事实上,这是我第一个使用多个py文件的项目)。是的,我缺少\uuuuu init\uuuuu.py
。我没听说过,所以我需要查一下。另外,我一直认为我需要导入每个.py文件,其中包含我在任何当前文件中使用的函数或类。请参阅-基本上,我是说,如果有模块a和B,a导入B或B导入a。这是解决此问题的最实用的python方法。换句话说,当A同时导入B时,B不能导入A。上面的链接将进一步详细介绍。只需移动代码即可。关于init.py,只需将其放在定义python包的目录中即可。因此,如果所有内容都在“mygame”文件夹下,请将init.py作为一个空白文件放在那里。您可能会在该文件中使用代码,原因有很多,但通常我觉得这不是必需的,在大多数情况下甚至是一个坏主意。最后一条评论可以解决您的问题-关于导入,您确实需要导入您使用的代码。您可以从模块导入模块或特定功能(导入开始与从开始导入启动屏幕)。很抱歉,我仍然无法理解。我尝试创建import.py文件并将这些导入放在那里,但当我在prompt.py中引用它时,仍然会遇到相同的错误。