hqd125


私信TA

用户名:hqd

访问量:2664

签 名:

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

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

  自我简介:

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

王子拯救公主小游戏


        由之前看到别人写的一个游戏启发,然后自己就写了这个小游戏,之间的老版本在此--->  https://blog.dotcpp.com/a/77468


         时隔好长时间,自己又将其完善了一下。


此小游戏现版本有以下几个功能:

1、有情节(开始、结束)

2、hero有名字、有擅长方向(类似玩游戏中选职业),每个方向有对应加成

3、有商店,能进行武器装备、血、蓝的购买

4、hero有技能,并能对技能进行升级,在战斗中使用技能能提升hero的血、蓝、攻击、防御

5、boss有技能,在战斗中随机使用

6、战斗能进行自动、手动控制,并能够设置战斗时进程快慢

7、战斗前能够自由/随机选择战斗BOSS,随机战斗胜利后有金币加成

8、战斗中能显示hero和boss的状态


话不多说,直接放上几张截图


截图1624036898.png

图1  游戏名+声明


截图1624036924.png

图2  故事情节(开始)

图3  起名字+选职业


图4  游戏主界面

图5  商店主界面

图6  购买物品界面

图7  设置自动/手动攻击+战斗过程时长

图8  手动模式下战斗界面

图9  手动模式下战斗界面

图10  手动模式下使用技能界面

截图1624036666.png

图11  自动模式下战斗界面

图12  结束情节


是不是蠢蠢欲动,跃跃欲试了?

        马上贴上代码,复制运行吧。(除了玩以外,还可以看看代码哦,有注释的)

#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;//防御
int skilllevel;//技能等级
double buff;//技能增幅
double money;//金钱
double maxhealth;//满状态生命
double maxmagic;//满状态魔法
double goodatsth;//擅长方向加成
} 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;

typedef struct SKILLLEVEL  //人物技能  结构
{
double money;    //每级花费金钱
double level;    //等级
double magic;    //使用花费魔法
}sklevel;

//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.4,1000},{200000,1000,5000,1500,0.5,10000},
{20000000,100000,50000,1500,0.7,100000}
};

//可选人物信息
person hero,per[3]= {{10,2,1,0.2,1,0.1,10,10,2,0.1},{10,2,1,0.2,1,0.1,10,10,2,0.05},{10,2,1,0.2,1,0.1,10,10,2,0.05}};
char name[100];//人物姓名
int skill;//人物擅长方向

//技能信息--升级所需金钱、技能等级、使用所需魔法信息
sklevel skilllevel[10]={0,1,5,500,2,20,2000,3,50,5000,4,200,14000,5,500,25000,6,1000,60000,7,3000,150000,8,5000,400000,9,8000,1000000,10,12000};

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}};//商店  药水信息
medi mofashui[4] = {{10,2}, {30,8}, {100,30}, {500,170}};//商店  魔法水信息
//开始 结束情节
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,float,char ,int);

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

void initialization()//初始化人物信息
{
char s[1000];
cout<<"请为王子确定一个名字:"<<endl;
cin>>name;
cout<<"1  血量高    2  攻击高    3  防御高\n";
cout<<"请选择"<<name<<"擅长的方向:"<<endl;
while(1)
{
cin>>skill;
if(skill>=1&&skill<=3)
break;
else cout<<"输入错误,请重新输入!"<<endl;
}
hero = per[skill-1];
switch(skill)   //初始化 人物擅长方向信息
{
case 1:
hero.maxhealth *=(1+hero.goodatsth) ;
hero.health =hero.maxhealth ;
break;
case 2:
hero.attack *=(1+hero.goodatsth );
break;
case 3:
hero.defense *=(1+hero.goodatsth);
break;
}
strcpy(s,name);
strcat(s,",赶快踏上营救公主的征途吧…………");
print(s);
}

void herostate()   //人物状态信息
{
cout<<name<<"当前状态:\n"<<"当前血量:"<<hero.health<<"\t当前魔法:"<<hero.magic<<"\t当前攻击:"<<hero.attack<<"\t当前防御:"<<hero.defense<<"\n最高血量:"<<hero.maxhealth<<"\t最高魔法:"<<hero.maxmagic<<"\t技能等级:"<<hero.skilllevel <<"\t当前金币:"<<hero.money<<endl<<endl;
}
void bossstate(boss b)
{
cout<< "BOSS状态:\n"<<"当前血量:"<<b.health <<"\t当前魔法:"<<b.magic <<"\t当前攻击:"<<b.attack<<"\t当前防御:"<<b.defense <<"\n\n";
}
int buybingqi(int i)   //买武器
{
if(hero.money >=weapon[i].money )
{
if(skill==2) hero.attack +=weapon[i].attack*(1+hero.goodatsth ) ;//攻击擅长时 购买武器有加成
else hero.attack +=weapon[i].attack ;
hero.money -=weapon[i].money ;
return 1;
}
else return 0;
}
int buyyifu(int i)   //买防御
{
if(hero.money >=clothes[i].money )
{
if(skill ==3)  hero.defense  +=clothes[i].defense *(1+hero.goodatsth ) ;//防御擅长时 购买衣服有加成
else hero.defense  +=clothes[i].defense  ;
hero.money -=clothes[i].money ;
return 1;
}
else return 0;
}
int buyyaoshui(int i)  //买血
{
if(hero.money >=medicine[i].money )
{
if(skill==1) hero.maxhealth +=medicine[i].maxhealth *(1+hero.goodatsth ) ;//血量擅长时 购买药水有加成
else hero.maxhealth +=medicine[i].maxhealth  ;
hero.money -= medicine[i].money ;
return 1;
}
else return 0;
}

int buymofashui(int i)  //买蓝
{
if(hero.money >=mofashui[i].money )
{
hero.maxmagic +=mofashui[i].maxhealth  ;
hero.money -=mofashui[i].money ;
return 1;
}
else return 0;
}

void buyjineng()  //买技能
{
if(hero.skilllevel ==10)
cout<<"技能达到顶级10级,购买失败!\n\n";
else if(hero.money >=skilllevel[hero.skilllevel].money   )
{
hero.skilllevel ++;
hero.buff+=0.05;
hero.money -= skilllevel[hero.skilllevel-1 ].money ;
cout<<"购买成功,技能提升为"<<hero.skilllevel <<"级!\n\n";
}
else
cout<<"金币不足,购买失败!\n\n";
Sleep(1000);
return ;
}
 
 
int buy(char flag , int i) //商店 购买东西分类
{
switch(flag)  //b为武器   y为衣服   h为药水  m为魔法
{
case 'b':
return buybingqi(i-1);
case 'y':
return buyyifu(i-1);
case 'h':
return buyyaoshui(i-1);
case 'm':
return buymofashui(i-1);
}
}

void buysth(char flag)//买兵器 衣服 血 蓝 界面
{
while(1)
{
herostate();
switch(flag)
{
case 'b':
printf("请选择需要购买的兵器:(增加攻击力)\n\n1.菜刀(攻击:%.0lf  金币:%.0lf)\n2.匕首(攻击:%.0lf  金币:%.0lf)\n3.大剑(攻击:%.0lf  金币:%.0lf)\n4.圣剑(攻击:%.0lf  金币:%.0lf)\n任意键返回\n",weapon[0].attack ,weapon[0].money ,weapon[1].attack ,weapon[1].money ,weapon[2].attack ,weapon[2].money ,weapon[3].attack ,weapon[3].money );
break;
case 'y':
printf("请选择需要购买的衣服:(增加防御力)\n\n1.帽子(防御:%.0lf  金币:%.0lf)\n2.腿甲(防御:%.0lf  金币:%.0lf)\n3.护手(防御:%.0lf  金币:%.0lf)\n4.铠甲(防御:%.0lf  金币:%.0lf)\n任意键返回\n",clothes[0].defense ,clothes[0].money ,clothes[1].defense ,clothes[1].money,clothes[2].defense ,clothes[2].money,clothes[3].defense ,clothes[3].money);
break;
case 'h':
printf("请选择需要购买的药水:(增加生命上限)\n\n1.小瓶药水(生命:%.0lf  金币:%.0lf)\n2.中瓶药水(生命:%.0lf  金币:%.0lf)\n3.大瓶药水(生命:%.0lf  金币:%.0lf)\n4.小瓶药坛(生命:%.0lf  金币:%.0lf)\n任意键返回\n",medicine[0].maxhealth ,medicine[0].money,medicine[1].maxhealth ,medicine[1].money,medicine[2].maxhealth ,medicine[2].money,medicine[3].maxhealth ,medicine[3].money );
break;
case 'm':
printf("请选择需要购买的魔法水:(增加魔法上限)\n\n1.小瓶魔法水(魔法:%.0lf  金币:%.0lf)\n2.中瓶魔法水(魔法:%.0lf  金币:%.0lf)\n3.大瓶魔法水(魔法:%.0lf  金币:%.0lf)\n4.小瓶魔法坛(魔法:%.0lf  金币:%.0lf)\n任意键返回\n",mofashui[0].maxhealth ,mofashui[0].money,mofashui[1].maxhealth ,mofashui[1].money,mofashui[2].maxhealth ,mofashui[2].money,mofashui[3].maxhealth ,mofashui[3].money );
break;
}
int k=getch(),f;
k-=48;
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<6; i++)
if( hero.maxhealth <=hf[i][0])
{
if(hero.money <hf[i][1])
cout<<"需要收费"<<hf[i][1]<<"金币,"<<name<<"王子,您的金币不足!"<<endl;
else
{
hero.health =hero.maxhealth ;
hero.magic  = hero.maxmagic ;
hero.money -=hf[i][1];
cout<<"花费"<<hf[i][1]<<"金币,状态恢复成功!"<<endl;
}
break;
}
}
else
cout<<"状态已满,无需恢复!"<<endl;
Sleep(500);
return;
}
void fuhuo()   //商店  复活界面
{
if(hero.money >200)
{
hero.health  = hero.maxhealth ;
hero.magic = hero.maxmagic ;
hero.money -=200;
}
else cout<<"金币不足!"<<endl;
}
void shop() //商店  主界面
{
while(1)
{
system("cls");
herostate();
printf("欢迎来到野外商店\n\n1.购买兵器\n2.购买衣服\n3.购买药水\n4.购买魔法\n5.升级技能\n6.恢复状态(按最高血量不同收费不同)\n任意键返回\n");
int k=getch();
k-=48;
system("cls");
switch(k)
{
case 1:
buysth('b');
break;
case 2:
buysth('y');
break;
case 3:
buysth('h');
break;
case 4:
buysth('m');
break;
case 5:
buyjineng();
break;
case 6:
huifu();
break;
default:
return;
}
}
}
void choiceboss()//选择boss主界面
{
cout<<"自动打怪还是手动打怪??\n\n1、自动(过程不暂停)\t\t2、手动(过程可控制)\n\n";
char autoORhand = getch();
while(autoORhand!='1'&&autoORhand!='2') autoORhand = getch();
cout<<"请设置每回合战斗过程显示时长:(默认500毫秒)\n\n";
int settime =500,a=1000;
while(a--)   //等待  1秒内有输入则接收,没有则默认500 
{
if(kbhit())
{
cin>>settime;
break;
}
Sleep(1);
}
while(1)   //选择BOSS界面 
{
system("cls");
herostate();
printf("欢迎闯关营救公主\n\n\n1.挑战1~17等级BOSS(按1~17数字)\n2.挑战终极BOSS(按18)\n3.随机挑战BOSS(按0,金币加成5%%)\n4.按其他键返回\n");
int jiacheng = 0;
int k=getch();
k-=48;
if(k==1)
{
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)
{
srand(time(NULL));
k=rand()%(bossmax-1);
jiacheng = 0.05;     //随机BOSS有5%的金币加成 
}
if(k>=0&&k<bossmax)
killboss(k,jiacheng,autoORhand,settime);
else return ;
}
}

void herouseskill(int num,int plusnum[4],double plus[4])
{
char p[4][10]= {"血","蓝","攻击","防御"};
double heroinfo[4] = {hero.maxhealth ,hero.maxmagic ,hero.attack ,hero.defense };
if(skilllevel[hero.skilllevel -1].magic >hero.magic ) cout<<"魔法不足,不能释放技能!\n\n";
else
{
hero.magic -=skilllevel[hero.skilllevel -1].magic ;
plusnum[num-1]=hero.skilllevel ;
plus[num-1]= hero.buff*heroinfo[num-1];
cout<<name<<"使用了加"<<p[num-1]<<"技能,增加了"<<plus[num-1]<<"点,持续"<<hero.skilllevel <<"回合\n\n";
}
}
 
void bossuseskill(int i,int bossnum[4],double bossplus[4],int *flag)
{
int j=i;
srand(time(NULL));
double bsinfo[5]= {bs[i].health,bs[i].magic,bs[i].attack,bs[i].defense,bs[i].buff };
int rnd = rand()%100;
int num[3][2]= {10,3,bossmax-2,5,bossmax-1,8};
if (i<10) i=0;
else if(i<=bossmax-2) i=1;
else if(i==bossmax-1) i=2;
//使用技能 有概率 
if(rnd/(num[i][1]*4)==0)//10级以下12%  17级以下20%  终极BOSS 32% 
{
char p[4][10]= {"血","蓝","攻击","防御"};
bossplus[rnd/num[i][1]]= bsinfo[rnd/num[i][1]]*bsinfo[4];//根据随机rnd的情况4选1 
bossnum[rnd/num[i][1]]=j/5+1;
cout<<"BOSS使用了加"<<p [rnd/num[i][1]]<<"技能,增加了"<< bossplus[rnd/num[i][1]]<<"点,持续"<< bossnum[rnd/num[i][1]]<<"回合!\n\n";
(*flag)++;
}
}

由于文章字数限制,我将剩下的代码贴到留言里面。


当然,如果有好的建议、意见,请多多评论、多多留言,以便改进,谢谢!

如果引用请注明出处,谢谢!


 

0.0分

7 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

前排资瓷~
2021-07-07 18:46:16
//续接6
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;
}
2021-06-19 17:19:47
//续接5
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*(1+jiacheng)<<"金币!\n离拯救出公主又近了一步!\n加油吧!!\n\n"<<endl;
hero.money += b.money *(1+jiacheng);
 
if(i+1==bossmax)  //终极BOSS挑战成功 
{
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");
2021-06-19 17:19:25
//续接4
cout<<name<<"王子对等级"<<i+1<<"的BOSS造成了"<<(atk>0? atk:0)<<"点伤害!\n"<<endl;
Sleep(settime);
}
//boss攻击 
if(b.health >0)
{
if((i<10&&flag <2) ||((i>=10&&i<bossmax-1)&&flag<4)||i==bossmax-1)//根据规则使用技能 
{
bossuseskill(i,bossnum,bossplus,&flag);
Sleep(settime);
}
//boss的技能加成 若无 置为0 
for(int j=0; j<4; j++)
{
if(bossnum[j]==0) bossplus[j]=0;
else bossnum[j]--;
}
b.health +=bossplus[0];
b.magic +=bossplus[1];
 
atk = (b.attack+bossplus[2]-hero.defense-plus[3] );
hero.health -=atk>0? atk:0;
cout<<"等级"<<i+1<<"的BOSS对"<<name<<"王子造成了"<< (atk>0? atk:0)<<"点伤害!\n"<<endl;
Sleep(settime);
}
2021-06-19 17:17:47
//续接3
}
hero.health = (hero.health +plus[0])>hero.maxhealth ? hero.maxhealth :(hero.health +plus[0]);
hero.magic  = (hero.magic +plus[1])>hero.maxmagic ? hero.maxmagic : (hero.magic +plus[1]);
//在技能都使用完的状态下,当hero攻击小于boss防御或者hero防御大于boss攻击时,需退出 
if(hero.defense >=b.attack &&hero.attack <=b.defense &&plusnum[2]==0&&plusnum[3]==0 &&bossnum[2]==0&&bossnum[3]==0 )
{
cout<<name<<"王子与等级"<<i+1<<"的BOSS打成了平手!\n"<<endl;
Sleep(settime);
break;
}
double atk;
//判断hero是否触发逃跑,若无则正常攻击,若有则跳过攻击 
if(!escape)
{
atk = (hero.attack+plus[2] -b.defense-bossplus[3] );
b.health -=atk>0? atk:0;
2021-06-19 17:17:06
//续接2
if(autoORhand =='1')  
{
int a= rand()%100;
if(a<10)       
{
int b = rand()%4; 
herouseskill(b+1,plusnum,plus);
}
Sleep(settime);
}
else if(autoORhand =='2')
{
cout<<"是否使用技能??\n\n使用技能请按   1、加血    2、加蓝    3、加攻击    4、加防御    5、逃跑\n\n按任意键跳过\n\n";
int kb = getch();
if(kb-48==5)
{
if(rand()%(i+1)>3)//高于3级的BOSS有几率逃跑失败 等级越高 几率越高 
{
cout<<"逃跑失败…………\n\n";
escape=1;
}
else
{
cout<<"逃跑成功!!!\n\n"<<endl;
Sleep(settime);
return ;
}
}
if(kb-48>=1&& kb-48<=4)
herouseskill(kb-48,plusnum,plus);
Sleep(settime);
}
//hero的技能加成 若无则置为0 
for(int j=0; j<4; j++)
{
if(plusnum[j]==0) plus[j]=0;
else plusnum[j]--;
2021-06-19 17:15:26
//续接1
void killboss(int i,float jiacheng,char autoORhand,int settime)   //正式战斗界面
{
system("cls");
boss b= bs[i];
int plusnum[4]= {0};//hero血 蓝 攻击 防御 的次数(等级几级就增加几次)
double plus[4]= {0};  //hero血 蓝 攻击 防御 增加数值
int flag =0;//boss使用技能状态(默认10级及以下最多使用2次,10级以上最多使用4次 ,终极boss使用不限 )
int bossnum[4]= {0}; //boss 使用技能后持续次数  加血 加蓝 加攻击 加防御
double bossplus[4]= {0};//boss 血 蓝 攻击 防御 增加数值
srand(time(NULL));
while(hero.health >0&& b.health >0)  //战斗中
{
int escape=0;//是否逃跑  
system("cls");
herostate();
bossstate(b);
2021-06-19 17:14:11
程序字数超过最大限度了,导致上传的代码出现错乱,我后面再试试,想办法将完整代码传上来
2021-06-19 02:28:38
  • «
  • 1
  • »