題目:
請寫一個小游戲,人狗大戰,2個人物,人和狗,游戲開始,生成2個人,3條狗,互相混戰,人被狗咬了會掉血(生命值減少),狗被人打了也掉血(生命值減少)。
注意,狗和人的攻擊力和生命值是不一樣的。
基類Role定義如下:
class Role(object):
def __init__(self, name):
self.name = name
def attack(self, enemy): # 攻擊敵人的方法
enemy.life_value -= self.agressivity
要求:?
(1)請根據題意定義子類Dog和People。
(2)并編寫測試代碼,輸出人狗混戰后各自的生命值。class Role(object):
def __init__(self, name):
self.name = name
def attack(self, enemy): # 攻擊敵人的方法
enemy.life_value -= self.agressivity
class Dog(Role):
life_value = 50
agressivity = 10
class People(Role):
life_value = 100
agressivity = 5
if __name__ == '__main__':
people1 = People('people1')
people2 = People('people2')
dog1 = Dog('dog1')
dog2 = Dog('dog2')
dog3 = Dog('dog3')
# 混戰開始
dog1.attack(people2)
people1.attack(dog3)
people2.attack(dog1)
dog1.attack(people2)
dog2.attack(people2)
dog2.attack(people1)
dog3.attack(people1)
# 輸出各自的生命值
print('{}的生命值為:{}'.format(dog1.name,dog1.life_value))
print('{}的生命值為:{}'.format(dog2.name, dog2.life_value))
print('{}的生命值為:{}'.format(dog3.name, dog3.life_value))
print('{}的生命值為:{}'.format(people1.name, people1.life_value))
print('{}的生命值為:{}'.format(people2.name, people2.life_value))