私は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()
あなたの実際の質問から気をそらすコードが多すぎます。実際の問題を示すためにコードを最小限に抑えてください。 – Evert
'raw'関数は' Name'クラスのメソッドの中に置かなければなりません。おそらく、そのためのクラスは必要ないでしょう。それは何もしないので、 'self.name = raw_input()'で十分でしょう。 – Evert