# 人狗大战
# 角色模型
# 人的模型
def Person(name,blood,aggr,sex):
person = {
"name": name ,
"blood": blood, # 血量
"aggr": aggr, # 攻击
"sex": sex,
}
def attack(dog):
dog["blood"] -= person["aggr"]
print("%s 被 %s打了,失去了%s 血" % (dog["name"], person["name"], person["aggr"]))
person["attack"] = attack # 增加一个技能
return person
alex= Person("狗剩儿",100,1,"不详")
nezha= Person("糯扎",200,2,"不详")
print(nezha)
# 用函数定义一个东西,方便增加人物,方便修改,更加规范,人模子
# 狗的模型
def Dog(name,blood,aggr,kind):
dog = {
"name": name ,
"blood": blood,
"aggr": aggr,
"kind": kind,
}
def bite(person):
person["blood"] -= dog["aggr"]
print("%s 被 %s咬了,失去了%s 血" % (person["name"], dog["name"], dog["aggr"]))
dog["bite"] = bite
return dog
jin= Dog("金星",100,1,"teddy")
suyang= Dog("苏阳",1,1,"狗东西")
print(suyang)
suyang["bite"](nezha)
nezha["attack"](suyang)
# {'name': '糯扎', 'blood': 200, 'aggr': 2, 'sex': '不详', 'attack': <function Person.<locals>.attack at 0x00000000028702F0>}
# {'name': '苏阳', 'blood': 1, 'aggr': 1, 'kind': '狗东西', 'bite': <function Dog.<locals>.bite at 0x0000000002870488>}
# 糯扎 被 苏阳咬了,失去了1 血
# 苏阳 被 糯扎打了,失去了2 血