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 = weaclass 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 = 0while 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
相关推荐
zhuquan0 2020-05-25
class Singleton: def __new__: # 关键在于这,每一次实例化的时候,我们都只会返回这同一个instance对象 if not hasattr: cls.instance =
lhxxhl 2020-05-16
PHP学习笔记 2020-05-07
DCXabc 2020-05-01
bcbeer 2020-05-02
fly00love 2020-03-08
liusarazhang 2020-03-06
zhuxianfeng 2020-03-03
Kwong 2020-03-01
ladysosoli 2020-03-01
liugan 2020-02-25
Dimples 2020-02-14
wangqing 2020-02-13
fanhuasijin 2020-02-03