python對象的屬性引用另一個類的


class GameRole:
def __init__(self, name, ad, hp):
self.name = name
self.ad = ad
self.hp = hp

def attack(self, p):
p.hp = p.hp - self.ad
if p.hp > 0:
print("%s攻擊%s,%s掉了%s點血,還剩%s點血"
% (self.name, p.name, p.name, self.ad, p.hp))
else:
print(p.name + "死亡")

def arm_weapon(self, wea):
self.wea = wea


class Weapon:
def __init__(self, name, ad):
self.name = name
self.ad = ad

def fight(self, p1, p2):
p2.hp = p2.hp - self.ad
if p2.hp > 0 and p1.hp > 0:
print("%s用%s打了%s,%s掉了%s點血,還剩下%s點血"
% (p1.name, self.name, p2.name, p2.name, self.ad, p2.hp))
else:
p2.hp = 0
# print(p2.name + "死亡,游戲結束")

p1 = GameRole("蓋倫", 20, 500)
p2 = GameRole("亞索", 50, 400)
axe = Weapon("斧頭", 60)
sward = Weapon("屠龍寶刀", 100)

p1.arm_weapon(axe)
p2.arm_weapon(sward)

count = 0
while 1: # 你們開始打吧,哈哈,管我毛事
print("第%s回合" % (count + 1))

if p1.hp > 0:
p1.wea.fight(p1, p2)

if p2.hp > 0:
p2.wea.fight(p2, p1)

if p1.hp <= 0:
print("%s用%s把%s打死了,%s獲勝,游戲結束,讓我們恭喜%s!"
% (p2.name, p2.wea.name, p1.name, p2.name, p2.name))
break

if p2.hp <= 0:
print("%s用%s把%s打死了,%s獲勝,游戲結束,讓我們恭喜%s!"
% (p1.name, p1.wea.name, p2.name, p1.name, p1.name))
break

count += 1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM