python類例子(開槍)

qq_38709565發表於2018-07-13
程式碼
class Person(object):
    """人的類"""

    def __init__(self, name):
        super(Person, self).__init__()
        self.name = name
        self.gun = None  # 用來儲存槍物件的引用
        self.hp = 100

    def anzhuang_zidan(self, dan_jia_temp, zi_dan_temp):
        """把子彈裝到彈夾中"""

        # 彈夾.儲存子彈(子彈)
        dan_jia_temp.baocun_zidan(zi_dan_temp)

    def anzhuang_danjia(self, gun_temp, dan_jia_temp):
        """把彈夾安裝到槍中"""

        # 槍.儲存彈夾(彈夾)
        gun_temp.baocun_danjia(dan_jia_temp)

    def naqiang(self, gun_temp):
        """拿起一把槍"""
        self.gun = gun_temp

    def __str__(self):
        if self.gun:
            return "%s的血量為:%d, 他有槍 %s" % (self.name, self.hp, self.gun)
        else:
            if self.hp > 0:
                return "%s的血量為%d, 他沒有槍" % (self.name, self.hp)
            else:
                return "%s 已掛...." % self.name

    def kou_ban_ji(self, diren):
        """讓槍發射子彈去打敵人"""
        # 槍.開火(敵人)

        self.gun.fire(diren)

    def xiao_xue(self, sha_shang_li):
        """根據殺傷力,掉相應的血量"""
        self.hp -= sha_shang_li


class Gun(object):
    """槍類"""

    def __init__(self, name):
        super(Gun, self).__init__()
        self.name = name  # 用來記錄槍的型別
        self.danjia = None  # 用來記錄彈夾物件的引用

    def baocun_danjia(self, dan_jia_temp):
        """用一個屬性來儲存這個彈夾物件的引用"""
        self.danjia = dan_jia_temp

    def __str__(self):
        if self.danjia:
            return "槍的資訊為:%s, %s" % (self.name, self.danjia)
        else:
            return "槍的資訊為:%s,這把槍中沒有彈夾" % (self.name)

    def fire(self, diren):
        """槍從彈夾中獲取一發子彈,然後讓這發子彈去擊中敵人"""

        # 先從彈夾中取子彈
        # 彈夾.彈出一發子彈()
        zidan_temp = self.danjia.tanchu_zidan()

        # 讓這個子彈去傷害敵人
        if zidan_temp:
            # 子彈.打中敵人(敵人)
            zidan_temp.dazhong(diren)
        else:
            print("彈夾中沒有子彈了。。。。")


class Danjia(object):
    """彈夾類"""

    def __init__(self, max_num):
        super(Danjia, self).__init__()
        self.max_num = max_num  # 用來記錄彈夾的最大容量
        self.zidan_list = []  # 用來記錄所有的子彈的引用

    def baocun_zidan(self, zi_dan_temp):
        """將這顆子彈儲存"""
        self.zidan_list.append(zi_dan_temp)

    def __str__(self):
        return "彈夾的資訊為:%d/%d" % (len(self.zidan_list), self.max_num)

    def tanchu_zidan(self):
        """彈出最上面的那顆子彈"""
        if self.zidan_list:
            return self.zidan_list.pop()
        else:
            return None


class Zidan(object):
    """子彈類"""

    def __init__(self, sha_shang_li):
        super(Zidan, self).__init__()
        self.sha_shang_li = sha_shang_li  # 這顆子彈的威力

    def dazhong(self, diren):
        """讓敵人掉血"""

        # 敵人.掉血(一顆子彈的威力)
        diren.xiao_xue(self.sha_shang_li)


def main():
    """用來控制整個程式的流程"""

    # 1. 建立老王物件
    laowang = Person("老王")

    # 2. 建立一個槍物件
    ak47 = Gun("AK47")

    # 3. 建立一個彈夾物件
    dan_jia = Danjia(20)

    # 4. 建立一些子彈
    for i in range(15):
        zi_dan = Zidan(10)

        # 5. 老王把子彈安裝到彈夾中
        # 老王.安裝子彈到彈夾中(彈夾,子彈)
        laowang.anzhuang_zidan(dan_jia, zi_dan)

    # 6. 老王把彈夾安裝到槍中
    # 老王.安裝彈夾到槍中(槍,彈夾)
    laowang.anzhuang_danjia(ak47, dan_jia)

    # test:測試彈夾的資訊
    # print(dan_jia)

    # test:測試槍的資訊
    # print(ak47)

    # 7. 老王拿槍
    # 老王.拿槍(槍)
    laowang.naqiang(ak47)

    # test:測試老王物件
    print(laowang)

    # 8. 建立一個敵人
    gebi_laosong = Person("隔壁老宋")
    print(gebi_laosong)

    # 9. 老王開槍打敵人
    # 老王.扣扳機(隔壁老宋)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)
    laowang.kou_ban_ji(gebi_laosong)
    print(gebi_laosong)
    print(laowang)


if __name__ == '__main__':
    main()

執行結果:


相關文章