【问题标题】:creating a monster battle with a multisided dice class使用多边骰子类创建怪物战斗
【发布时间】:2021-04-27 17:41:01
【问题描述】:

我正在制作一个带有多边骰子类和 while 循环的基本战斗程序。基本上,你和一条龙开始时有 100 点生命值,然后随机掷出一个 20 面的骰子来确定你们各自造成的伤害。例如,如果我掷出 6,而龙掷出 8,那么我将剩下 92 点生命值,而龙将剩下 94 点生命值。这将一直持续到一个健康值达到 0。下面是我的代码示例:

import random

class MSDie:
    def __init__(self, num_sides):
        self.num_sides = num_sides
        self.current_value = self.roll()

    def roll(self):
        self.current_value = random.randrange(1,self.num_sides+1)
        return self.current_value
    
    def __int__(self):
        return int(self.current_value)
    
    def getValue(self):
        return self.value

    def setValue(self, value):
        self.value = value

    
print("you are a legendary hero on a quest to save the kingdom from an evil dragon.")
print("You find him outside his lair and the two of you begin to battle.")
print("Both you and the dragon start with 100 health.")
print("You each will both randomly deal between 1-20 damage.")
print("The first whose health drops to 0 loses.")
playerHealth = 100
DragonHealth = 100

while(playerHealth > 0 or DragonHealth > 0):
    print("You have ", playerHealth, "Health.")
    print("The dragon has ", DragonHealth, "Health.")
    myDamage = MSDie(20)
    DragonDamage = MSDie(20)
    playerHealth = playerHealth - DragonDamage
    DragonHealth = DragonHealth - myDamage
    print("")
    print("You dealt ", myDamage, "to the dragon.")
    print("The dragon dealth ", DragonDamage, "to you.")
    print("")

但是,每次我尝试从健康中减去伤害掷骰时,我都会不断收到此错误:

File "C:\Users\Jacks\OneDrive\Documents\Python files\Ds files\monsterbattle.py", line 52, in <module>
    playerHealth = playerHealth - DragonDamage

TypeError: unsupported operand type(s) for -: 'int' and 'MSDie'

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    DragonDamageMSDie 类的一个实例。即使您实现了__int__ 方法,Python 也不会自动将其转换为整数。您需要明确地执行此操作。试试playerHealth - int(DragonDamage)

    【讨论】:

      【解决方案2】:
      import random
      
      class MSDie:
          def __init__(self, num_sides):
              self.num_sides = num_sides
              self.current_value = self.roll()
      
          def roll(self):
              self.current_value = random.randrange(1,self.num_sides+1)
              return self.current_value
          
          def __int__(self):
              return int(self.current_value)
          
          def getValue(self):
              return self.value
      
          def setValue(self, value):
              self.value = value
      
          
      
      print("you are a legendary hero on a quest to save the kingdom from an evil dragon.")
      print("You find him outside his lair and the two of you begin to battle.")
      print("Both you and the dragon start with 100 health.")
      print("You each will both randomly deal between 1-20 damage.")
      print("The first whose health drops to 0 loses.")
      playerHealth = 100
      DragonHealth = 100
      
      while(playerHealth > 0 or DragonHealth > 0):
          print("You have ", playerHealth, "Health.")
          print("The dragon has ", DragonHealth, "Health.")
          myDamage = MSDie(20)
          DragonDamage = MSDie(20)
          playerHealth = playerHealth - int(DragonDamage)
      

      正如上面@mcsoini 建议的那样,您需要显式转换为int。

      【讨论】:

        【解决方案3】:

        我建议您大量简化您的代码。您只需要一个 MSDie 实例,而不是每次循环迭代中的 2 个,并且骰子可能只提供值,它不需要内部值。你也可以在你的情况下使用and,当一个到达0时停止

        class MSDie:
            def __init__(self, num_sides):
                self.num_sides = num_sides
        
            def roll(self):
                return random.randrange(1, self.num_sides + 1)
        

        使用

        player_health = 100
        dragon_health = 100
        multi_dice = MSDie(20)
        
        while player_health > 0 and dragon_health > 0:
            player_damage = multi_dice.roll()
            dragon_damage = multi_dice.roll()
            player_health -= dragon_damage
            dragon_health -= player_damage
            print("You dealt ", player_damage, "to the dragon, it now have", dragon_health)
            print("The dragon dealth ", dragon_damage, "to you, you now have", player_health)
            print("")
        
        if player_health > dragon_health:
            print(f"You won with {player_health}PV, dragon is dead")
        else:
            print(f"You loose, you're dead, dragon has {dragon_health}PV")
        

        【讨论】:

          猜你喜欢
          • 2012-12-10
          • 2013-11-14
          • 2019-01-30
          • 2011-09-20
          • 2016-11-26
          • 1970-01-01
          • 2015-09-27
          • 2011-09-08
          • 1970-01-01
          相关资源
          最近更新 更多