2014-12-31 42 views
1

我一直在爲RPG遊戲做一個戰鬥測試。無論出於何種原因,我都會收到class has no attribute錯誤。職業沒有屬性X

任何想法如何解決這個問題?

class Enemy: 
    def __init__(self, name, H, STR): 
     self.name = name 
     self.H = H 
     self.STR = STR 

    def is_alive(self): 
     return self.H > 0 

    enemyturn = ["1", "2"] 

class Goblin(Enemy): 
    def __init__(self, name, H, STR): 
     name = "Goblin" 
     H = 50 
     STR = 5 

這些類是在下面的代碼中使用:

if command == "1": 
    Goblin.H -= Player.STR 
    print("You strike the Goblin for {0} damage.".format(Player.STR)) 
    random.choice(enemyturn) 
    if random.choice == "1": 
     Player.H -= Goblin.STR 
     print("The Goblin strikes you for {0} damage.".format(Player.STR)) 
     if random.choice == "2": 
      pass 
      Combat() 
+0

哪條線返回此錯誤? – Marcin

+2

@Marcin這一個:'Goblin.H - = Player.STR' – Jivan

回答

3

你在呼喚你的屬性實例化對象之前。

您應該將它們聲明爲類變量。

class Enemy: 
    name = "" 
    H = 0 
    STR = 0 

    def __init__(self, name, H, STR): 
     self.name = name 
     self.H = H 
     self.STR = STR 

    def is_alive(self): 
     return self.H > 0 


class Goblin(Enemy): 
    def __init__(self, name, H, STR): 
     self.name = "Goblin" 
     self.H = 50 
     self.STR = 5 
+0

是的,它固定它。謝謝。 –

相關問題