【发布时间】: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