Python 3: 名称 'Class instance' 未定义
Python 3: Name 'Class instance' not defined
我一直在开发一款基于文本的冒险游戏,目前我正致力于将一个大型游戏脚本拆分成单独的模块。
这是包含游戏逻辑的主要游戏模块。在 main()
函数中,我定义了 classes
的实例
from gameworld import *
def main():
player = Player("Jeff", 100)
bag = Bag([])
location = Location('introd')
command = ' '
while command != "":
command = input('>>> ')
if command in location.room.exits:
location.travel(command)
elif command == 'look':
location.room_desc()
elif command == '':
print('You have to say what it is you want to do!')
command = '#'
elif command == 'search':
location.search_room()
elif command.split()[0] == 'Take':
bag.check_take(command.split()[1])
elif command == 'Inventory':
bag.check_inv()
else:
print('Invalid command')
if __name__ == '__main__':
main()
以下是游戏世界模块的节选。它包括我遇到困难的功能。 Location.key_check
和 Bag.check_take
函数。
#gameworld.py
class Room:
def __init__(self, name, description, exits, actions, roominv, roomkey, lock):
self.name = name
self.description = description
self.exits = exits
self.actions = actions
self.roominv = roominv
self.roomkey = roomkey
self.lock = lock
class Location:
def __init__(self, room):
self.room = world[room]
def key_check(self, new_room_name):
if world[new_room_name].lock and world[new_room_name].roomkey not in bag.inventory:
self.no_key()
else:
world[new_room_name].lock = False
self.set_room(new_room_name)
self.room_desc()
class Bag():
def __init__(self, inventory):
self.inventory = inventory
def add_to_inv(self, key):
self.inventory.append(location.room.roominv[key])
del location.room.roominv[key]
def none_here(self, key):
print("You can't find a", key)
def check_take(self, key):
if location.room.roominv and key in location.room.roominv:
self.add_to_inv(key)
print('you take the', key)
else:
self.none_here(key)
当我 运行 游戏并尝试拾取物品时,我收到此回溯错误:
Traceback (most recent call last):
File "C:\Users\Daniel\Python 3.6\Scripts\PythonPR\PythonPR\FlubbosModuleTest.py", line 31, in <module>
main()
File "C:\Users\Daniel\Python 3.6\Scripts\PythonPR\PythonPR\FlubbosModuleTest.py", line 23, in main
bag.check_take(command.split()[1])
File "C:\Users\Daniel\Python 3.6\Scripts\PythonPR\PythonPR\gameworld.py", line 81, in check_take
if location.room.roominv and key in location.room.roominv:
NameError: name 'location' is not defined
我在尝试移动到上锁的房间和 key_check
方法 运行s 时收到类似的错误。当代码全部包含在同一个脚本中时,运行 可以毫无问题地访问这些 class 个实例。
当您调用 bag.check_take 作为参数时,您需要传递位置变量,因为位置未定义且不在 Bag class 的范围内。当你调用一个class成员函数时,你需要传递代表其他对象的变量。
如果您已将 Bag
设计为具有您更新的库存属性。
def add_to_inv(self, key):
self.inventory.append(location.room.roominv[key])
del location.room.roominv[key]
Nitpick:将某个位置的房间清单的突变移动到管理器功能。请参阅下面的其余答案以获得一个想法。
你可以把check_take
做成一个带钥匙、包和位置的函数。此函数可以导入并相应地传递密钥、包和位置。
def check_take(key, bag, location=None):
if location.room.roominv and key in location.room.roominv:
bag.add_to_inv(key)
else:
bag.none_here(key)
我一直在开发一款基于文本的冒险游戏,目前我正致力于将一个大型游戏脚本拆分成单独的模块。
这是包含游戏逻辑的主要游戏模块。在 main()
函数中,我定义了 classes
from gameworld import *
def main():
player = Player("Jeff", 100)
bag = Bag([])
location = Location('introd')
command = ' '
while command != "":
command = input('>>> ')
if command in location.room.exits:
location.travel(command)
elif command == 'look':
location.room_desc()
elif command == '':
print('You have to say what it is you want to do!')
command = '#'
elif command == 'search':
location.search_room()
elif command.split()[0] == 'Take':
bag.check_take(command.split()[1])
elif command == 'Inventory':
bag.check_inv()
else:
print('Invalid command')
if __name__ == '__main__':
main()
以下是游戏世界模块的节选。它包括我遇到困难的功能。 Location.key_check
和 Bag.check_take
函数。
#gameworld.py
class Room:
def __init__(self, name, description, exits, actions, roominv, roomkey, lock):
self.name = name
self.description = description
self.exits = exits
self.actions = actions
self.roominv = roominv
self.roomkey = roomkey
self.lock = lock
class Location:
def __init__(self, room):
self.room = world[room]
def key_check(self, new_room_name):
if world[new_room_name].lock and world[new_room_name].roomkey not in bag.inventory:
self.no_key()
else:
world[new_room_name].lock = False
self.set_room(new_room_name)
self.room_desc()
class Bag():
def __init__(self, inventory):
self.inventory = inventory
def add_to_inv(self, key):
self.inventory.append(location.room.roominv[key])
del location.room.roominv[key]
def none_here(self, key):
print("You can't find a", key)
def check_take(self, key):
if location.room.roominv and key in location.room.roominv:
self.add_to_inv(key)
print('you take the', key)
else:
self.none_here(key)
当我 运行 游戏并尝试拾取物品时,我收到此回溯错误:
Traceback (most recent call last):
File "C:\Users\Daniel\Python 3.6\Scripts\PythonPR\PythonPR\FlubbosModuleTest.py", line 31, in <module>
main()
File "C:\Users\Daniel\Python 3.6\Scripts\PythonPR\PythonPR\FlubbosModuleTest.py", line 23, in main
bag.check_take(command.split()[1])
File "C:\Users\Daniel\Python 3.6\Scripts\PythonPR\PythonPR\gameworld.py", line 81, in check_take
if location.room.roominv and key in location.room.roominv:
NameError: name 'location' is not defined
我在尝试移动到上锁的房间和 key_check
方法 运行s 时收到类似的错误。当代码全部包含在同一个脚本中时,运行 可以毫无问题地访问这些 class 个实例。
当您调用 bag.check_take 作为参数时,您需要传递位置变量,因为位置未定义且不在 Bag class 的范围内。当你调用一个class成员函数时,你需要传递代表其他对象的变量。
如果您已将 Bag
设计为具有您更新的库存属性。
def add_to_inv(self, key):
self.inventory.append(location.room.roominv[key])
del location.room.roominv[key]
Nitpick:将某个位置的房间清单的突变移动到管理器功能。请参阅下面的其余答案以获得一个想法。
你可以把check_take
做成一个带钥匙、包和位置的函数。此函数可以导入并相应地传递密钥、包和位置。
def check_take(key, bag, location=None):
if location.room.roominv and key in location.room.roominv:
bag.add_to_inv(key)
else:
bag.none_here(key)