这是我的密码:
# pokemon1
class Pokemon(object):
def __init__(self,name,hp,damage):
self.name = name
self.hp = hp
self.damage = damage
def fight(self,other):
if(self.hp > 0):
print("%s did %d damage to %s"%(self.name,self.damage,other.name))
other.hp -= self.damage
if (other.hp > 0):
print("%s has %d hp left" % (other.name, other.hp))
else:
print("%s has died" % (other.name))
return other.fight(self)
else:
print("%s wins! (%d hp left)"%(other.name,other.hp))
return other,self
class pikachu(Pokemon):
def __init__(self):
super(pikachu, self).__init__('pikachu', 100, 10)
class pidgy(Pokemon):
def __init__(self):
super(pidgy, self).__init__('pidgy', 200, 12)
#main
import pokemon1
pikachu = pokemon1.pikachu
pidgy = pokemon1.pidgy
p = pokemon1.Pokemon
p.fight(pikachu,pidgy)
Reinderien是对的,但我尝试了您的代码,发现了一个不同的问题,您设计的类与您尝试使用它的方式不同。你知道吗
此代码应该起作用:
您需要在最后一行中调用Pokemon(),而不是Pokemon来调用构造函数。你知道吗
相关问题 更多 >
编程相关推荐