如何退出战斗系统?

时间:2017-08-06 15:40:16

标签: python

我一直在研究Python the Hard way book,它提出了编写战斗系统的挑战

我想知道如何打破'win'子句的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

1 个答案:

答案 0 :(得分:0)

我相信你正在进入战斗,并且death可以在某些时候返回,这可能会加载那个场景...

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

在这种情况下如何实现退出while循环的方法并移动到另一个场景/打印win语句?

如何将'Win' : Win()添加到场景地图中?

关于错误

  

AttributeError:&#39; NoneType&#39;对象没有属性&#39;输入&#39;

您可能还需要None的场景(不是'None')或检查您当前的场景是否存在

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

此外,这实际上是检查customer2是否还活着且customer1已经死了

if customer2_hitpoints and customer1_hitpoints == 0:

您需要明确检查两者是否为零