我目前正在开发一个小型冒险游戏,到目前为止,我已经有了一个player.py和items.py以及一个如下所示的world.py:
import game
class MapTile:
def __init__(self, x, y):
self.x = x
self.y = y
def intro_text(self):
raise NotImplementedError("Create a subclass instead!")
class StartTile(MapTile):
def intro_text(self):
return "some text, " + user_name + """ even more text
"""
class BoringTile(MapTile):
def intro_text(self):
return """
some text
"""
class VictoryTile(MapTile):
def intro_text(self):
return """
some text
"""
world_map = [
[None, VictoryTile(1,0), None],
[None, BoringTile(1,1), None],
[BoringTile(0,2),StartTile(1,2),BoringTile(2,2)],
[None,BoringTile(1,3),None]
]
def tile_at(x, y):
if x < 0 or y < 0:
return None
try:
return world_map[y][x]
except IndexError:
return None
和一个像这样的game.py:
import world
from player import Player
def play():
print("------------------------------------")
print(" Willkommen zu ...")
print("------------------------------------" + "\n")
player = Player()
while True:
user_name = get_user_name()
room = world.tile_at(player.x, player.y)
print(room.intro_text())
action_input = get_player_command()
if action_input in ["n", "N"]:
player.move_north()
elif action_input in ["s", "S"]:
player.move_south()
elif action_input in ["o", "O"]:
player.move_east()
elif action_input in ["w", "W"]:
player.move_west()
elif action_input in ["i", "I"]:
player.print_inventory()
def get_user_name():
return input("text " + "\n")
def get_player_command():
return input()
def print_ordered(to_print):
for i, value in enumerate(to_print, 1):
print(str(i) + ". " + str(value))
play()
我目前正试图设置游戏世界,但是当我启动游戏时,我得到了这个回溯:
Traceback (most recent call last):
File "C:\Users\samwa\Workspace\TextAdventure\game.py", line 1, in <module>
import world
File "C:\Users\samwa\Workspace\TextAdventure\world.py", line 1, in <module>
import game
File "C:\Users\samwa\Workspace\TextAdventure\game.py", line 38, in <module>
play()
File "C:\Users\samwa\Workspace\TextAdventure\game.py", line 14, in play
room = world.tile_at(player.x, player.y)
AttributeError: module 'world' has no attribute 'tile_at'
尽管world.py中的“tile_at”就在那里。如何解决?
发布于 2018-06-12 09:49:42
只要将world.tile_at更改为tile_at。
https://stackoverflow.com/questions/-100005315
复制相似问题