人狗大戰


 

定義兩個類(人,狗)實現人狗大戰

(版本1)

class Dog:
    def __init__(self,name,hp,ad,kind):  # 初始化方法  設置狗的參數:名字,血量,攻擊,種類
        self.name = name   # 實例變量   對象屬性
        self.hp = hp
        self.ad = ad
        self.type = kind

    def bite(self, person):     # 定義 狗咬人 的方法
        person.hp -= self.ad    # 人的血量減去狗的攻擊就是人新的血量
        print('%s咬了%s,%s掉了%s點血' % (self.name, person.name, person.name, self.ad))

class Person:
    def __init__(self,name,hp,ad,sex):  # 初始化方法    設置人的參數:名字,血量,攻擊,性別
        self.name = name        # 實例變量   對象屬性
        self.hp = hp
        self.ad = ad
        self.sex = sex

    def fight(self,dog):  # 動態變量  方法   人打狗
        # 狗掉血,就是人的攻擊力
        dog.hp -= self.ad
        print('%s攻擊了%s,%s掉了%s點血'%(self.name,dog.name,dog.name,self.ad))

hei = Dog('小黑',300,20,'哈士奇')   # 小黑 對象 實例
alex = Person('alex',100,10,'male')  # alex 對象 實例

print(hei.hp)    # 300
print(alex.hp)   # 100

hei.bite(alex)   # 小黑咬了alex,alex掉了20點血
alex.fight(hei)  # alex攻擊了小黑,小黑掉了10點血

print(hei.hp)    # 290
print(alex.hp)   # 80

 

(版本2)增加了武器類,添加了人購買和裝備武器的方法,添加了武器作用方法  【使用了類的組合】

class Person:
    def __init__(self,name,hp,ad,sex):
        self.name = name
        self.hp = hp
        self.ad = ad
        self.sex = sex
        self.money = 2000.8      #給人一個金額
        self.beg = []               #給人一個空背包

    def fight(self,dog):
        dog.hp -= self.ad
        print('%s打了%s,%s掉了%s點血'%(self.name,dog.name,dog.name,self.ad))

    def buy_weapon(self,weapon):               #添加了買武器的方法
        if weapon.price <= self.money:         #判斷武器價格是否小於人所有的金額
            self.money -= weapon.price         #求得余額
            self.money = round(self.money,2)   #余額四舍五入,保留兩位小數
            self.beg.append(weapon)            #背包中加入購買的武器
            print('購買%s成功,您當前的余額:%s'%(weapon.name,self.money))
        else:
            print('您的余額不足,請充值之后重試')

    def use_weapon(self):             #添加了使用武器的方法
        for weap in self.beg:         #將武器從背包中拿出
            if weap.t == 'weapon':    #判斷背包中拿出的是不是武器
                self.weapon = weap    # 組合
                self.hp += weap.hp    #人的血量獲得武器加成
                self.ad += weap.ad    #人的攻擊獲得武器加成
                break

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

    def bite(self,person):
        person.hp -= self.ad
        print('%s咬了%s,%s掉了%s點血'%(self.name,person.name,person.name,self.ad))

class Weapon:     #新定義了一個武器類
    t = 'weapon'  #靜態變量
    def __init__(self,name,price,ad,hp):
        self.name = name
        self.price = price
        self.ad = ad
        self.hp = hp

    def throw(self,dog,person):    #在武器類中添加使用武器的方法
        dog.hp -= self.ad
        print('%s被%s扔中了,掉了%s點血,當前%s的血量為%s'%(dog.name,person.name,self.ad,dog.name,dog.hp))

alex = Person('alex',100,10,'male')   #傳入人的參數
hei = Dog('小黑',300,20,'藏獒')       #傳入狗的參數
zhuan = Weapon('板磚',2000,200,100)   #傳入武器的參數
alex.buy_weapon(zhuan)                #人 買    武器
alex.use_weapon()                     #人 裝備  武器
print(alex.weapon.name)               #查看人裝備的武器名稱
print(alex.__dict__)                  #查看人此時狀態
alex.weapon.throw(hei,alex)           #人對狗使用武器   

 

 

(版本3)在版本1基礎上進行了類的繼承

class Animal(object):       #父類  (含有人與狗共有的屬性)
    def __init__(self,name,hp,ad):
        self.name = name
        self.hp = hp
        self.ad = ad
class Dog(Animal):  #繼承
    def __init__(self,name,hp,ad,kind):
        super().__init__(name,hp,ad)
        self.kind = kind    #狗的種類

    def bite(self,person):
        person.hp -= self.ad
        print('%s攻擊了%s,%s掉了%s點血' % (self.name, person.name, person.name, self.ad))

class Person(Animal):
    def __init__(self,name,hp,ad,sex):
        super().__init__(name,hp,ad)
        self.sex = sex  #人的性別

    def fight(self,dog):
        dog.hp -= self.ad
        print('%s攻擊了%s,%s掉了%s點血'%(self.name,dog.name,dog.name,self.ad))

hei = Dog('小黑',300,20,'哈士奇')
alex = Person('alex',100,10,'male')
alex.fight(hei)
print(hei.hp)
hei.bite(alex)
print(alex.hp)

 


免責聲明!

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



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