python - 我可以将项目及其属性存储在外部文件中并在需要时调用它吗? (用 Python 编写 Roguelike 时)

标签 python python-2.7 roguelike libtcod

我刚刚完成 this tutorial 的工作用于在 Python 中编写 Roguelike 程序,现在我非常依赖自己来确定去哪里以及下一步该做什么。

我的困境在于代码的困惑。我想将元素存储在某处,例如项目;生物;技能;任何可能有大量它们自己拥有大量属性的东西。

目前,所有这些代码都在一个越来越大的文件中,这是最基本的。将项目放置在关卡上的函数目前看起来很像:(生成关卡时调用此函数)

def place_objects(room):

    #Maximum number of items per room
    max_items = from_dungeon_level([[1, 1], [2, 4]])

    #Chance of each item (by default they have a chance of 0 at level 1, which then goes up)
    item_chances = {}
    item_chances['heal'] = 35
    item_chances['lightning'] = from_dungeon_level([[25, 4]])
    item_chances['fireball'] = from_dungeon_level([[25, 6]])
    item_chances['confuse'] = from_dungeon_level([[10, 2]])
    item_chances['sword'] = from_dungeon_level([[5, 4]])
    item_chances['shield'] = from_dungeon_level([[15, 8]])

    #Choose a random number of items
    num_items = libtcod.random_get_int(0, 0, max_items)

    for i in range(num_items):
        #Choose random spot for this item
        x = libtcod.random_get_int(0, room.x1+1, room.x2-1)
        y = libtcod.random_get_int(0, room.y1+1, room.y2-1)

        #Only place it if the tile is not blocked
        if not is_blocked(x, y):
            choice = random_choice(item_chances)
            if choice == 'heal':
                #Create a healing potion
                item_component = Item(use_function=cast_heal)
                item = Object(x, y, '~', 'Salve', libtcod.light_azure, item=item_component)
            elif choice == 'lightning':
                #Create a lightning bolt scroll
                item_component = Item(use_function=cast_lightning)
                item = Object(x, y, '#', 'Scroll of Lightning bolt', libtcod.light_yellow, item=item_component)
            elif choice == 'fireball':
                #Create a fireball scroll
                item_component = Item(use_function=cast_fireball)
                item = Object(x, y, '#', 'Scroll of Fireball', libtcod.light_yellow, item=item_component)
            elif choice == 'confuse':
                #Create a confuse scroll
                item_component = Item(use_function=cast_confuse)
                item = Object(x, y, '#', 'Scroll of Confusion', libtcod.light_yellow, item=item_component)
            elif choice == 'sword':
                #Create a sword
                equipment_component = Equipment(slot='right hand', power_bonus=3)
                item = Object(x, y, '/', 'Sword', libtcod.sky, equipment=equipment_component)
            elif choice == 'shield':
                #Create a shield
                equipment_component = Equipment(slot='left hand', defense_bonus=1)
                item = Object(x, y, '[', 'Shield', libtcod.sky, equipment=equipment_component)

            objects.append(item)
            item.send_to_back()

由于我打算添加更多项,此函数将变得非常长,然后需要创建所有函数来处理每个项的作用。我真的很想将它从 main.py 中取出并存储在其他地方,以使其更易于使用,但我目前不知道该怎么做。

以下是我尝试思考问题的尝试:

我能不能有一个包含项目类的文件,每个项目都包含许多属性? (名称、类型、条件、附魔、图标、重量、颜色、描述、装备槽、 Material )。然后将项目的功能存储在那个文件中?主文件如何知道何时调用其他文件?

是否可以将所有项目数据存储在外部文件(如 XML 或其他文件)中并在需要时从那里读取?

显然,这不仅适用于元素。当我真正想要的是一个主循环和更好的组织结构时,这对于没有一个真正臃肿的 main.py 非常有用,该 main.py 包含游戏中的所有生物、元素和其他对象,膨胀了数千行代码。

最佳答案

如果您不需要人类可读的文件,请考虑使用 Python 的 pickle图书馆;这可以序列化对象和函数,这些对象和函数可以写入文件或从文件中读回。

在组织方面,您可以有一个对象文件夹,将这些类从您的主游戏循环中分离出来,例如:

game/
    main.py
    objects/
        items.py
        equipment.py
        creatures.py

为了进一步改进,使用继承来做例如:

class Equipment(Object):

class Sword(Equipment):

class Item(Object):

class Scroll(Item):

然后所有的设置,例如sword 可以在初始化中定义(例如它在哪只手上)并且只需要传入特定剑的变化(例如名称,力量奖励)。

关于python - 我可以将项目及其属性存储在外部文件中并在需要时调用它吗? (用 Python 编写 Roguelike 时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20169712/

相关文章:

python - OpenCV:OCR 之前阴影图像的轮廓检测

python - TensorFlow 调试器值错误 : Node name 'Add/x' is not found in partition graphs of device

python - Windows 上的 PySide2

roguelike - 哪种编程语言最适合创建 Roguelike 游戏?

c++ - 启动具有特定变量值的派生类

c++ - Roguelike FOV问题

python - 将字符串从 C 传递到 Python 进行多处理,无需进行额外的复制

java - 如何安装antlr4?

python - **kwargs 和默认参数

python - read() 的文件大小限制?