首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Creating a textadventure in python: AttributeError: module x has no attribute y

Creating a textadventure in python: AttributeError: module x has no attribute y
EN

Stack Overflow用户
提问于 2018-06-12 00:25:49
回答 1查看 0关注 0票数 0

我目前正在开发一个小型冒险游戏,到目前为止,我已经有了一个player.py和items.py以及一个如下所示的world.py:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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()

我目前正试图设置游戏世界,但是当我启动游戏时,我得到了这个回溯:

代码语言:javascript
复制
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”就在那里。如何解决?

EN

回答 1

Stack Overflow用户

发布于 2018-06-12 09:49:42

只要将world.tile_at更改为tile_at。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档