2011-06-09 56 views
0

我想知道如何解決這個問題,我用我的第一塊OOP代碼。問題在於Snake類的攻擊方法。我在比賽中有兩條蛇,並試圖讓蛇去攻擊另一條蛇。目前我使用兩個變量來記錄Snake的輪到它,然後使用它來嘗試攻擊另一個蛇。但是這不起作用。任何人有任何想法如何解決這個問題?非常感謝。Python面向對象編程新手

class Snake: 
    hp=100 
    attack=25 
    defense=1 
    def set_name(self, name): 
     self.name=name 

    def shed(self): 
     self.defense=self.defense+1 

    def attack(self, opposite, current): 
     opposite.hp=opposite.hp-(current.attack-opposite.defense) 

    def eat(self): 
     self.attack=self.attack+5 
     print(str(self.name) + " eats a rat!") 
     print(str(self.name) + "'s attack dmg is now " + str(self.attack)) 

    def sleep(self): 
     print (str(self.name) + " goes to sleep") 
     self.hp=self.hp+10 
     if self.hp>100: 
      self.hp=100 
     print (str(self.name) + " wakes up with " + str(self.hp) + "hp") 

##initialises the snakes 
alpha=Snake() 
beta=Snake() 
## gives the snakes names of the user's choice 
alpha_name=raw_input("What would you like to name your snake? ") 
alpha.set_name(alpha_name) 
beta_name=raw_input("What would you like to name the other snake? ") 
beta.set_name(beta_name) 
##starts the game 
turn=True 
while alpha.hp>0 and beta.hp>0: 
    while turn==True: 
     opposite="beta" 
     current="alpha" 
     action=raw_input("attack, sleep, eat or shed? ") 
     try: 
      if action=="attack": 
       alpha.attack(opposite, current) 
      if action=="sleep": 
       alpha.sleep() 
      if action=="eat": 
       alpha.eat() 
      if action=="shed": 
       alpha.shed() 

      turn=False 
     except IOError: 
      print("Please chose only one action, exaclty how it is typed") 
    while turn==False: 
     opposite="alpha" 
     current="beta" 
     if beta.hp<15: 
      beta.sleep() 
     elif alpha.hp>75: 
      beta.attack() 
     else: 
      index=random.randint(1, 3) 
      if index==1: 
       beta.shed() 
      elif index==2: 
       beta.eat() 
      else: 
       beta.attack(opposite, current)  
     turn=True 
+3

「不工作」是完全無用的報告。會發生什麼,如果發生錯誤,您準備發生什麼,確切的消息和追溯等等。 – delnan 2011-06-09 14:27:29

+7

請告訴我們究竟發生了什麼。問一個問題的好方法是:1.問題背景2.你期望發生什麼3.發生什麼4.有問題的代碼 – JackMc 2011-06-09 14:28:07

+0

@JackMc:允許竊取該評論並使用它? – Trufa 2011-06-09 14:38:46

回答

2
在 「攻擊」

您嘗試訪問 「opposite.hp」,但這種方法是帶一個字符串,而不是一個對象:

opposite="alpha" 
current="beta" 

=>更改爲

opposite=alpha 
current=beta 

另外,還有一個字段和方法同名:攻擊。我建議將該字段重命名爲「攻擊點」或其他內容。

此外,您稱爲「beta.attack()」。你忘記了那裏的方法論點。

+0

非常感謝您的意見。我得到它的工作感謝你們:) – Sean 2011-06-10 05:35:24

1

當您發生beta攻擊時,您正在調用方法attack(),但沒有任何參數。我想,你希望beta.attack(alpha,beta)

但你可能會重構,只需要對方作爲參數的方法(因爲你知道是誰在攻擊(這是調用的攻擊方法)的對象)

def attack(self, opposite): 
    opposite.hp -= self.attack-opposite.defense 
2

我見兩個問題。首先是你傳遞變量的名稱而不是變量本身。

改變這一點:

while alpha.hp>0 and beta.hp>0: 
    while turn==True: 
     opposite="beta" 
     current="alpha" 
     action=raw_input("attack, sleep, eat or shed? ") 
     try: 
      if action=="attack": 
       alpha.attack(opposite, current) 

這樣:

while alpha.hp>0 and beta.hp>0: 
    while turn==True: 
     opposite=beta 
     current=alpha 
     action=raw_input("attack, sleep, eat or shed? ") 
     try: 
      if action=="attack": 
       alpha.attack(opposite, current) 

此外,您必須在蛇類定義了兩次攻擊領域。

class Snake: 
    attack=25 

    def attack(self, opposite, current): 

這就是我想出了你的代碼打後:

import random 

class Snake: 
    hp=100 
    attack_skill=25 
    defense=1 

    def set_name(self, name): 
     self.name=name 

    def shed(self): 
     self.defense=self.defense+1 

    def attack(self, opposite): 
     opposite.hp = opposite.hp - (self.attack_skill - opposite.defense) 

    def eat(self): 
     self.attack_skill += 5 
     print(str(self.name) + " eats a rat!") 
     print(str(self.name) + "'s attack dmg is now " + str(self.attack_skill)) 

    def sleep(self): 
     print (str(self.name) + " goes to sleep") 
     self.hp=self.hp+10 
     if self.hp>100: 
      self.hp=100 
     print (str(self.name) + " wakes up with " + str(self.hp) + "hp") 


##initialises the snakes 
alpha=Snake() 
beta=Snake() 
## gives the snakes names of the user's choice 
alpha_name=raw_input("What would you like to name your snake? ") 
alpha.set_name(alpha_name) 
beta_name=raw_input("What would you like to name the other snake? ") 
beta.set_name(beta_name) 
##starts the game 
turn=True 
while alpha.hp>0 and beta.hp>0: 
    while turn==True: 
     opposite="beta" 
     current="alpha" 
     action=raw_input("attack, sleep, eat or shed? ") 
     try: 
      if action=="attack": 
       alpha.attack(beta) 
      if action=="sleep": 
       alpha.sleep() 
      if action=="eat": 
       alpha.eat() 
      if action=="shed": 
       alpha.shed() 

      turn=False 
     except IOError: 
      print("Please chose only one action, exaclty how it is typed") 
    while turn==False: 
     opposite="alpha" 
     current="beta" 
     if beta.hp<15: 
      beta.sleep() 
     elif alpha.hp>75: 
      beta.attack(alpha) 
     else: 
      index=random.randint(1, 3) 
      if index==1: 
       beta.shed() 
      elif index==2: 
       beta.eat() 
      else: 
       beta.attack(alpha) 
     turn=True 
+0

獎金OOP點你會寫一個'TurnBasedFight'類。 – 2011-06-09 15:51:39

+0

@Jochen Real OOP會要求你編寫一個基類'Animal',從中派生'Reptiles'類,從你派生出最後的'Snakes'類。當蛇贏得'TurnBasedFight'(應該是一個派生自「Fight」的類,其中'RealTimeFight'也被派生出來)時,你可能還想打印一些東西。 – 2011-06-09 16:16:17

+0

@Jonno_FTW:不,「真正的」OOP並不意味着你寫儘可能多的班級,你可以。另一個管理輪流和輸入戰鬥的班級只會讓事情變得更簡單。 – 2011-06-09 16:43:32