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



我刚刚完成了用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中取出,放在其他地方,这样更容易使用,但我现在不知道该怎么做。

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

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

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

这是我可以应用的东西,显然不仅仅是项目。当我真正想要的是一个主循环和一个更好的组织结构时,这将非常有用,因为没有一个真正臃肿的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):

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

是否所有项目数据都存储在外部文件中(如XML或什么),并在需要时从那里阅读?

您可以使用ConfigParser模块执行此操作。

您可以将项目的属性存储在一个单独的文件(一个普通的文本文件)中。读取此文件可自动创建对象。

我将在这里使用文档中的示例作为指南:

import ConfigParser
config = ConfigParser.RawConfigParser()
config.add_section('Sword')
config.set('Sword', 'strength', '15')
config.set('Sword', 'weight', '5')
config.set('Sword', 'damage', '10')
# Writing our configuration file to 'example.cfg'
with open('example.cfg', 'wb') as configfile:
    config.write(configfile)

现在,要读取文件:

import ConfigParser
from gameobjects import SwordClass
config = ConfigParser.RawConfigParser()
config.read('example.cfg')
config_map = {'Sword': SwordClass}
game_items = []
for i in config.sections():
    if i in config_map:
       # We have a class to generate
       temp = config_map[i]()
       for option,value in config.items(i):
           # get all the options for this sword
           # and set them
           setattr(temp, option, value)
       game_items.append(temp)

最新更新