记录#
今天的练习是关于编写游戏人物的类。虽然不算难,但是有些地方可能会让您感到困惑。练习的要求如下:
- 我的游戏需要一个角色,包括姓名、生命值和魔法值。
- 在初始化角色时,需要设置这些值。
- 需要一个方法来输出这些数据。
- 需要创建一个名为
Player
的子类,它继承自Character
类,并且还有一个额外的生命值属性。 Player
类还应该有一个方法来检查角色的状态,并返回 “是” 或 “否”。- 还应该创建一个名为
Enemy
的子类,它有额外的类型
和力量
属性。 Enemy
类应该有两个子类:Orc
,它有一个速度
属性。Vampire
,它有一个白天/黑夜
跟踪器属性。
- 实例化一个
Player
对象、两个Vampire
对象和三个Orc
对象。您可以选择它们的姓名。 - 打印出它们的属性值。
CODE#
class gamer:
gname = None
health = None
magic = None
def __init__(self, gname, health, magic):
self.gname = gname
self.health = health
self.magic = magic
def print(self):
print(f"{self.gname} > {self.health} > {self.magic}")
class player(gamer):
alive = None
def __init__(self, gname, health, magic, alive):
self.gname = gname
self.health = health
self.magic = magic
self.alive = alive
def alive(self):
if self.alive:
print("alive No")
return True
else:
print("alive Yes")
return True
def print(self):
print(f"{self.gname} > {self.health} > {self.magic} > {self.alive}")
class enemy(gamer):
type = None
strength = None
def __init__(self, gname, health, magic, type, strength):
self.gname = gname
self.health = health
self.magic = magic
self.type = type
self.strength = strength
def print(self):
print(
f"{self.gname} > {self.health} > {self.magic} > {self.type} > {self.strength}"
)
class attribute(enemy):
orc = "speed"
def __init__(self, gname, health, magic, type, strength, orc):
self.gname = gname
self.health = health
self.magic = magic
self.type = type
self.strength = strength
self.orc = orc
def print(self):
print(
f"{self.gname} > {self.health} > {self.magic} > {self.type} > {self.strength} > {self.orc}"
)
class tracker(enemy):
Speed = None
def __init__(self, gname, health, magic, type, strength, Speed):
self.gname = gname
self.health = health
self.magic = magic
self.type = type
self.strength = strength
self.Speed = Speed
def print(self):
print(
f"{self.gname} > {self.health} > {self.magic} > {self.type} > {self.strength} > {self.Speed}"
)
david = player("david", "100", "50", "3")
david.print()
david.alive()
Boris = attribute("Boris", "45", "70", "Vampire", "3", "Night")
Boris.print()
Rishi = attribute("Rishi", "45", "10", "Vampire", "75", "day")
Rishi.print()
Ted = tracker("Ted", "75", "40", "orc", "80", "45")
Ted.print()
Station = tracker("Station", "75", "40", "orc", "80", "50")
Station.print()
Bill = tracker("Bill", "60", "5", "orc", "75", "90")
Bill.print()