2016-07-18 10 views
-1

私はPythonにはかなり新しく、テキストベースのゲームを作成することによってクラスとメソッドがどのようにやりとりするかを学んでいます。コードは素晴らしいですが、私の質問はName(オブジェクト)クラスにあります。私がコードに書いているところでは、(私はいつも以下のように始めか中間に)私はいつも入力を促されます。私は、raw_input( ">")をプロンプトする属性name_newが原因である可能性があると思われますが、クラス名()が要求されたときに、回避するか変更する方法があるかどうかは疑問でした。クラスの属性が間違った時刻にすぐに実行されますか?

class Character(object): 

    def __init__(self, name, weapon, spec_ability, health): 
     self.name = name 
     self.weapon = weapon 
     self.spec_ability = spec_ability 
     self.health = 100 


    def special_ability(self): 
     special = False 
     rand_num = str(random.choice(rdm)) 

     if '1' in rand_num or '2' in rand_num or '3' in rand_num: 
      special = True 
      if special: 
       print "Your special ability is ready." 
       return True 

      else: 
       return 
     else: 
      print "Your special ability is not ready." 

    def Critical_Chance(self): 
     crit = False 
     rand_num = str(random.choice(rdm)) 


     if '1' in rand_num or '2' in rand_num or '3' in rand_num: 
      crit = True 
      if crit: 
       print "Critical hit incoming." 
      return True 
     else: 
      return 

class Warrior(Character): 

    def __init__(self, name, weapon, spec_ability, health, armor): 
     super(Warrior, self).__init__(name, weapon, spec_ability, health) 
     self.armor = armor 

class Mage(Character): 

    def __init__(self, name, weapon, spec_ability, health, cloak): 
     super(Mage, self).__init__(name, weapon, spec_ability, health) 
     self.cloak = cloak 

class Rogue(Character): 

    def __init__(self, name, weapon, spec_ability, health, boots): 
     super(Rogue, self).__init__(name, weapon, spec_ability, health) 

class Name(object): 

    name_new = raw_input("> ") 


def start(): 

    print """\nWelcome to the world of _______. \n 
You are a young hero or heroine 
in search of the gold that hides within the Mountains of _____. 
In order to obtain the gold and treasure that lies within the 
mountains, you must battle great monsters and face dangerous 
perils.""" 

    print "Will you pursue such adventure? Or will you refuse?" 

    choice = raw_input("> ") 

    if "Yes" in choice or "yes" in choice: 
     Introduction() 
    elif "No" in choice or "no" in choice: 
     sys.exit() 
    else: 
     print "You typed: %s. \nPlease try again." % choice 
     start() 


def Introduction(): 

    print "Thank you for joining in on this adventure..." 
    print "Well, welcome to your adventure for the gold." 
    print "You will encounter dangerouse quests and enemies, but you will be\ 
rewarded in the end." 

    print "Now, what class will you be?\n\ 
Warrior     Mage    Rogue" 

    char_choice = raw_input("> ") 

    verynew_name = Name() 
    name_choice = Name.name_new 

    if "Warrior" in char_choice or "warrior" in char_choice: 
     name_choice = Warrior(name_choice, None, None, None, None) 
     print name_choice.name 
     print name_choice.weapon 
     print name_choice.spec_ability 
     print name_choice.health 
     print name_choice.armor 


    elif "Mage" in char_choice or "mage" in char_choice: 
     name_choice = Mage(name_choice, None, None, None, None) 
     print name_choice.name 
     print name_choice.weapon 
     print name_choice.spec_ability 
     print name_choice.health 
     print name_choice.cloak 

    elif "Rogue" in char_choice or "rogue" in char_choice: 
     name_choice = Rogue(name_choice, None, None, None, None) 
     print name_choice.name 
     print name_choice.weapon 
     print name_choice.spec_ability 
     print name_choice.health 
     print name_choice.boots 

    else: 
     print "You must pick a class." 
     start() 

    print "Our story starts in the town of Larton." 
    #super(Larton, self).enter() 


start() 
+0

あなたの実際の質問から気をそらすコードが多すぎます。実際の問題を示すためにコードを最小限に抑えてください。 – Evert

+0

'raw'関数は' Name'クラスのメソッドの中に置かなければなりません。おそらく、そのためのクラスは必要ないでしょう。それは何もしないので、 'self.name = raw_input()'で十分でしょう。 – Evert

答えて

1

PARAMTERとして名前を取ることはありません、あなたのキャラクタークラスとサブクラスのコンストラクタを定義します。 例えば

クラスの戦士(文字):

def __init__(self, weapon, spec_ability, health, armor): 
    super(Warrior, self).__init__(weapon, spec_ability, health) 
    self.armor = armor 

と文字クラスでは、あなたはあなたが常にによって、ロジックの後半に名前を割り当てることができ、デフォルト

によってNoneにするself.nameを定義することができます

character.name = Name.name_new 

のようなものを使用している限り、あなたがそうであるように、あなたが実際に名前の値を読む前に、あなたは

罰金になること0
2

プログラムを実行すると、モジュールレベルのコードが実行されます。これには、Nameクラスが含まれます。

class Name(object): 
    name_new = raw_input("> ") 

これはクラスNameを作成してもクラス内のコードを実行します。だからあなたは名前を入力するように促されます。

あなたはあなたがクラス(インスタンス化たときにそれが何Name()だからraw_inputが実行されるように、あなたのNameクラスを変更する必要があり、この

verynew_name = Name() 

のような名前のためにユーザーにプロンプ​​トを表示できるようにしたい場合、それはNameのインスタンスを作成します)行います

class Name(object): 
    def __init__(self): 
     self.name_new = raw_input("> ") 

(代わりにname_choice = Name.name_newのあなたは私たちに持っているだろうことに注意してくださいe name_choice = verynew_name.name_new

+0

ありがとうございます!出来た!! –

関連する問題