猫史档案馆


【Python作品分享】类的运用

用户:ALCOR-ALCOR-查看:0 回复:0 评论:0 创建时间:2021-01-01T09:10:16


【作品展示】

center_image

 

【作品介绍】

第一天学class的运用,胃好疼啊,整整打了3h,有没有什么可以改进的,各位dalao请指点一下...

 

【作品源代码】


class Simulation:
    """简单尝试模拟。"""

    def __init__(self, name, kind, age):
        """初始化属性。"""
        import random
        self.name = name
        self.kind = kind
        self.age = age
        self.hp = random.randint(20,25)
        self.mp = random.randint(0,15)
        self.attack = random.randint(2,5)
        self.level = 1
        self.exp = 0
        self.hard = 20

    def check(self):
        import time
        print(f"尝试获取‘{self.name}’信息...")
        time.sleep(1)
        try:
            print(f"获取成功!\n数值如下:")
            time.sleep(1)
            print(f" Name: {self.name}(LV.{self.level})")
            time.sleep(0.1)
            print(f" Kind: {self.kind}")
            time.sleep(0.1)
            print(f" Age: {self.age}")
            time.sleep(0.1)
            print(f" HP: {self.hp}")
            time.sleep(0.1)
            print(f" Mp: {self.mp}")
            time.sleep(0.1)
            print(f" Atk: {self.attack}")
            time.sleep(0.1)
            print(f" Exp: {self.exp}/{self.hard}")
        except:
            print("获取失败!")
    
    def was_harm(self,harm):
        self.hp=self.hp-harm
        if harm>0 :
            print(f"{self.name}受到了{harm}点伤害,还剩下{self.hp}点生命。")
        if harm<0 :
            print(f"{self.name}被治愈了,恢复{harm}点生命。")
    
    def level_up(self,exp):
        self.exp = self.exp + exp
        for exp_a in range(1,6):
            if self.exp >= self.hard:
                self.exp = self.exp - self.hard
                self.level = self.level + 1
                self.hard = self.hard*1.1//1 +self.age*0.05//1
                self.hp = self.hp*1.2//1 +self.level*0.02//1 +self.attack*0.25//1 +self.age*0.05//1
                self.mp = self.mp*1.1//1 +self.hp*0.01//1 +self.level*0.05//1 +self.attack*0.15//1 +self.age*0.05//1
                self.attack = self.attack +self.hp*0.01//1 +self.mp*0.05//1 +self.level*0.5//1 +self.age*0.05//1
def control():
    n=0
    while True:
        try:
            choice=input("\n1.创建一个实例。\n2.函数check试用。\n3.函数was_harm试用。\n4.函数level_up试用。\n")
            if choice == '1':
                n=n+1
                name_choice=input("姓名:")
                kind_choice=input("种族:")
                age_choice=input("年龄:")
                try:
                    if n==1:
                        age_choice=int(age_choice)
                        goodman=Simulation(name_choice,kind_choice,age_choice)
                    if n==2:
                        age_choice=int(age_choice)
                        bifgirl=Simulation(name_choice,kind_choice,age_choice)
                    if n==3:
                        age_choice=int(age_choice)
                        pfs=Simulation(name_choice,kind_choice,age_choice)
                    if n>=4:
                        try:
                            n=input("要重建的位【1/2/3】 ")
                            n=int(n)
                            if n==1:
                                age_choice=int(age_choice)
                                goodman=Simulation(name_choice,kind_choice,age_choice)
                            if n==2:
                                age_choice=int(age_choice)
                                bifgirl=Simulation(name_choice,kind_choice,age_choice)
                            if n==3:
                                age_choice=int(age_choice)
                                pfs=Simulation(name_choice,kind_choice,age_choice)
                        except:
                            print("重建失败!")
                            n=4
                        else:
                            print("重建成功!")
                            n=4
                except:
                    print("创建失败!")
                else:
                    print("创建成功!")
            if choice == '2':
                choice_twice=input("要分析的位【1/2/3】 ")
                try:
                    if choice_twice == '1':
                        goodman.check()
                    elif choice_twice == '2':
                        bifgirl.check()
                    elif choice_twice == '3':
                        pfs.check()
                except:
                    print(f"{choice_twice}位空空如也!")
            if choice == '3':
                harm=input("受到的伤害/治愈:")
                harm=int(harm)
                try:
                    choice_twice=input("要接受的位【1/2/3】 ")
                    if choice_twice == '1':
                        goodman.was_harm()
                    elif choice_twice == '2':
                        bifgirl.was_harm()
                    elif choice_twice == '3':
                        pfs.was_harm()
                except:
                    print(f"{choice_twice}位空空如也!")
            if choice == '4':
                exp=input("获得的经验:")
                exp=int(exp)
                try:
                    choice_twice=input("要接受的位【1/2/3】 ")
                    if choice_twice == '1':
                        goodman.level_up(exp)
                    elif choice_twice == '2':
                        bifgirl.level_up(exp)
                    elif choice_twice == '3':
                        pfs.level_up(exp)
                except:
                    print(f"{choice_twice}位空空如也!")
            if choice == '5':
                break
        except:
            print("输入有错!")

control()
            






















print("\n\n\n")

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页