我正在为我的游戏开发人员创建代码。需要创建5个敌人和常规玩家实例的类。我已经得到它所以它创建了其中一个的5个实例,但是以<__main__.Player object at 0x020E3410>
作为另一个回来。它们都具有相同的属性。我的代码:
class Player(object):
def __init__(self, nm, am, wp, ht, tp, ide):
self.name = nm
self.ammo = am
self.weapon = wp
self.health = ht
self.type = tp
self.id = ide
class Enemy(object):
def __init__(self, nm, am, wp, ht, tp, ide):
self.name = nm
self.ammo = am
self.weapon = wp
self.health = ht
self.type = tp
self.id = ide
def __str__(self):
values = "Hi my name is " + self.name + "\n" + "Ammo: " + str(self.ammo) + "\n" + "Weapon: " + self.weapon + "\n" + "Health: " + str(self.health) + "\n" + "I am a: " + self.type + "\n" + "ID #: " + str(self.id)
return values
def __str__(self):
values1 = "Hi my name is " + self.name + "\n" + "Ammo: " + str(self.ammo) + "\n" + "Weapon: " + self.weapon + "\n" + "Health: " + str(self.health) + "\n" + "I am a: " + self.type + "\n" + "ID #: " + str(self.id)
return values1
def main():
Enemies = 0
Players = 0
while Players < 5:
play1 = (Player("Joe", 5, "Machine gun", 22, "Hero", Players))
print (play1)
Players = Players + 1
break
while Enemies < 5:
ene1 = (Enemy("Julian", 100, "Rocket Launcher", 1337, "Enemy", Enemies))
print (ene1)
Enemies = Enemies + 1
main()
答案 0 :(得分:1)
您的Enemy
课程有两种__str__
方法,但您的Player
课程没有:
class Player(object):
def __init__(self, nm, am, wp, ht, tp, ide):
self.name = nm
self.ammo = am
self.weapon = wp
self.health = ht
self.type = tp
self.id = ide
class Enemy(object):
def __init__(self, nm, am, wp, ht, tp, ide):
self.name = nm
self.ammo = am
self.weapon = wp
self.health = ht
self.type = tp
self.id = ide
def __str__(self):
values = "Hi my name is " + self.name + "\n" + "Ammo: " + str(self.ammo) + "\n" + "Weapon: " + self.weapon + "\n" + "Health: " + str(self.health) + "\n" + "I am a: " + self.type + "\n" + "ID #: " + str(self.id)
return values
def __str__(self):
values1 = "Hi my name is " + self.name + "\n" + "Ammo: " + str(self.ammo) + "\n" + "Weapon: " + self.weapon + "\n" + "Health: " + str(self.health) + "\n" + "I am a: " + self.type + "\n" + "ID #: " + str(self.id)
return values1
你可能想要:
class Player(object):
def __init__(self, nm, am, wp, ht, tp, ide):
self.name = nm
self.ammo = am
self.weapon = wp
self.health = ht
self.type = tp
self.id = ide
def __str__(self):
values1 = "Hi my name is " + self.name + "\n" + "Ammo: " + str(self.ammo) + "\n" + "Weapon: " + self.weapon + "\n" + "Health: " + str(self.health) + "\n" + "I am a: " + self.type + "\n" + "ID #: " + str(self.id)
return values1
class Enemy(object):
def __init__(self, nm, am, wp, ht, tp, ide):
self.name = nm
self.ammo = am
self.weapon = wp
self.health = ht
self.type = tp
self.id = ide
def __str__(self):
values = "Hi my name is " + self.name + "\n" + "Ammo: " + str(self.ammo) + "\n" + "Weapon: " + self.weapon + "\n" + "Health: " + str(self.health) + "\n" + "I am a: " + self.type + "\n" + "ID #: " + str(self.id)
return values
请注意,为了尽量减少重复的代码,您可以创建基类,如:
class BasePlayer(object):
def __init__(self, nm, am, wp, ht, tp, ide):
self.name = nm
self.ammo = am
self.weapon = wp
self.health = ht
self.type = tp
self.id = ide
def __str__(self):
values = "Hi my name is " + self.name + "\n" + "Ammo: " + str(self.ammo) + "\n" + "Weapon: " + self.weapon + "\n" + "Health: " + str(self.health) + "\n" + "I am a: " + self.type + "\n" + "ID #: " + str(self.id)
return values
然后使用继承来创建Enemy
和Player
:
class Enemy(BasePlayer):
# enemy related stuff
class Player(BasePlayer):
# goody related stuff