Python 面向对象--人狗大战(代码)

项目实训 人狗大战

    多个人,多条狗,多个品种,年龄,攻击力

可参考教程:https://blog.csdn.net/Aluxian_/article/details/134141861?spm=1001.2014.3001.5502

attack_vals = {
    "二哈":20,
    "藏獒":70,
    "金毛":40
}
 
 
def dog(name, d_type):
    data = {
        "name":name,
        "d_type":d_type,
        "life_val":100
    }
 
    if d_type in attack_vals:
        data["attack_val"] = attack_vals[d_type]
    else:
        data["attack_val"] = 15
 
    def dog_bite(person_obj):
        person_obj["life_val"] -= data["attack_val"]
        print("狗[%s]咬了人[%s]一口,人掉血[%s],还有血量[%s]..." % (data['name'],
                                                      person_obj["name"],
                                                      data["attack_val"],
                                                      person_obj["life_val"]))
 
    data["bite"] = dog_bite
 
    return data
 
def person(name,age):
    data = {
        "name": name,
        "age": age,
        "life_val": 100
    }
    if age >= 18:
        data["attack_val"] = 50
    else:
        data["attack_val"] = 30
 
    def person_beat(dog_obj):
        dog_obj["life_val"] -= data["attack_val"]
        print("人[%s]打了狗[%s]一棒,狗掉血[%s],还有血量[%s]..." % (data['name'],
                                                      dog_obj["name"],
                                                      data["attack_val"],
                                                      dog_obj["life_val"]))
 
    data["beat"] = person_beat
    return data
 
 
 
d1 = dog("zbc1","二哈")
d2 = dog("zbc2","藏獒")
d3 = dog("zbc3","金毛")
 
p1 = person("hga",18)
p2 = person("hag2",15)
p3 = person("hga3",19)
 
d1["bite"](p1)
p1["beat"](d1)
d1["bite"](p1)
d1["bite"](p1)
p1["beat"](d1)

你可能感兴趣的:(其他,python,开发语言)