http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label14
开发一个<人狗大战>游戏,通过函数实现
''' 函数 制作一个人狗大战游戏。 人:名字,血量,攻击力,性别 狗:名字,血量,攻击力,品种 ''' def Person(name,hp,attack,gender): dic = {'name': name, 'health': hp, 'attack': attack, 'gender': gender} def fight(dog): dog['health'] -= dic['attack'] print('%s攻击了%s,%s掉了%s点血'%(dic['name'],dog['name'],dog['name'],dic['attack'])) dic['fight'] = fight return dic def Dog(name,hp,attack,kind): dic = {'name': name, 'health': hp, 'attack': attack, 'kind': kind} def bite(person): person['health'] -= dic['attack'] print('%s攻击了%s,%s掉了%s点血.'%(dic['name'],person['health'],person['name'],dic['attack'])) dic['bite'] = bite return dic # 互相打斗 alex = Person('alex',300,30,'buxiang') hei = Dog('xiaohei',200,20,'hashiqi') # print(alex) # print(hei) alex['fight'](hei) print(hei['health']) hei['bite'](alex) print(alex['health'])