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

six*_*les 5 python roguelike python-2.7 libtcod

我刚刚完成了本教程,用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()
Run Code Online (Sandbox Code Playgroud)

当我打算添加更多项目时,此功能将变得非常长,然后需要创建所有功能来处理每个项目的功能.我真的想把它从main.py中取出并存放在其他地方,以便更容易使用,但我现在不知道该怎么做.

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

我是否可以拥有一个包含项目类的文件,该项目类包含每个项目包含的多个属性; (名称,类型,条件,结界,图标,重量,颜色,描述,equip_slot,materal).然后存储项目在文件中的功能?主文件如何知道何时调用其他文件?

所有商品数据都可以存储在外部文件(如XML或其他东西)中,并在需要时从那里读取吗?

这是我可以应用于显然不仅仅是项目的东西.这对于没有真正膨胀的main.py来说非常有用,它可以保存游戏中的所有生物,物品和其他物体,数以千计的代码行,当我真正想要的是一个主循环和一个更好的组织结构时.

jon*_*rpe 1

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

在组织方面,您可以有一个对象文件夹,将这些类与主游戏循环分开,例如:

game/
    main.py
    objects/
        items.py
        equipment.py
        creatures.py
Run Code Online (Sandbox Code Playgroud)

要进一步改进,请使用继承来执行以下操作:

class Equipment(Object):

class Sword(Equipment):

class Item(Object):

class Scroll(Item):
Run Code Online (Sandbox Code Playgroud)

然后,任何例如剑的所有设置都可以在初始化中定义(例如,它在哪只手中),并且只需要传递针对特定剑而变化的内容(例如,名称、力量加成)。