hqd125


私信TA

用户名:hqd

访问量:2661

签 名:

若MS WPS没有把握,何不把时间花在C上面?一举两得

等  级
排  名 347
经  验 5127
参赛次数 5
文章发表 6
年  龄 0
在职情况 教师
学  校
专  业

  自我简介:

学练同步,知行合一,希望大家都能理论与动手结合,拒绝理论派! 前期扎实学习打基础,后期快乐练习过二级!

这是我无意中看别人文章发现的,觉得这个思路还是挺好的,于是就cover过来了

原版在这里

https://blog.dotcpp.com/a/73087


然后,结合自己的思路,写了下面的代码  勉强能跑起来

还有好几个功能没有完成

1、人物、boss技能在战斗中的使用

2、人物、boss字符图像显示,因为图片转字符图像占地太多,还没搞

3、人物的技能升级、战斗中如何使用,还没想好


看后面有时间完善下吧

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#define bossmax 18       //boss 个数 
using namespace std;

typedef struct BOSS      //boss 结构
{
    double health;//生命
    double magic;//魔法
    double attack;//攻击
    double defense;//防御
    double buff;//技能增幅
    double money;//金钱


} boss;

typedef struct PERSON    //人物  结构
{
    double health;//当前生命
    double magic;//当前魔法
    double attack;//攻击
    double defense;//防御
    double buff;//技能增幅
    double money;//金钱
    double maxhealth;//满状态生命
    double maxmagic;//满状态魔法

} person;
typedef struct WEAPON  //商店武器  结构
{
    double money;//金钱
    double attack;//攻击
} weap;
typedef struct CLOTHES  //商店衣服  结构
{
    double money;//金钱
    double defense;//防御
} cloth;
typedef struct MEDICINE   //商店药水  结构
{
    double money;//金钱
    double maxhealth;//最高生命增加值
} medi;

//boss信息
boss bs[bossmax]= {{2,1,0.6,0.2,0.05,3},{3,2,1.1,0.5,0.05,5},{4,3,2,0.7,0.05,9},
    {5.9,4,3.16,1,0.05,15},{8,6,5.5,1.5,0.1,22},{14,8,7,2,0.1,30},{19,12,10,3,0.1,50},
    {32,15,20,6,0.15,72},{73,22,50,10,0.15,99},{157,35,100,22,0.15,160},{200,56,146.23,45,0.15,235},
    {403,88,254.13,70,0.2,280},{801,100,312,101,0.2,350},{1200,150,403,156,0.2,420},
    {3630,260,601,225,0.3,500},{20210,480,1023,459,0.3,1000},{200000,1000,5000,1500,0.5,10000},
    {20000000,100000,50000,1500,0.7,100000}


};

//可选 人物信息
person hero,per[3]= {{10,2,1,0.2,0.1,10,10,2},{10,2,1,0.2,0.1,10,10,2},{10,2,1,0.2,0.1,10,10,2}};
char name[100];

weap weapon[4]= {{10,1},{30,4},{100,15},{500,90}};  //商店  武器信息
cloth clothes[4] = {{10,1},{30,4},{100,15},{500,90}};//商店  衣服信息
medi medicine[4] = {{10,3}, {30,13}, {100,45}, {500,270}};//商店  药水信息
//  开始 结束情节
char qingjie[1000] ="从前\n\n明兰国有一位帅气的王子和一位美丽的公主\n\n他们都深爱着对方\n\n就在国王为他们准备大婚的前一天\n\n竟然从深山中跑出一只巨大的怪兽\n\n将美丽的公主抢走了\n\n而深爱着公主的王子决定踏上营救公主的艰难之路…………\n\n";
char qingjieover[1000] = "经过艰苦卓绝的战斗\n\n王子终于从BOSS手中夺回了心爱的公主\n\n回到皇宫后\n\n国王为他们举行了盛大的婚礼\n\n王子和公主终于过上了幸福的生活…………\n\n\n\n\n       OVER";

void killboss(int );
void zhujiemian();

void print(char a[])  //输出字符函数
{
    for(int i=0; a[i]!=0; i++)
    {
        cout<<a[i];
        Sleep(45);
    }
    getch();
    system("cls");
}

void initialization()   //初始化 人物信息
{
    int skill;
    char s[1000];
    cout<<"请为王子确定一个名字:"<<endl;
    cin>>name;
    cout<<"1  血量高    2  攻击高    3  防御高\n";
    cout<<"请选择"<<name<<"擅长的方向:"<<endl;
    cin>>skill;
    hero = per[skill-1];
    strcpy(s,name);
    strcat(s,",赶快踏上营救公主的征途吧…………");
    print(s);
}

void herostate()   //人物状态信息  函数
{
    cout<<"当前血量:"<<hero.health <<"\t当前魔法:"<<hero.magic <<"\t当前攻击:"<<hero.attack <<"\t当前防御:"<<hero.defense  <<"\n最高血量:"<<hero.maxhealth <<"\t最高魔法:"<<hero.maxmagic <<"\t当前金币:"<<hero.money <<endl<<endl<<endl;
}
int buybingqi(int i)   //买武器   成功返回1 失败返回0
{
    if(hero.money >=weapon[i].money )
    {
        hero.attack +=weapon[i].attack ;
        hero.money -=weapon[i].money ;
        return 1;
    }
    else return 0;
}
int buyyifu(int i)   //买衣服    成功返回1 失败返回0
{
    if(hero.money >=clothes[i].money )
    {
        hero.defense  +=clothes[i].defense  ;
        hero.money -=clothes[i].money ;
        return 1;
    }
    else return 0;

}
int buyyaoshui(int i)  //买药水    成功返回1 失败返回0
{
    if(hero.money >=medicine[i].money )
    {
        hero.maxhealth  +=medicine[i].maxhealth  ;
        hero.money -= medicine[i].money ;
        return 1;
    }
    else return 0;
}

int buy(char flag , int i)   //商店  购买东西分类
{
    switch(flag)           //b为武器   y为衣服   h为药水
    {
        case 'b':
            return buybingqi(i-1);
        case 'y':
            return buyyifu(i-1);
        case 'h':
            return buyyaoshui(i-1);
    }
}

void bingqi()   //商店  买兵器界面
{
    while(1)
    {
        herostate();
        printf("请选择需要购买的兵器:\n\n1.菜刀\n2.匕首\n3.大剑\n4.圣剑\n任意键返回\n");
        int k=getch(),f;
        k-=48;
        char flag = 'b';
        system("cls");
        if(k>=1&&k<=4)
        {
            f = buy(flag,k);
            herostate();
            if(f)  cout<<"购买成功!"<<endl;
            else  cout<<"金币不足!"<<endl;
        }
        else
        {
            system("cls");
            return;
        }
        getch();
        system("cls");
    }
}
void yifu()  //商店  买衣服界面
{
    while(1)
    {
        herostate();
        printf("请选择需要购买的衣服:\n\n1.帽子\n2.腿甲\n3.护手\n4.铠甲\n任意键返回\n");
        int k=getch(),f;
        k-=48;
        char flag = 'y';
        system("cls");
        if(k>=1&&k<=4)
        {
            f = buy(flag,k);
            herostate();
            if(f)  cout<<"购买成功!"<<endl;
            else  cout<<"金币不足!"<<endl;
        }
        else
        {
            system("cls");
            return;
        }
        getch();
        system("cls");
    }
}

void yaoshui() //商店  买药水界面
{
    while(1)
    {
        herostate();
        printf("请选择需要购买的药水:\n\n1.小瓶药水\n2.中瓶药水\n3.大瓶药水\n4.小瓶药坛\n任意键返回\n");
        int k=getch(),f;
        k-=48;
        char flag = 'h';
        system("cls");
        if(k>=1&&k<=4)
        {
            f = buy(flag,k);
            herostate();
            if(f)  cout<<"购买成功!"<<endl;
            else  cout<<"金币不足!"<<endl;
        }
        else
        {
            system("cls");
            return;
        }
        getch();
        system("cls");
    }
}

void huifu()   //商店  状态恢复  根据生命值不同 花费不同
{
    double hf[6][2]= {20,2,100,5,500,12,1000,25,5000,100,1e30,200};
    if(hero.health <hero.maxhealth ||hero.magic <hero.maxmagic )
    {
        for(int i=0; i>=0; i--)
            if( hero.maxhealth <=hf[i][0])
            {
                if(hero.money <hf[i][1])
                    cout<<"金币不足!"<<endl;
                else
                {
                    hero.health =hero.maxhealth ;
                    hero.magic  = hero.maxmagic ;
                    hero.money -=hf[i][1];
                    cout<<"状态恢复成功!"<<endl;

                }
                break;
            }
    }
    else
        cout<<"状态已满,无需恢复!"<<endl;
    Sleep(400);
    return;

}

void shop() //商店  主界面
{
    while(1)
    {
        system("cls");
        herostate();
        printf("欢迎来到野外商店\n\n1.购买兵器\n2.购买衣服\n3.购买药水\n4.恢复状态\n任意键返回\n");

        int k=getch();
        k-=48;
        switch(k)
        {
            case 1:
                system("cls");
                bingqi();
                break;
            case 2:
                system("cls");
                yifu();
                break;
            case 3:
                system("cls");
                yaoshui();
                break;
            case 4:
                system("cls");
                huifu();
                break;
            default:
                return;
        }
    }
}
void choiceboss()  //战斗  选择boss主界面
{
    while(1)
    {
        system("cls");
        herostate();
        printf("欢迎闯关营救公主\n\n\n1.挑战1~17等级BOSS(按1~17数字)\n2.挑战终极BOSS(按18)\n3.随机挑战BOSS(按0,金币加成5%%)\n4.按其他键返回\n");

        int k=getch();
        k-=48;
        if(k==1)    // 因为是自动跳转 所以 输入十几的时候加了一个1秒内输入第二个数字的判断
        {
            //  如果没有第二个数字  则还是第一个BOSS
            int a=100,m=0;
            while(a--)
            {
                if(kbhit())
                {
                    m=getch();
                    if(m>=48&&m<=57)
                        k=10+m-48-1;
                    break;
                }
                Sleep(10);
            }
        }
        else if( k==0)   //随机BOSS
        {
            srand(time(NULL));
            k=rand()%bossmax;
        }
        if(k>=0&&k<bossmax)
            killboss(k);
        else zhujiemian();
    }
}
void fuhuo()   //商店  复活界面
{
    if(hero.money >200)
    {
        hero.health  = hero.maxhealth ;
        hero.magic = hero.maxmagic ;
        hero.money -=200;
    }
    else cout<<"金币不足!"<<endl;
}

void killboss(int i)   //战斗  正式战斗界面
{
    system("cls");
    boss b= bs[i];
    while(hero.health >0&& b.health >0)  //战斗中
    {
        system("cls");
        herostate();

        double atk = (hero.attack -b.defense );
        b.health -=atk>0? atk:0;
        cout<<name<<"王子对等级"<<i+1<<"的BOSS造成了"<<(atk>0? atk:0)<<"点伤害!"<<endl;
        if(b.health >0)
        {
            atk = (b.attack-hero.defense );
            hero.health -=atk>0? atk:0;
            cout<<"等级"<<i+1<<"的BOSS对"<<name<<"王子造成了"<< (atk>0? atk:0)<<"点伤害!"<<endl;
        }
        Sleep(50);
    }
    if(hero.health <=0)   //挑战失败
    {
        cout<<name<<"王子挑战失败!\n是否复活??(y/n)\n\n";
        int f = getch();
        if(f=='y'||f=='Y') fuhuo();
        else return ;

    }
    else if(b.health <=0)   //挑战成功
    {
        cout<<"恭喜获得"<<b.money<<"金币!\n离拯救出公主又近了一步!\n加油吧!!\n\n"<<endl;
        hero.money += b.money ;

        if(i+1==bossmax)
        {
            system("cls");
            print(qingjieover);
            getch();
            exit(1);
        }

    }
    getch();

}

void zhujiemian()   //主界面  包含 商店  战斗 2个选项
{
    while(1)
    {
        system("cls");
        herostate();
        printf("1.商店 2.战斗 \n");
        char k=getch();
        if(k>'2'||k<'1')
            system("cls");
        if(k=='1')
        {
            system("cls");
            shop();
        }
        if(k=='2')
        {
            system("cls");
            choiceboss();
        }

    }
}

int main()    //主函数
{
    system("title  王子拯救公主");    //游戏框标题
    print("王子拯救公主小游戏\n\n          hqd\n\n         ---- 设计 covered from 天尊沐笙");
    print(qingjie);  //故事情节
    initialization(); //初始化
    zhujiemian();  //主界面
    return 0;
}


 

0.0分

4 人评分

  评论区

有截图就好了
2021-03-16 19:02:58
  • «
  • 1
  • »