這裏是新的python學習器。我試圖在Python中設計一個文本RPG,並且我希望所有敵方怪物的統計數據在遊戲的特定部分增加。這裏是我的代碼爲一些人的敵人:Python,如何在大類下創建特定的類
class Executioner:
def __init__(self,name):
self.name = name
self.potions = 0
self.maxhp = 800
self.hp = self.maxhp
self.attack = 25
self.ability = 0
self.defense = 0
self.goldgain = 333
self.expgain = 250
ExecutionerIG = Executioner("Executioner the slayer")
class Goblin:
def __init__(self,name):
self.name = name
self.potions = 0
self.maxhp = 80
self.hp = self.maxhp
self.attack = 25
self.ability = 0
self.defense = 0
self.goldgain = 5.5
self.expgain = 22
GoblinIG = Goblin("Goblin")
class Zombie:
def __init__(self,name):
self.name = name
self.potions = 0
self.maxhp = 180
self.hp = self.maxhp
self.attack = 19
self.ability = 0
self.defense = 0
self.goldgain = 7
self.expgain = 28
ZombieIG = Zombie("Zombie")
我想一個辦法做到這一點很快可以定義這些類放入一個大班裏叫敵人,然後調用enemies.hp,enemies.attack等增加。
但我不知道該怎麼做,因爲我現在相當不滿意。
關於如何通過指定大班級下的小班級或快速增加班級統計數據的方式來增加班級統計數據的建議(只需輸入並增加班級數據將不起作用,因爲我將擁有大量敵方小怪)將會非常感謝。
感謝您的幫助,但您再次知道如何去做Daniel Roseman所說的話?在通用Enemy類的__init__方法中接受這些值?一個詳細的例子將不勝感激! – pythonsohard
這與您使用name屬性所做的相同。所以你應該有'def __init __(self,name,maxhp,attack,ability,defense,goldgain,expgain):self.name = name self.maxhp = maxhp self.attack =攻擊self.ability =能力self.defense =防禦self.goldgain = goldgain self.expgain = expgain'那麼你可以創建一個殭屍通過以下操作: 'zombie =敵人(「殭屍」,180,19,0,0,7,28)' – MercuryMan