Title
Fork me on GitHub

面向对象demo-人狗大战游戏设计

# Author:yujunyu
# -*- codeing = utf-8 -*-
# @Time :2022/4/22 19:21
# @Author :yujunyu
# @Site :
# @File :dog_person.py
# @software: PyCharm



attack_val={
    "京巴":30,
    "藏獒":80,
}

def dog(name, d_type):#模板
    data={
     "name": name,
     "d_type":d_type,
     # "attack_val":30,
     "life_val":100
    }
    if d_type in attack_val:
        data["attack_val"] = attack_val[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# 为了从函数外部可以调用这个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 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"]=beat
    return data

d1=dog('hxs','京巴')#实体
d2=dog('hxs2','藏獒')
p1=person("yjy",19)
d1["bite"](p1)
p1["beat"](d2)
print(d1)

posted @ 2022-05-05 09:44  新西兰蟹老板  阅读(49)  评论(0编辑  收藏  举报  来源