需要有关此代码的帮助。我不知道为什么在我运行它时会抛出这个错误 -
Traceback (most recent call last):
File "python", line 130, in <module>
File "python", line 111, in encounterModule
File "python", line 100, in CombatModule
TypeError: 'int' object is not callable
#Combat/exp system test of concept
import time
print("Welcome to the Combat Concept Testing Module!")
print("What would you like your Hero to be called?")
nameHero = input(">")
class Hero:
'Hero class'
def __init__(self):
#Constructor to build object
self.name = "Placeholder" #Must Declare name after creation of object
self.maxhitpoints = 150
self.hitpoints = self.maxhitpoints
self.defPower = 20
self.AD = 30
self.exp = 0
self.lvl = 1
self.threshold = 5 ** self.lvl
self.isAlive = True
self.maxenergy = 50
self.energy = self.maxenergy
def basicattack(self,target):
#First level attack function
self.attack = self.AD
target.hitpoints = target.hitpoints - self.attack
print("Target striked!")
def heavyattack(self,target):
if self.lvl == 2:
#Second level attack function
self.attack = self.AD * 1.2
target.hitpoints = target.hitpoints - self.attack
print("Target heavily striked!")
else:
#If level isn't high enough, attack will not occur.
pass
def printStats(self):
#This module prints our hero's stats.
print("HP: %d" % (self.maxhitpoints))
print("DefPower: %d" % (self.defPower))
print("Attack Damage: %d" % (self.AD))
print("XP: %d" % (self.exp))
print("LVL: %d" % (self.lvl))
print("Current HP: %d" % (self.hitpoints))
class badguy1:
'Jokes on you'
def __init__(self):
self.hitpoints = 80
self.AD = 20
self.expworth = 10
self.isAlive = True
def attack(self,target):
self.attack = self.AD
target.hitpoints = target.hitpoints - self.attack
def printStat(self):
print(self.hitpoints)
def counterattack(self,target):
target.hitpoints = target.hitpoints - self.AD
def expgain(recp,givE): #recp = recipient; givE = person donating experience after demise
recp.exp = recp.exp + givE.expworth
print(recp.exp)
def lvlUp(in1):
if in1.exp >= in1.threshold:
in1.lvl = in1.lvl + 1
else:
pass
def isAliveTest(testin):
if testin.hitpoints > 0:
testin.isAlive = True
else:
testin.isAlive = False
def CombatModule(fighter,fightee): #Fighter initiates fight
print("What is your first strike?")
choice = input("X for a basic strike, Y for a advanced strike")
if choice == "X" or "x":
fighter.basicattack(fightee)
print("Target HP: %d" % fightee.hitpoints)
elif choice == "Y" or "y":
fighter.heavyattack(fightee)
print("Target HP: %d" % fightee.hitpoints)
else:
print("Sorry you aren't able to do that")
if fightee.isAlive == True:
fightee.attack(fighter)
print("You've been struck")
print("Current HP: %d" % fighter.hitpoints)
else:
pass
def encounterModule(attacker,attackee):
while True:
if attacker.isAlive == True:
if attackee.isAlive == True:
CombatModule(attacker,attackee)
else:
print("The target is dead!")
break
else:
print("You have died!")
break
print("The hero's name will be %s" % nameHero)
me = Hero()
me.name = nameHero
en = badguy1()
encounterModule(me,en)
是的,所以我不明白为什么它会抛出这个错误。代码在通过combatmodule函数扫描2次后运行正常,然后在第二次运行时,它会抛出该错误。
答案 0 :(得分:1)
badguy1().attack
可以引用当前类定义中的两件事。变量self.attack
或方法attack(self,target)
。您的代码在使用哪个attack
时会感到困惑,并且使用了错误的代码。更改方法或变量名称以修复此