2016-03-09 73 views
0

好吧我一直在運行我的前額到一些代碼,我已經確定我只是在黑暗中拍攝此時。出於某種原因讓類點擊是一個真正的屁股疼痛,我想如果我使用自己的個人代碼,它可能是有道理的。所以這裏有一個我寫的試驗小腳本遊戲。 (它很粗糙,我相當新):https://github.com/Villagesmithy/Zombies-.git瞭解類和__Call__方法Py 2.7

我覺得要了解我在做什麼錯我必須讓它在我自己的代碼中指出。我確信有很多類似於我的問題,但他們悲傷地看着我的頭。

我得到的錯誤是這樣的:

Attribute Error: Combat instance has no __Call__ method 

這裏就是我認爲是問題類:

class Combat: 

    def battle(self): 

     print start.player, " has: ", hp, "." 
     print "The zombie has: ", zombie.zom_health() 
     time.sleep(1) 

     if haf == True: 

      total_damage = hero_attacks * hero_damage 
      zombie.zom_health() - total_damage 
      print "You strike the zombie for %d damage!" %(total_damage) 
      print "The zombie's health is %d" %zombie.zom_health() 
      return zombie.zom_health()    
      time.sleep(3) 

     elif haf == False: 
      total_damage = zombie.zom_damage()- hero.hero_armor() 
      if total_damage > 0: 
       total_damage - hp 
       return hp 
       print "A zombie shambles through the baricade and damages you!" 
       print "You lost %d hp! Your hp is now: %d" %(total_damage, hp) 
       combat_loop() 
       time.sleep(3) 

      elif total_damage <= 0: 
       print "A zombie lurches at you but misses!" 
       time.sleep(3) 
       combat_loop() 

     else: 
      z.zom_killed() 

    def initv(battle): 
     bat = battle() 

     hero_init = random.randint(1,20) 
     zom_init = random.randint(1,20) 


     if hero_init >= zom_init: 
      #global haf Ignoring for now 
      haf = True 
      print "You attack first!" 
      bat.battle() 
     elif hero_init < zom_init: 
      #global haf 
      haf = False 
      print "The zombie attacks!" 
      bat.battle() 

    def start(): #The only fucking code that works 
     global zombies 
     zombies = random.randint(20,30) 
     arm = random.sample(armor,1) 
     wea = random.sample(weapon, 1) 

     player = raw_input("What is your name?") 
     print player, ",\n" 
     print "Your colony is under attack by %s zombies!" %(zombies) 
     print "Hold them off so the others can escape." 
     print "You are wearing an ", arm, "and weilding a", wea 
     time.sleep(3) 



    def combat_loop(): 
     combat_call = Combat() 

     while zombies > 0: 
      combat_call.initv() 

     if zombies <= 0: 
      print "You held off the zombies off long enough to escape!" 
      print "With the way clear you grab your belongings and follow suit." 
      print "The end!" 
      sys.exit() 

現在,如果你說這嘖嘖有老兄不知道他在做什麼你會是對的!我只是希望你們能幫助我做點擊。整個計劃可能需要以我不知道的賭注來焚燒。任何幫助你可以提供幫助。

+0

請您提供您的問題充分回溯。 – zondo

+2

你的縮進是正確的嗎?看起來像''Combat''是一個沒有任何東西的類...... – MSeifert

+0

'initv'需要繼承自我。一個好的IDE會爲你找到這些錯誤,我建議你嘗試一下PyCharm。 – Signal

回答

3

我假設initv實際上是Combat的方法,但你忘了把它拿self作爲參數,給它一個名爲battle代替參數。

當你調用bat.initv(),它傳遞selfbattleself是公約的名稱;方法的第一個位置參數是self無論你決定將它命名)。因此,當您在initv中執行bat = battle()時,與self()的做法是一樣的,即試圖將您的類的實例視爲可調用對象。

從我所知道的,真正的目標是調用battle方法,所以initv的定義,第一行應該是:

def initv(self): 
    bat = self.battle() 

這下標準名稱經過self,然後調用battle方法。這是一個有點不清楚什麼battle方法返回(這似乎在兩個代碼路徑隱式返回None,和任何第三代碼路徑,它在它sleep從未發生感謝return搶佔它zombie.zom_health()回報率),但這代碼有很多問題,很難確定「正確」的行爲是什麼。

爲了記錄,這個錯誤幾乎肯定是抱怨缺少__call__方法,而不是__Call__; the special method that lets instances of a class act as callables themselves is all lower case.

0

combat_loop中,您將combat_call定義爲Combat()。目前combat_callCombat的一個實例。然後在你的while循環中,你說combat_call.initv()。由於combat_callCombat的實例,因此這是Combat.initv(combat_call)的快捷方式。也就是說,combat_callinitv()的唯一參數。在initv()中,您採用一個參數:battle。在這種情況下,battlecombat_call是相同的東西,它是Combat的實例。然後你說bat = battle()battle已經是Combat的一個實例,所以你試圖調用一個實例。一些實例可以被調用,但爲此,他們需要定義一個__call__方法。你的班級沒有,所以有一個錯誤。我認爲,不要以battle作爲參數,取self。然後將bat定義爲self.battle()

0

還有一件事,我不認爲zombie.zom_health() - total_damage行將工作,除非你在Zombie類實現__sub__方法。即使如此,我認爲這隻會在殭屍和英雄每個人都擁有相同的父類時說:'Human'。但我希望不會。 '有人'將不得不測試...在我測試更多修復你的英雄課程之前,我需要先進行一些睡眠。 :)也許Zombie - Hero可能工作。當殭屍類中的對象(即Z1Z2)都來自殭屍類時,此示例從您的殭屍類重新開始工作。所以... Hero.total_damage()也可能工作。我當然不知道。

import random 

class Zombie: 

    def __init__(self, zom_health=None): 
     self.__zom_health = None 
     if zom_health: 
      self.__zom_health = zom_health 
     else: 
      self.randomize_zom_health() 
     self.__zom_damage = 0 

    def randomize_zom_health(self): 
     zom_hp = random.randint(20,35) 
     if zom_hp <= 0: 
      print zom_killed 
      self.__zom_health = 0 
     else: 
      self.__zom_health = zom_hp 

    def __sub__(self, Other): 
     self.__zom_health -= Other.zom_damage() #or Other.total_damage()? 

    def zom_health(self): 
     return self.__zom_health 

    def zom_damage(self, damage=None): 
     if damage: self.__zom_damage = damage 
     return self.__zom_damage 

>>> reload(Zombie) 
>>> z1 = Zombie.Zombie(20) 
>>> z2 = Zombie.Zombie(zom_health=30) 
>>> z1.zom_health() 
20 
>>> z2.zom_health() 
30 
>>> z2.zom_damage(z2.zom_health()) 
30 
>>> z1 - z2 
>>> z1.zom_health() 
-10 
>>>