我出于学习目的而制作基于文本的地板游戏。我希望所有可移动功能都在单独的python文件中,但是我在使它们协同工作时遇到问题。
我的主游戏为floors.py
,地图文件为floormap.py
。
我可以从floormap.py
内部floors.py
完全导入并运行函数。
但是我不知道floors.py
在运行函数后如何返回到floormap.py
函数。这是下面的例子。当我运行此命令时,在终端中出现以下错误:
NameError: global name 'first_hall_1' is not defined
我确实使用以下方法来工作:
from floormap import first_hall_1
但是我可以找到一种方法来使这些函数在原始文件中再次被调用。
Floors.py:
import floormap
def first_hall_object():
grab = raw_input("Enter Command > ")
backward = ['back', 'Back', 'Backward', 'backward']
if any (s in grab for s in backward):
first_hall_1()
def walkin_hall():
print "whatever"
floormap.py:
import floors
def first_hall_1():
print "You are in front of the door again. It is locked."
walkin_hall()
您需要first_hall_1
使用模块名称进行限定floormap
。
def first_hall_object():
grab = raw_input("Enter Command > ")
backward = ['back', 'Back', 'Backward', 'backward']
if any (s in grab for s in backward):
floormap.first_hall_1() # <-----
walkin_hall()
通话相同:
def first_hall_1():
print "You are in front of the door again. It is locked."
floors.walkin_hall()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句