私はちょうどthis tutorialを使ってPythonでRoguelikeをプログラミングすることを終えました。今どこに行けばいいのか、そして次に何をするのかはわかります。アイテムとそのプロパティを外部ファイルに保存し、必要に応じて呼び出すことはできますか? (RoguelikeをPythonでプログラミングするとき)
私の苦境はコードの厄介さです。私はどこかに要素を格納したいと思います。生き物;スキル;自分自身のために数多くの特性を持つ多数のものが存在する可能性のあるもの。
現時点では、このコードはすべて単一のファイルに収められていますが、これは非常に大きくなります。これは最も基本的なものです。レベル上のアイテムを配置するための機能は、現時点では非常に次のようになります(レベルが生成されたとき、この関数が呼び出された)
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を持っていないので、ゲーム内のすべての生き物、アイテム、その他のオブジェクトを保持していないため、本当に便利です。