面对对象练习
import time dic = {} Controls = {"1": "查看角色", "2": "修炼", "3": "战斗"} class game: def __init__(self, n, g, s): self.name = n self.age = g self.sex = s def Make_cultivation(self): print("%s 你修炼了一会战力加:100, 血量加:100" % self.name) dic["CP"] += 100 dic["Vitality"] += 100 dic["HP"] = dic["Vitality"] def Beat_monsters(self): print("%s 你参加了打怪,生命值减:—500 剩余血量:%s" % (self.name, dic["HP"])) dic["HP"] -= 500 if dic["HP"] <= 0: print("%s 你死了!" % self.name) return False else: print("%s 打怪成功!" % self.name) return True def HP(self): if dic["HP"] < dic["Vitality"]: time.sleep(1) dic["HP"] += 1 def info(self): print("%s 年龄 %s 性别 %s 战力 %s 总血量 %s 剩余血量%s" % (dic["name"], dic["age"], dic["sex"], dic["CP"], dic["Vitality"], dic["HP"])) while True: if not dic: name = input("请输入用户名:") age = input("请输入年龄:") sex = input("请输入性别:") dic["name"] = name dic["age"] = age dic["sex"] = sex dic["Vitality"] = 100 dic["CP"] = 100 dic["HP"] = 100 print("创建角色成功!初始血量100,初始战力100") else: name = dic["name"] age = dic["age"] sex = dic["sex"] obj = game(name, age, sex) obj.HP() for i in Controls: print(i, Controls[i]) input_Controls = input("请选择操作:") if input_Controls == "1": obj.info() elif input_Controls == "2": obj.Make_cultivation() elif input_Controls == "3": flag = obj.Beat_monsters() if not flag: break