2017-08-06 108 views
-3

我一直在努力通過Python的艱難的方式書,它提出了寫作戰系統的挑戰如何退出戰鬥系統?

我想知道如何打破'贏''條款或移動到下一個場景的while循環。
目前,遊戲在用戶'死亡'時起作用,但在擊敗對手雙方時,遊戲以錯誤消息結束。

File "fight_test.py", line 146, in <module> 
    a_game.play() 
File "fight_test.py", line 20, in play 
    next_scene_name = current_scene.enter() 
AttributeError: 'NoneType' object has no attribute 'enter' 

while health > 0 and (customer1 or customer2): 
      #While loop runs whilst health > 0 and one of the customers remains. 
      print "\n", "-" * 20 
      your_attack = randint(4,12) 
      customer1_attack = randint(1,4) 
      customer2_attack = randint(3,6) 
      if customer2_hitpoints == 0 and customer1_hitpoints == 0: 
       return 'Win' 

      attack = int(raw_input("Type 1 to attack Customer 1, 2 to attack Customer 2 >")) 
      if attack == 1: 
       if customer1: 
        customer1_hitpoints = customer1_hitpoints - your_attack 
        print "You refuse service to Customer 1, %d damage." % your_attack 
        if customer1_hitpoints <= 0: 
         customer1 = False 
         print "Customer 1 is removed from the building." 
       else: 
        print "Customer 1 has already been removed and left an angry Trip Advisor review." 
      elif attack == 2: 
       if customer2: 
        customer2_hitpoints = customer2_hitpoints - your_attack 
        print "You tell customer 2 that they have had too much already, %d damage." % your_attack 
        if customer2_hitpoints <= 0: 
         customer2 = False 
         print "Customer 2 falls over and throws up, they are promptly removed from the building." 
       else: 
        print "They have already left to get a late night burger." 
      else: 
       print "Why are you just doing nothing?" 

      if customer1: 
       health = health - customer1_attack 
       print "Customer 1 tells you that the service is better in Wetherspoons." 
       print "It causes %d damage and you have d% health left." % (customer1_attack, health) 
       if health <= 0: 
        return 'death' 
        break 

      if customer2: 
       health = health - customer2_attack 
       #print Fight.quotes[randint(0, len(self.quips)-1)] 
       print "The customer tells you \"This isn't how I make this drink at home!\"" 
       print "It causes %d damage and you have %d health left." % (customer2_attack, health) 
       if health <= 0: 
        return 'death' 
        break 
+0

中斷或返回將退出循環...您似乎已經有那些 –

+4

具體是什麼問題?這是很多代碼,通過 –

+1

嗨,歡迎來到堆棧溢出!雖然爲你遇到的代碼提供上下文總是很有用,「[越多的代碼可以通過,越不可能找到你的問題](http://stackoverflow.com/help/mcve )」。你可以試着把它分解爲[Minimal,Complete和Verifiable示例](https://stackoverflow.com/help/mcve)? – GoBusto

回答

0

您正在進入戰鬥,我相信,和death可以在某些時候它可能會加載該場景被退回......

scenes = { 
    'fight' : Fight(), 
    'death' : Death() 
} 

How might I implement a method to exit the while loop in such a case and move to another scene/print a win statement?

怎麼樣將'Win' : Win()添加到場景圖中?

關於錯誤

AttributeError: 'NoneType' object has no attribute 'enter'

您還可能要爲None(不'None')場景或檢查當前場景中存在

if not current_scene: 
    print "Done!" 
    exit(0) 
next_scene_name = current_scene.enter() 

而且,這實際上是檢查是否的customer2還活着並且客戶1已經死了

if customer2_hitpoints and customer1_hitpoints == 0: 

你需要明確檢查兩者是否爲零

+0

歡呼蟋蟀。我發現死於遊戲的作品,並打印出退出聲明。剛剛在場景地圖上添加了「Win」,並做了if檢查,看看是否都是== 0.可悲的是,如果擊敗兩個敵人,我仍然會收到錯誤消息。 – Kynan

+0

嗯。什麼錯誤? –

+0

我得到這個:文件 「fight_test.py」,線路146,在 a_game.play() 文件 「fight_test.py」,第20行,在遊戲 next_scene_name = current_scene.enter() AttributeError的: 'NoneType'對象沒有任何屬性'輸入' 只是試圖解決現在如何將它連接到'贏'場景。 – Kynan