2013-11-24 8 views
5

私はちょうど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を持っていないので、ゲーム内のすべての生き物、アイテム、その他のオブジェクトを保持していないため、本当に便利です。

答えて

1

人が読めるファイルが必要ない場合は、Pythonのpickleライブラリを使用することを検討してください。これはオブジェクトと関数を直列化することができます。オブジェクトと関数はファイルに書き込んだり、ファイルから読み込んだりすることができます。組織面では

、あなたがメインのゲームループからこれらのクラスを分離、オブジェクトフォルダを持つことができ、例えば:さらなる改善のために

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

、例えばを行うには、継承を使用します。

class Equipment(Object): 

class Sword(Equipment): 

class Item(Object): 

class Scroll(Item): 

次に、たとえば(例えば名前、パワーボーナス)のために異なるものだけが渡される必要があります。

1

すべてのアイテムデータを保存することができますか(XMLや のような)外部ファイルに保存し、必要に応じてそこから読み込みます。

ConfigParserモジュールでこれを行うことができます。

アイテムのプロパティを別のファイル(通常のテキストファイル)に保存することができます。このファイルを読んでオブジェクトを自動的に作成します。

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) 
:ファイルを読むために、今すぐ

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) 

:私はここに、ガイドとしてのドキュメントからの例を使用するつもりだ

関連する問題