【Python】200行的文字RPG游戏

import time,os,random

class 人物:
    def __init__(self,name,maxhp=100,ac=10,speed=1.0,money=0):
        self.name=name
        self.hp=maxhp
        self.maxhp=maxhp
        self.minac=int(ac*0.5)
        self.maxac=int(ac*1.5)
        self.speed=speed
        self.exp=0
        self.money=money
        self.maxexp=100
        self.rank=0
        self.challenge=0
        
    def rankup(self):
        self.maxhp+=10+self.rank
        self.minac+=random.randint(2,8)
        self.maxac+=random.randint(4,12)
        self.hp=self.maxhp
        self.speed+=0.1
        self.exp-=self.maxexp
        self.maxexp=int(self.maxexp*1.1)
        self.rank+=1

    def printa(self):
        print('【%s】' %self.name)
        print('等级:%d(%d/%d)' %(self.rank,self.exp,self.maxexp))
        print('生命值:%d/%d' %(self.hp,self.maxhp))
        print('攻击力:%d-%d' %(self.minac,self.maxac))
        print('速度:%.1f' %self.speed)
        print('金钱:%d' %self.money)
        print('劲敌等级:%d' %self.challenge)
        print()

    def ac(self):
        return random.randint(self.minac,self.maxac)

    def save(self):
        s=self.name+'\n'+str(self.rank)+'\n'+str(self.exp)+'\n'+str(self.maxexp)+'\n'+str(self.hp)+'\n'
        s+=str(self.maxhp)+'\n'+str(self.minac)+'\n'+str(self.maxac)+'\n'+str(self.speed)+'\n'+str(self.money)+'\n'
        s+=str(self.challenge)
        with open('hero.save','w') as f:
            f.write(s)
        print('数据保存完毕!\n')
        
def 加载勇士():
    global hero
    if os.path.exists('hero.save'):
        try:
            with open('hero.save','r') as f:
                d=f.read().split('\n')
            hero=人物(d[0])
            hero.rank=int(d[1])
            hero.exp=int(d[2])
            hero.maxexp=int(d[3])
            hero.hp=int(d[4])
            hero.maxhp=int(d[5])
            hero.minac=int(d[6])
            hero.maxac=int(d[7])
            hero.speed=float(d[8])
            hero.money=int(d[9])
            hero.challenge=int(d[10])
            print('欢迎回来,勇士!')
            print()
            return 0
        except Exception as e:
            print('存档读取出错:',e)
            print()
            
    hero=人物(input('勇士,取名:'))
    
def 战斗_伤害事件(A,B):
    global times
    times+=1
    ac=B.ac()
    A.hp-=ac
    print('<%d>\n【%s】攻击【%s】!\n【%s】受到 %d 点伤害!\n【%s】剩余生命值:%d\n' %(times,B.name,A.name,A.name,ac,A.name,A.hp))
    if A.hp<=0:
        return 1
    else:
        return 0
    
def 获取经验系数函数(x):
    if x<5:
        return -0.198*x+1
    if x>=5:
        return 0.01
    
def 去打怪(who):
    global times
    try:
        s2=int(input('你想挑战多少等级的怪物?(>0)'))
    except:
        s2=1
        
    print()
    if s2>0:
        os.system('cls')
        print('挑战【%d】级怪物开始!\n' %s2)
        if who.challenge<s2:
            who.challenge=s2
            
        monster=人物('怪物'+str(s2)+'号',s2*50,s2*8,s2/5+1,s2*random.randint(0,20))
        exp=int(s2*100*获取经验系数函数(who.rank-s2))
        if s2%10==0:
            monster.hp+=40
            monster.minac+=6
            monster.maxac+=12
            monster.money+=s2//10*200
            exp*=2
            print('这个怪物异常强大!')

        times=0
        while monster.hp>0 and who.hp>0:
            if monster.speed>who.speed:
                if 战斗_伤害事件(who,monster):
                    break
                time.sleep(0.5)
                if 战斗_伤害事件(monster,who):
                    break
                time.sleep(0.5)
            else:
                if 战斗_伤害事件(monster,who):
                    break
                time.sleep(0.5)
                if 战斗_伤害事件(who,monster):
                    break
                time.sleep(0.5)
                
        if who.hp>0:
            if monster.money>0:
                print('你成功的消灭了怪物,经验和钱包得到了增长!')
            else:
                print('你成功的消灭了怪物,经验得到了增长!')
            who.exp+=exp
            
            ranktmp=0
            while who.exp>=who.maxexp:
                who.rankup()
                ranktmp+=1
            if ranktmp==1:
                print('【%s】的力量发生了质变!\n' %who.name)
            elif ranktmp>1 and ranktmp<100:
                print('【%s】的力量得到了惊人的提升!\n' %who.name)
            elif ranktmp>=100:
                print('【%s】一定是开了作弊器!\n' %who.name)
                
            who.money+=monster.money
        else:
            print('很遗憾勇士,你被怪物杀死了...游戏结束...')
            print()
            return True
    else:
        print('没有更弱的怪物了,勇士。')
    print()
    return False

def 寻求治疗(who):
    os.system('cls')
    if who.hp<who.maxhp and who.money>0:
        if who.money>=who.maxhp-who.hp:
            print('在圣光的洗礼下,你恢复如初!')
            who.money-=who.maxhp-who.hp
            who.hp=who.maxhp
        else:
            print('由于你的金钱不够,牧师只治疗了你部分伤势……真是黑暗!')
            who.hp+=who.money
            who.money=0
    else:
        print('你的心灵得到了治愈...')
    print()

加载勇士()
#hero=人物(input('勇士,取名:'))

gameover=False

while gameover==False:
    print('1、去打怪')
    print('2、查看属性')
    print('3、寻求治疗')
    print('4、保存存档')
    print()
    s=input('勇士你现在打算干嘛?(输入序号执行对应操作)')
    print()

    if s=='1':
        gameover=去打怪(hero)
    elif s=='3':
        寻求治疗(hero)
    elif s=='4':
        os.system('cls')
        hero.save()
    else:
        os.system('cls')
        hero.printa()
        
input('-- Game Over --')
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值