依赖关系

# class Person:
#     def play(self, tools): # 通过参数的传递把另外一个类的对象传递进来
#         tools.run()
#         print("很开心, 我能玩儿游戏了")
#
#
# class Computer:
#     def run(self):
#         print("电脑开机. 可以运行")
#
# class Phone:
#     def run(self):
#         print("手机开机, 可以运行")
#
#
# c = Computer()
# phone = Phone()
#
# p = Person()
# p.play(phone)



# 写一个植物大战僵尸
# 1. 植物
#       打僵尸. 僵尸掉血
# 2. 僵尸
#       吃植物. 植物掉血

class Plant:
    def __init__(self, name, hp, ad): # 200
        self.name = name
        self.hp = hp
        self.ad = ad

    def attack(self, js):
        print("植物攻击僵尸")
        js.hp -= self.ad
        print(f"僵尸掉血{self.ad}, 还剩下{js.hp}")

class JiangShi:
    def __init__(self, name, hp, ad): # 1000  800
        self.name = name
        self.hp = hp
        self.ad = ad

    def attack(self, zw):
        print("僵尸咬植物")
        zw.hp -= self.ad
        print(f"植物掉血{self.ad}, 还剩{zw.hp}")


# 植物
wd = Plant("歪脖子豌豆", 10, 20)
# 僵尸
js = JiangShi("铁桶僵尸", 200, 1)
wd.attack(js)
wd.attack(js)
wd.attack(js)
wd.attack(js)
wd.attack(js)

js.attack(wd)
js.attack(wd)
js.attack(wd)
js.attack(wd)

  

posted on 2018-12-19 19:44  =.===  阅读(269)  评论(0编辑  收藏  举报

导航