2017-08-06 38 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