記錄#
今天的練習是關於編寫遊戲角色的類別。雖然不算難,但是有些地方可能會讓您感到困惑。練習的要求如下:
- 我的遊戲需要一個角色,包括姓名、生命值和魔法值。
- 在初始化角色時,需要設定這些值。
- 需要一個方法來輸出這些資料。
- 需要創建一個名為
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("大衛", "100", "50", "3")
david.print()
david.alive()
Boris = attribute("鮑里斯", "45", "70", "吸血鬼", "3", "夜晚")
Boris.print()
Rishi = attribute("里西", "45", "10", "吸血鬼", "75", "白天")
Rishi.print()
Ted = tracker("泰德", "75", "40", "獸人", "80", "45")
Ted.print()
Station = tracker("斯泰森", "75", "40", "獸人", "80", "50")
Station.print()
Bill = tracker("比爾", "60", "5", "獸人", "75", "90")
Bill.print()