2014-03-06 60 views
0

創建戰鬥系統所以這是代碼非常簡單的Python遊戲

# TEST.PY 

import sys 
import random 

class Fight(object): 
    def enter(self): 
     print "\n", "-" * 10 
     print "There are two muchachos:" 
     print "MUCHACHO1" 
     print "MUCHACHO2" 
     print "One of them looks wounded or something." 

     your_hit_points = 20 
     muchacho1_hit_points = 6 
     muchacho2_hit_points = 11 
     muchacho1 = True 
     muchacho2 = True 


     while your_hit_points > 0 or (not muchacho1 and not muchacho2): 
      print "\n", "-" * 10 
      your_attack = random.randint(4,12) 
      muchacho1_attack = random.randint(1,4) 
      muchacho2_attack = random.randint(4,8) 


      attack = int(raw_input("Type 1 to attack MUCHACHO1, 2 to attack MUCHACHO2 >")) 

      if attack == 1: 
       muchacho1_hit_points - your_attack 
       print "You hit MUCHACHO1 for %d hit points." % your_attack 

       if muchacho1_hit_points <= 0 and muchacho1: 
        muchacho1 = False 
        print "MUCHACHO1 killed!" 
       else: 
        pass 

      elif attack == 2: 
       muchacho2_hit_points - your_attack 
       print "You hit MUCHACHO2 for %d hit points." % your_attack 

       if muchacho2_hit_points <= 0 and muchacho2: 
        muchacho2 = False 
        print "MUCHACHO2 killed!" 
       else: 
        pass 

      else: 
       print "DOES NOT COMPUTE" 
       pass 

      your_hit_points - muchacho1_attack 
      print "MUCHACHO1 hit you for %d points, you have %d hit points left." % (muchacho1_attack, your_hit_points) 

      your_hit_points - muchacho2_attack 
      print "MUCHACHO2 hit you for %d points, you have %d hit points left." % (muchacho2_attack, your_hit_points) 

     exit(1) 

    a_fight = Fight() 
    a_fight.enter() 

我有什麼困難。基本上,WHILE循環永遠不會結束,似乎每個人的生命值都不減。我有一種感覺,在我的腸道中,我失去了一些非常非常基本的東西,因爲我現在已經編寫了幾個小時,所以我可能看不到簡單的東西。

我知道這可以通過使用更多的類或功能更好地完成,但現在我想這樣做(對80 + char行也抱歉)。

+0

一個忠告,儘量經常測試你的代碼,你將看到的問題更快,你會知道問題出在哪裏 –

+0

當我死了muchacho時,我討厭它。如果我在他們殺了我之前設法殺死他們,那麼他們的鬼魂就會永遠打敗我,即使我死了。 (請檢查你的while邏輯) – cmd

回答

0

我並不是說它已經完成,或者我擁有最好的代碼風格,但是我對代碼做了一些改進(順便說一句,遊戲很好)。 我看到的一些錯誤: 你的循環錯了(當你或至少一個muchacho仍然活着時,你應該運行循環)。 每次造成傷害時,您都需要覆蓋muchacho1_hit_points/muchacho2_hit_points。 你需要檢查muchacho在每輪之前是否還活着。 這個類沒有意義,你可以使用一個函數。 python pass什麼都不做,在你的情況下可以忽略;它通常用於聲明稍後使用的容器類。 一些改進,你可以這樣做:catch異常的情況下,用戶不輸入整數(現在崩潰)

import random 

class Fight(object): 
    def enter(self): 
    print "\n", "-" * 10 
    print "There are two muchachos:" 
    print "MUCHACHO1" 
    print "MUCHACHO2" 
    print "One of them looks wounded or something." 

    your_hit_points = 20 
    muchacho1_hit_points = 6 
    muchacho2_hit_points = 11 
    muchacho1 = True 
    muchacho2 = True 

    while your_hit_points > 0 and (muchacho1 or muchacho2): 
     print "\n", "-" * 10 
     your_attack = random.randint(4,12) 
     muchacho1_attack = random.randint(1,4) 
     muchacho2_attack = random.randint(4,8) 

     attack = int(raw_input("Type 1 to attack MUCHACHO1, 2 to attack MUCHACHO2 >")) 
     if attack == 1: 
     if muchacho1: 
      muchacho1_hit_points = muchacho1_hit_points - your_attack 
      print "You hit MUCHACHO1 for %d hit points." % your_attack 
      if muchacho1_hit_points <= 0: 
      muchacho1 = False 
      print "MUCHACHO1 killed!" 
     else: 
      print "MUCHACHO 1 is already dead!" 
     elif attack == 2: 
     if muchacho2: 
      muchacho2_hit_points = muchacho2_hit_points - your_attack 
      print "You hit MUCHACHO2 for %d hit points." % your_attack 
      if muchacho2_hit_points <= 0: 
      muchacho2 = False 
      print "MUCHACHO2 killed!" 
     else: 
      print "MUCHACHO 2 is already dead!" 
     else: 
     print "DOES NOT COMPUTE" 

     if muchacho1: 
     your_hit_points = your_hit_points - muchacho1_attack 
     print ("MUCHACHO1 hits you for %d points, you have %d" 
      " hit points left." %(muchacho1_attack, your_hit_points)) 
     if your_hit_points <= 0: 
      print 'You are dead' 
      break 
     if muchacho2: 
     your_hit_points = your_hit_points - muchacho2_attack 
     print ("MUCHACHO2 hits you for %d points, you have %d" 
      " hit points left." % (muchacho2_attack, your_hit_points)) 
     if your_hit_points <= 0: 
      print 'You are dead' 

a_fight = Fight() 
a_fight.enter() 
+0

是的,我注意到我的WHILE循環有錯誤的條件..我仍然有一點點問題。它是一個類的原因是因爲它是遊戲的一部分,我只是粘貼了相關的代碼。我會研究例外情況。 – user3056783

1

我認爲你想要做的是muchacho1_hit_points -= your_attackmuchacho2相同。現在你只是放棄減法的結果。

+0

哦,就是這樣。我知道我錯過了一些小事。我很累,謝謝。 – user3056783