标题:c语言RPG游戏
只看楼主
拉拉面
Rank: 1
来 自:广州
等 级:新手上路
帖 子:26
专家分:5
注 册:2018-9-22
结帖率:100%
 问题点数:0 回复次数:1 
c语言RPG游戏
// AFFAIR_H//////////////////////////main.cpp///////////////////////////////////
#include "casting.h"
#include "affair.h"
using namespace std;
int main()
{
    srand(static_cast<unsigned>(time(NULL)));
    character *car=NULL;
    freak *fak=new freak;
    bool setting=false;
    bool decide=false;
    string m_name;
    string freak_name[5]={"山猪","黑狼","血鹰","僵尸","古墓亡灵"};
    cout<<"欢迎来玩这个游戏,如发现有任何BUG请大家发到C++贴吧我的贴子上,谢谢!"<<endl;
    while(true)
    {
        mainmenu(setting);
        int select;
        cout<<"请选择菜单编号:";
        cin>>select;
        if(!cin)
        {
            cin.clear();
            cin.ignore(1024,'\n');
        }
        switch(select)
        {
        case 1:
            cout<<"请为你的角色取个名:";
            cin>>m_name;
            car=new character(m_name);
            setting=true;
            decide=true;
            break;
        case 2:
            if(setting==true)
                car->saveplan();
            else
                cout<<"还没有开始游戏!"<<endl;
            break;
        case 3:
            car=new character;
            if(car->readplan())
            {
                setting=true;
                decide=true;
                 }
            break;
        case 4:
            if(setting!=true)
            {
                cout<<"没有这个选项,请重新选择!"<<endl;
                continue;
            }
            break;
        case 0:
            delete car;
            delete fak;
            return 0;
        default:
            cout<<"没有这个选项,请重新选择!"<<endl;
            continue;
        }
        if(select==1||select==3||select==4)
        {
            while(decide)
            {
                secondmenu();
                cout<<"请选择菜单编号:";
                cin>>select;
                if(!cin)
                {
                    cin.clear();
                    cin.ignore(1024,'\n');
                    continue;
                }
                switch(select)
                {
                case 1:
                    fak=new freak(freak_name[(car->getrank()/10)%5],car->getrank());
                    cout<<"遇到怪物!"<<endl;
                    fak->showcasting();
                    break;
                case 2:
                    fak=new freak("妖界之首",100);
                    cout<<"作者:你惨啦!妖界之首你也敢挑战,作者我都不敢去打,你是我的偶像啊!"<<endl;
                    cout<<"妖界之首:小子,连作者都不敢来找我,你居然敢闯进来找我,看来不简单啊。"<<endl;
                    cout<<"妖界之首:就算你怎么个不简单,我还是让你死得很好看的,哈哈哈……"<<endl;
                    cout<<car->getname()<<":费话少说,看招!"<<endl;
                    fak->showcasting();
                    break;
                case 3:
                    drugshop(car);
                    break;
                case 4:
                    weaponshop(car);
                    break;
                case 5:
                    defendshop(car);
                    break;
                case 6:
                    car->showpack(true,false);break;
                case 7:
                    car->showcasting();
                    break;
                case 0:
                    decide=false;
                    break;
                default:
                    cout<<"没有这个选项,请重新选择!"<<endl;
                }
                if(select==1||select==2)
                {
                    while(decide)
                    {
                        if(select==1)
                            attackmenu(true);
                        else
                            attackmenu(false);
                        cout<<"请选择菜单编号:";
                        int menuno;
                        cin>>menuno;
                        if(!cin)
                        {cin.clear();
                            cin.ignore(1024,'\n');
                            continue;
                        }
                        switch(menuno)
                        {
                        case 1:
                            if(car->setcasting(car->attack(fak)))
                            {
                                fak->~freak();
                                decide=false;
                                continue;
                            }
                            break;
                        case 2:
                            skillmenu(car);
                            cin>>menuno;
                            if(!cin) {
                                cin.clear();
                                cin.ignore(1024,'\n');
                                continue;
                            }
                            if(!car->useskill(menuno))
                                continue;
                            break;
                        case 3:
                            if(!car->showpack(false,false))
                                continue;
                            break;
                        case 0:
                            if(select==1)
                            {
                                if(rand()%100<50)
                                {fak->~freak();
                                    decide=false;
                                    cout<<"哈哈……逃跑成功啦!"<<endl;
                                    continue;
                                }
                                cout<<"糟糕!!!还是没逃出他的魔掌!"<<endl;
                            }
                            else
                            {
                                cout<<"没有这个选项,请重新选择!"<<endl;
                                continue;
                            }
                            break;
                        default:
                            cout<<"没有这个选项,请重新选择!"<<endl;
                            continue;}
                        if(fak->attack(car)>0)
                        {
                            car->~character();
                            fak->~freak();
                            car=NULL;
                            decide=false;
                            setting=false;
                            cout<<"胜败乃是兵家常事,大侠重新来过吧!"<<endl;
                        }
                    }
                    if(car!=NULL)
                       decide=true;
                }
            }
            if(car!=NULL)
               decide=true;
        }
    }
}/////////////////////////////////affair.cpp///////////////////////////////

#include "affair.h"
#include "casting.h"
using namespace std;
void mainmenu(bool bl)
{
    cout<<"[1] 开始新的游戏"<<endl;
    cout<<"[2]   保存进度"<<endl;
    cout<<"[3]   读取存档"<<endl;
    if(bl==true)
        cout<<"[4]   继续游戏"<<endl;
    cout<<"[0]   退出游戏"<<endl;
}
void secondmenu()
{
    cout<<"[1] 去打小怪             [2] 去打BOSS"<<endl;
    cout<<"[3] 去药品店             [4] 去武器店"<<endl;
    cout<<"[5] 去防器店             [6] 打开背包"<<endl;
    cout<<"[7] 查看状态             [0] 返回主菜单"<<endl;
}
void attackmenu(bool bl)
{
    cout<<"[1]普通攻击   [2]使用技能          [3]打开背包";
    if(bl==true)
        cout<<"         [0]逃跑";
    cout<<endl;
}
void drugshop(character *act)
{
    int select,count;
    cout<<"药店老板:欢迎光临!这里有上等的药材,要点什么吗?"<<endl;
    while(true)
    {
        cout<<"[0] 止血草    可恢复100点血量      单价100"<<endl;
        cout<<"[1] 行军丹    可恢复200点血量      单价200"<<endl;
        cout<<"[2] 护心丹    可恢复500点血量      单价500"<<endl;
        cout<<"[3] 龙胆草    可恢复1000点血量    单价1000"<<endl;
        cout<<"[4] 鼠儿果    可恢复80点魔力     单价150"<<endl;
        cout<<"[5] 还神丹    可恢复160点魔力     单价300"<<endl;
        cout<<"[6] 玉真散    可恢复400点魔力    单价750"<<endl;
        cout<<"[7] 蜂王蜜    可恢复800点魔力    单价1500"<<endl;
        cout<<"依次输入物品的编号和数量来买物品,输入两次8则进行卖物品,输入两次非数字退出商店。"<<endl;
        cout<<"当前金钱:"<<act->getmoney()<<endl; cout<<"请输入物品的编号:";
        cin>>select;
        cout<<"请输入要购买的数量:";
        cin>>count;
        if(cin)
        {
            if(select==8&&count==8)
            {
                act->showpack(true,true);
            }
            else if(select<8)
                act->shop(select,count);
            else
                cout<<"没有这种物品!"<<endl;
        }
        else
        {
            cin.clear();
            cin.ignore(1024,'\n');
            cin.ignore(1024,'\n');
            return;
        }
    }
}
void weaponshop(character *act)
{
    int select,count;
    cout<<"武器店老板:欢迎光临!这里一定会有合你的武器,随便挑吧。"<<endl;
    while(true)
    {
        cout<<"[0] 青铜剑    攻击力加20     单价3200"<<endl;
        cout<<"[1] 流光刃      攻击力加40     单价6400"<<endl;
        cout<<"[2] 凌风剑    攻击力加100    单价16000"<<endl;
        cout<<"[3] 镇妖剑    攻击力加200   单价32000"<<endl;
        cout<<"依次输入物品的编号和数量来买物品,输入两次8则进行卖物品,输入两次非数字退出商店。"<<endl;
        cout<<"当前金钱:"<<act->getmoney()<<endl;
        cout<<"请输入物品的编号:";
        cin>>select;
        cout<<"请输入要购买的数量:";
        cin>>count;
        if(cin) {
            if(select==8&&count==8)
            {
                act->showpack(true,true);
            }
            else if(select<4)
                act->shop(select+8,count);
            else
                cout<<"没有这种物品!"<<endl;
        }
        else
        {
            cin.clear();
            cin.ignore(1024,'\n');
            cin.ignore(1024,'\n');
            return;
        }
    }
}
void defendshop(character *act)
{
    int select,count;
    cout<<"防器店老板:欢迎光临!这里一定会有合你穿的防器,随便挑吧。"<<endl;
    while(true)
    {
        cout<<"[0] 黑貂裘      防御加10   单价3500"<<endl;
        cout<<"[1] 云纹细鳞甲      防御加20   单价7000"<<endl;
        cout<<"[2] 翼云甲      防御加50     单价17500"<<endl;
        cout<<"[3] 太极软甲     防御加100   单价35000"<<endl;
        cout<<"依次输入物品的编号和数量来买物品,输入两次8则进行卖物品,输入两次非数字退出商店。"<<endl;
        cout<<"当前金钱:"<<act->getmoney()<<endl;
        cout<<"请输入物品的编号:";
        cin>>select;
        cout<<"请输入要购买的数量:";
        cin>>count;
        if(cin)
        {
            if(select==8&&count==8)
            {
                act->showpack(true,true); }
            else if(select<4)
                act->shop(select+12,count);
            else
                cout<<"没有这种物品!"<<endl;
        }
        else
        {
            cin.clear();
            cin.ignore(1024,'\n');
            cin.ignore(1024,'\n');
            return;
        }
    }
}

void skillmenu(character *act)
{
    cout<<"[0]狂煞   当前等级:"<<act->getskillrank(0)<<"   使用增加攻击力:"<<act->getskillproperty(0)
            <<"   消耗魔力:"<<act->getskillspend(0)<<"    持续回合:"<<act->getskillcount(0)<<endl;
    cout<<"[1]天罡战气    当前等级:"<<act->getskillrank(1)<<"    使用增加防御:"<<act->getskillproperty(1)
            <<"    消耗魔力:"<<act->getskillspend(1)<<"    持续回合:"<<act->getskillcount(1)<<endl;
    cout<<"请输入技能编号使用技能,输入任意字母返回上一级。"<<endl;
}
///////////////////////////////////casting.cpp////////////////////////////////
#include "casting.h"
using namespace std;
character::character(string na)//构造角色
{
    name=na;
    rank=1;
    hp_max=100;
    hp_min=hp_max;
    mp_max=80;
    mp_min=mp_max;
    exp_min=0;
    exp_max=100;
    for(int i=0;i<2;i++)
    {
        skillrank[i]=1;
        skillexp[i]=0;
        skillproperty[i]=20-i*10;
        skillcount[i]=3;
        skillstate[i]=0;
        skillspend[i]=20+i*10;
    }
    hp_name[0]="止血草";
    hp_name[1]="行军丹";
    hp_name[2]="护心丹";
    hp_name[3]="龙胆草";
    mp_name[0]="鼠儿果";
    mp_name[1]="还神丹";
    mp_name[2]="玉真散";
    mp_name[3]="蜂王蜜";
    weaponname[0]="青铜剑";
    weaponname[1]="流光刃";
    weaponname[2]="凌风剑";
    weaponname[3]="镇妖剑";
    defendname[0]="黑貂裘";
    defendname[1]="云纹细鳞甲";
    defendname[2]="翼云甲";
    defendname[3]="太极软甲";
    for(int i=0;i<4;i++)
        {
        hp_drug[i]=0;
        mp_drug[i]=0;
        weaponcount[i]=0;
        defendcount[i]=0;
        weapon[i]=20+i*i*20;
        defend[i]=10+i*i*10;
        }
    weaponstate=0;
    defendstate=0;
    attack_min=10;
    attack_max=attack_min+weapon[weaponstate];
    defend_min=5;
    defend_max=defend_min+defend[defendstate];
    money=0;
}
int character::attack(character *chr)    //攻击函数
{
    srand(static_cast<unsigned>(time(NULL)));   //取得当前时间作为随机种子
    int temp,temdef; //定义临时伤害变量
    temdef=chr->defend_max;
    if(chr->skillstate[1]>0)
    {
        temdef+=chr->skillproperty[1];
        chr->skillstate[1]--;
    } temp=(attack_max+rand()%10)/(temdef/10+1); //求出伤害值
    if(skillstate[0]>0)
    {
        temp+=skillproperty[0];
        skillstate[0]--;
    }
    if(rand()%100<10)   //百分之十的几率出现致命一击
    {
        temp*=2;
        cout<<"致命一击!"<<endl;
    }
    chr->hp_min-=temp;
    if(chr->hp_min<0)
        chr->hp_min=0;
    cout<<name<<"攻击"<<chr->name<<"伤害:"<<temp<<"点。"<<endl;
    cout<<chr->name<<"还剩HP:"<<chr->hp_min<<"点。"<<endl;
    if(chr->hp_min==0)
        {
        cout<<chr->name<<"已倒下!"<<endl;
        for(int i=0;i<2;i++)
        {
            skillstate[i]=0;
            chr->skillstate[i]=0;
        }
        return chr->hp_max;   //如果对方死亡则返回对方最大生命值用以计算经验值否则返回零
        }
    else
        return 0;
}
bool character::setcasting(int m_exp)   //计算经验提升属性
{
    srand(static_cast<unsigned>(time(NULL)));
    if(m_exp!=0)
    {
        if(rand()%10==0) //百分之十的机率获得红药
        {
            hp_drug[rank/20%4]++;
            cout<<"获得一包"<<hp_name[rank/20%4]<<"。"<<endl;
        }
        if(rand()%10==1)    //百分之十的机率获得蓝药
        {
            mp_drug[rank/20%4]++;
            cout<<"获得一包"<<mp_name[rank/20%4]<<"。"<<endl;
        }
        exp_min+=m_exp*1.25;
        money+=m_exp*2.8;
        cout<<name<<"获得"<<m_exp*1.25<<"点经验和"<<m_exp*2.8<<"两银。"<<endl;if(exp_min>=exp_max) //经验满则提升属性
        {
            exp_min-=exp_max;
            rank++;
            exp_max+=rank*exp_max;
            attack_min=rank*attack_min;
            attack_max=attack_min+weapon[weaponstate];
            defend_min=rank*defend_min;
            defend_max=defend_min+defend[defendstate];
            hp_max+=rank*100*0.65;
            hp_min=hp_max;
            mp_max+=rank*80*0.55;
            mp_min=mp_max;
            cout<<"恭喜! 你升级了!"<<endl;
        }
        return true;
    }
    return false;
}
void character::shop(int m_run,int m_count) //购买物品
{
    if(m_run>=0&&m_run<=3)
        if(money>=((m_run%4)*(m_run%4))*100+m_count*100)
        {
           hp_drug[m_run%4]+=m_count;
           money-=((m_run%4)*(m_run%4))*100+m_count*100;
        }
       else
       {
           cout<<"对不起!你的金钱不足!"<<endl;
           return;
       }
    else if(m_run>=4&&m_run<=7)
        if(money>=((m_run%4)*(m_run%4))*150+m_count*150)
        {
           mp_drug[m_run%4]+=m_count;
           money-=((m_run%4)*(m_run%4))*150+m_count*150;
        }
       else
       {
           cout<<"对不起!你的金钱不足!"<<endl;
           return;
       } else if(m_run>=8&&m_run<=11)
        if(money>=((m_run%4)*(m_run%4))*3200+m_count*3200)
        {
           weaponcount[m_run%4]+=m_count;
           money-=((m_run%4)*(m_run%4))*3200+m_count*3200;
        }
       else
       {
           cout<<"对不起!你的金钱不足!"<<endl;
           return;
       }
    else if(m_run>=12&&m_run<=15)
        if(money>=((m_run%4)*(m_run%4))*3500+m_count*3500)
        {
           defendcount[m_run%4]+=m_count;
           money-=((m_run%4)*(m_run%4))*3500+m_count*3500;
        }
       else
       {
           cout<<"对不起!你的金钱不足!"<<endl;
           return;
       }
    else
    {
        cout<<"没有这种物品!"<<endl;
        return;
    }
    if(m_count>0)
        cout<<"交易成功!"<<endl;
}
void character::sell(int m_run, int m_count) //出售物品
{
    if(m_run>=0&&m_run<=3)
    {
        if(hp_drug[m_run%4]>=m_count)
        {
            hp_drug[m_run%4]-=m_count;
            money+=(m_run%4+1)*m_count*80;
        }
        else
        {
            cout<<"物品数量不足,交易失败!"<<endl;
            return;
        }
    }
    else if(m_run>=4&&m_run<=7)
    {
        if(mp_drug[m_run%4]>=m_count)
        {
            mp_drug[m_run%4]-=m_count;money+=(m_run%4+1)*m_count*120;
        }
        else
        {
            cout<<"物品数量不足,交易失败!"<<endl;
            return;
        }
    }
    else if(m_run>=8&&m_run<=11)
    {
        if(weaponcount[m_run%4]>=m_count)
        {
            weaponcount[m_run%4]-=m_count;
            money+=(m_run%4+1)*m_count*2300;
        }
        else
        {
            cout<<"物品数量不足,交易失败!"<<endl;
            return;
        }
    }
    else if(m_run>=12&&m_run<=15)
    {
        if(defendcount[m_run%4]>=m_count)
        {
            defendcount[m_run%4]-=m_count;
            money+=(m_run%4+1)*m_count*2500;
        }
        else
        {
            cout<<"物品数量不足,交易失败!"<<endl;
            return;
        }
    }
    else
    {
        cout<<"没有这种物品!"<<endl;
        return;
    }
    cout<<"交易成功!"<<endl;
}
bool character::showpack(bool bl, bool se) //打开背包,参数用于判断是否在打怪的过程中
{
    int select,count;
    do
    {
        cout<<"----------------------------------------------"<<endl;
        for(int i=0;i<8;i++)
        {
            if(i<4)
            {if(hp_drug[i]>0)
                    cout<<"["<<i<<"] "<<hp_name[i]<<"           "<<hp_drug[i]<<"包"<<endl;
            }
            else if(i>3&&i<8)
                if(mp_drug[i%4]>0)
                    cout<<"["<<i<<"] "<<mp_name[i%4]<<"           "<<mp_drug[i%4]<<"包"<<endl;
        }
        if(bl==true)
        {
            for(int i=8;i<16;i++)
            {
                if(i<12)
                {
                    if(weaponcount[i%4]>0)
                        cout<<"["<<i<<"] "<<weaponname[i%4]<<"           "<<weaponcount[i%4]<<"把"<<endl;
                }
                else if(i>11&&i<16)
                    if(defendcount[i%4]>0)
                        cout<<"["<<i<<"] "<<defendname[i%4]<<"           "<<defendcount[i%4]<<"件"<<endl;
             }
        }
        cout<<"----------------------------------------------"<<endl;
        cout<<"金钱:"<<money<<endl;
        cout<<"----------------------------------------------"<<endl;if(se==false)
        {
            cout<<"输入编号使用物品,输入任意字母关闭背包。"<<endl;
            cin>>select;
            if(cin)
            {
                if(usearticle(select,bl))
                {
                    cout<<"物品使用成功!"<<endl;
                    return true;
                }
            }
        }
        else
        {
            cout<<"请依次输入物品编号和数量来进行出售物品,输入任意字母返回上一级。"<<endl;
            cin>>select>>count;
            if(cin)
            {
                sell(select,count);
                return true;
            }
        }
    }
    while(cin);
    cin.clear();
    cin.ignore(1024,'\n');
    return false;
}

bool character::usearticle(int m_no, bool bl) //使用物品函数最后参数用于判断是否在打怪的过程中
{
    if(m_no<4)
    {
        if(hp_drug[m_no]>0)
        {
            hp_drug[m_no]--;
            if(hp_min+((m_no*m_no*100)+100)<hp_max)
                hp_min+=((m_no*m_no*100)+100);
            else
                hp_min=hp_max;
        }else
        {
            cout<<"没有这个物品!"<<endl;
            return false;
        }
    }
    else if(m_no>3&&m_no<8)
    {
        if(mp_drug[m_no%4]>0)
        {
            mp_drug[m_no%4]--;
            if(mp_min+(((m_no%4)*(m_no%4)*80)+80)<mp_max)
                mp_min+=(((m_no%4)*(m_no%4)*80)+80);
            else
                mp_min=mp_max;
        }
        else
        {
            cout<<"没有这个物品!"<<endl;
            return false;
        }
    }
    else if(m_no>7&&m_no<12&&bl==true)
    {
        if(weaponcount[m_no%4]>0)
        {
            weaponcount[weaponstate]++;
            weaponcount[m_no%4]--;
            attack_max=attack_min+weapon[m_no%4];
            weaponstate=m_no%4;
        }
        else
        {
            cout<<"没有这个物品!"<<endl;
            return false;
        }
    }
    else if(m_no>11&&m_no<16&&bl==true)
    {
        if(defendcount[m_no%4]>0)
        {
            defendcount[defendstate]++;
            defendcount[m_no%4]--;
            defend_max=defend_min+defend[m_no%4];defendstate=m_no%4;
        }
        else
        {
            cout<<"没有这个物品!"<<endl;
            return false;
        }
    }
    else
    {
        cout<<"没有这个物品!"<<endl;
        return false;
    }
    return true;
}

void character::showcasting() //显示人物状态
{
    cout<<"-----------------------------------------------------------------------"<<endl;
    cout<<"名称:"<<setw(10)<<name<<"             "<<"等级:"<<setw(5)<<rank<<endl;
    cout<<"-----------------------------------------------------------------------"<<endl;
    cout<<"血量:"<<setw(6)<<hp_min<<"/"<<setw(6)<<hp_max<<"          "
            <<"魔力:"<<setw(6)<<mp_min<<"/"<<setw(6)<<mp_max<<endl;
    cout<<"-----------------------------------------------------------------------"<<endl;
    cout<<"攻击:"<<setw(6)<<attack_max<<"                 "
            <<"防御:"<<setw(6)<<defend_max<<endl;
    cout<<"-----------------------------------------------------------------------"<<endl;
    cout<<"武器:"<<setw(10)<<weaponname[weaponstate]<<"                "
            <<"防器:"<<setw(10)<<defendname[defendstate]<<endl;
    cout<<"-----------------------------------------------------------------------"<<endl;
    cout<<"经验:"<<setw(10)<<exp_min<<"/"<<setw(10)<<exp_max<<endl;
    cout<<"-----------------------------------------------------------------------"<<endl;
}

bool character::useskill(int select)
{
    if(select>=0&&select<=1)
    {
        if(skillspend[select]<=mp_min) {
            if(skillstate[select]==0)
            {
                skillstate[select]=skillcount[select];
                skillexp[select]++;
                mp_min-=skillspend[select];
                if(skillexp[select]==skillrank[select]*10&&skillrank[select]<10)
                {
                    skillexp[select]=0;
                    skillrank[select]++;
                    skillcount[select]++;
                    skillspend[select]+=20+select*10;
                    skillproperty[select]+=30-select*10;
                    cout<<"恭喜!你的技能升级了!"<<endl;
                }
                cout<<"技能使用成功!"<<endl;
                return true;
            }
            else
            {
                cout<<"你这个技能的状态还在持续!"<<endl;
                return false;
            }
        }
        else
        {
            cout<<"你的魔力值不足!"<<endl;
            return false;
        }
    }else
    {
        cout<<"没有这个技能!"<<endl;
        return false;
    }
}

void character::saveplan()
{
    fstream saveout;
    saveout.open("./save.sav",ios_base::out);
    saveout<<name<<" "<<rank<<" "<<hp_min<<" "<<hp_max<<" "<<mp_min<<" "<<mp_max<<endl;
    saveout<<attack_min<<" "<<attack_max<<" "<<defend_min<<" "<<defend_max<<endl;
    saveout<<exp_min<<" "<<exp_max<<" "<<money<<" "<<weaponstate<<" "<<defendstate<<endl;
    for(int i=0;i<2;i++)
    {
        saveout<<skillrank[i]<<" "<<skillexp[i]<<" "<<skillcount[i]<<" "<<skillstate[i]<<" "
                <<skillproperty[i]<<" "<<skillspend[i]<<endl;
    }
    for(int i=0;i<4;i++)
    {
        saveout<<hp_drug[i]<<" "<<hp_name[i]<<" "<<mp_drug[i]<<" "<<mp_name[i]<<endl;
        saveout<<weapon[i]<<" "<<weaponcount[i]<<" "<<weaponname[i]<<endl;
        saveout<<defend[i]<<" "<<defendcount[i]<<" "<<defendname[i]<<endl;
    }
    saveout.close();
    cout<<"数据保存成功!"<<endl;
}

bool character::readplan()
{
    fstream savein;
    savein.open("./save.sav",ios_base::in);
    if(!savein)
    {
        cout<<"文件不存在,或打开失败!"<<endl;
        return false;
    }
    savein>>name>>rank>>hp_min>>hp_max>>mp_min>>mp_max;
    savein>>attack_min>>attack_max>>defend_min>>defend_max;
    savein>>exp_min>>exp_max>>money>>weaponstate>>defendstate;
    for(int i=0;i<2;i++)
    {
        savein>>skillrank[i]>>skillexp[i]>>skillcount[i]>>skillstate[i]>>skillproperty[i]>>skillspend[i];
    }
    for(int i=0;i<4;i++)
    {
        savein>>hp_drug[i]>>hp_name[i]>>mp_drug[i]>>mp_name[i];
        savein>>weapon[i]>>weaponcount[i]>>weaponname[i];
        savein>>defend[i]>>defendcount[i]>>defendname[i];
    }
    savein.close();
    cout<<"数据读取成功!"<<endl;
    return true;
}

freak::freak(string na, int ra) : character(na)
{
    srand(static_cast<unsigned>(time(NULL)));
    name=na;
    rank=ra+rand()%3;
    hp_max=rank*100+rand()%50;
    hp_min=hp_max;
    attack_max=rank*10+rand()%20;
    defend_max=rank*5+rand()%10;
}

void freak::showcasting()
{
    cout<<"------------------------------------------------------"<<endl;
    cout<<"名称:"<<setw(10)<<name<<"            等级:"<<setw(6)<<rank<<endl;
    cout<<"------------------------------------------------------"<<endl;
    cout<<"血量:"<<setw(6)<<hp_min<<"/"<<setw(6)<<hp_max<<endl;
    cout<<"------------------------------------------------------"<<endl;
    cout<<"攻击:"<<setw(6)<<attack_max<<"              防御:"<<setw(6)<<defend_max<<endl;
    cout<<"------------------------------------------------------"<<endl;
}


下面是函数库代码


程序代码:
////////////////////////////////affair.h////////////////////////////////
// Components for manipulating sequences of characters -*- C++ -*-

// Copyright (C) 1997-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.>.

/** @file include/string

 *  This is a Standard C++ Library header.

 */

//
// ISO C++ 14882: 21  Strings library
//
#include "casting.h"
#ifndef AFFAIR_H
#define AFFAIR_H
void mainmenu(bool bl); //主菜单,参数用于判断是否已经存在角色
void secondmenu(); //子菜单
void attackmenu(bool bl); //打怪菜单,参数判断是否打的是boss
void drugshop(character *act); //药店函数
void weaponshop(character *act); //武器店
void defendshop(character *act);   //防器店
void skillmenu(character *act); //技能菜单
#endif // AFFAIR_H//////////////////////////main.cpp///////////////////////////////////




还有一个函数库
程序代码:
////////////////////////////////casting.h//////////////////////////////////////////
// Components for manipulating sequences of characters -*- C++ -*-

// Copyright (C) 1997-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.>.

/** @file include/string

 *  This is a Standard C++ Library header.

 */

//
// ISO C++ 14882: 21  Strings library
//
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <iomanip>
#ifndef CASTING_H
#define CASTING_H
using namespace std;
class character   //人物
{
protected:
    string name; //角色名称
    int rank; //角色等级
    int skillrank[2]; //技能等级
    int skillexp[2]; //技能经验
    int skillcount[2]; //技能持续回合数
    int skillstate[2]; //技能状态
    int skillproperty[2]; //技能属性
    int skillspend[2]; //技能消耗MP值
    int hp_min; //最小血量
    int hp_max; //最大血量
    int mp_min; //最小法力
    int mp_max; //最大法力
    int attack_min; //最小攻击力
    int attack_max; //最大攻击力
    int defend_min; //最小防御力
    int defend_max; //最大防御力
    int exp_min; //最小经验值
    int exp_max; //最大经验值
    int money; //金钱
    int hp_drug[4]; //四种红药的数量
    int mp_drug[4]; //四种蓝药的数量
    string hp_name[4]; //四种红药的名称
    string mp_name[4]; //四种蓝药的名称
    int weapon[4]; //四种武器的攻击
    int defend[4]; //四种防器的防御
    int weaponcount[4]; //记录每种武器的数量
    int defendcount[4]; //记录每种防器的数量
    string weaponname[4]; //四种武器的名称
    string defendname[4]; //四种防器的名称
    int weaponstate; //用于判断武器的状态
    int defendstate; //用于判断防器的状态
public:
    character(){} //默认构造
    character(string na); //构造角色对像需要一个名称
    ~character(){} //析构函数
    string getname(){return name;} //获得人物名称
    int getrank(){return rank;} //获得人物等级
    int getmoney(){return money;} //获得金钱
    int getskillrank(int select){return skillrank[select];}//获得技能等级
    int getskillproperty(int select){return skillproperty[select];} //获得技能属性
    int getskillspend(int select){return skillspend[select];} //获得技能消耗值
    int getskillcount(int select){return skillcount[select];} //获得技能持续回合数
    int attack(character *chr);   //攻击函数对方死亡时返回经验值
    bool useskill(int select); //使用技能函数参数为选择的技能
    bool setcasting(int m_exp); //根据经验设置人物的属性
    void shop(int m_run,int m_count); //购物函数需要第一个参数是类型,最后一个是数量
    void sell(int m_run,int m_count); //出售物品第一个是种类,最后一个是数量
    bool showpack(bool bl,bool se); //显示背包状态参数用来判断环境
    bool usearticle(int m_no,bool bl); //使用物品第一个参数为物品的编号,最后一个判断环境
    void showcasting(); //显示人物状态
    void saveplan(); //保存进度
    bool readplan(); //读取进度
};
class freak : public character   //怪物
{
    public:
    freak(){} //默认构造函数
    freak(string na,int ra); //构造怪物对像需要一个名称和一个等级作为参数
    void showcasting(); //重写显示怪物状态
    ~freak(){} //析构函数
};

#endif // CASTING_H


[此贴子已经被作者于2018-9-22 20:21编辑过]

搜索更多相关主题的帖子: int cout select cin return 
2018-09-22 20:13
拉拉面
Rank: 1
来 自:广州
等 级:新手上路
帖 子:26
专家分:5
注 册:2018-9-22
得分:0 
哈哈哈

[此贴子已经被作者于2018-9-22 20:24编辑过]


哈哈哈哈哈啦啦啦
2018-09-22 20:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-489948-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029779 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved