一、游戏名称:战斗之旅
游戏规则
- 角色选择:玩家可以选择不同的角色,每个角色都有不同的属性和技能。
- 商城:玩家可以访问商城购买不同的装备,包括武器和回复物品。
- 战斗:玩家可以与其他角色进行战斗。在战斗中,玩家可以选择攻击、使用物品或加点技能。
- 技能加点:玩家可以使用技能点提升生命、攻击力、防御力或余额。成功提升技能将增强角色能力。
游戏流程
- 开始游戏:玩家选择角色并进入游戏。
- 商城:玩家可以访问商城购买装备,提升角色能力。
- 战斗:玩家可以与其他角色进行战斗,通过策略和装备选择来击败对手。
- 技能加点:玩家可以使用技能点提升角色属性,增强角色实力。
游戏目标
玩家的目标是通过购买装备、提升角色能力和战斗来提升自己的角色,并最终击败其他角色,成为最强的战士。
二、程序编写:
1. 基础设置
1.1 清屏函数
我这里用的是Linux系统
void Clear_screen(void){//system("cls") ; //Windows系统: system("clear") ; //Linux/Unix系统://system("clear") ; //macOS系统:
}
1.2 平衡调整区域
#define Blood_max 100
#define skill_min 10 //最初技能值
#define skill_ix 20 //技能上限 ,后面添加会有失败可能#define attack_init 3 //最初伤害
#define money_init 299 //最初的金币#define Defensive_init 10 //最初护盾
#define artillery_min 30 //大炮伤害最小时
#define artillery_ix 60
2. 角色配置:
2.1 人物数据显示名称
void init_ZT(void){ //显示人物所有数据cout<<"名 字 血量 武器名 攻击力 防御力 技能 余额 ID "<<endl;
}
2.2 角色类
//角色
class role{
public:int Blood ; //血量 int balance ; //余额int attack ; //攻击力int Defensive ; //防御力int skill ; //技能 可用于+防御力,攻击力,回复血量,技能值int number ; //角色IDchar role_name[20] ; //角色名char equip_name[20] ; //武器名char Defensive_name[20] ; //防御武器名//自定义值role(int y, int a,int r ,int h, int n, int b){balance = y;attack = a;Defensive = r;skill = h;number = n;Blood = b;}role(void){balance = 0;attack = 0;Defensive = 0;skill = 0;number = 0;Blood = 0;}//行为void state(){cout<<role_name <<" ## "<<Blood <<" ## "<<equip_name <<" "<<attack <<" "<<Defensive <<" "<<skill <<" "<<balance <<" "<<number <<endl;}
};
2.3 初始化角色信息
// 初始化角色信息
void init_role(role* r, const char* name, int n) {// 设置角色名strcpy(r->role_name, name);//初始化武器名strcpy(r->equip_name, "拳 头");// 设置角色属性r->balance = money_init;r->attack = attack_init;r->Defensive = Defensive_init;r->skill = skill_min;r->number = n;r->Blood = Blood_max;}
2.4 创建角色
//创建角色
role soldier ; //小兵
role marshal ; //元帅
role Monon ; //魔龙
role player ; //玩家
role player_2 ; //玩家2
2.5 显示所有角色
void role_news(void){cout<<"********************** 角色信息 *************************"<<endl;init_ZT(); //显示人物所有数据名称cout<<"######################################################"<<endl;soldier.state();marshal.state();Monon.state();player.state();player_2.state();/*cout<<soldier.role_name <<"##"<<soldier.equip_name <<" "<<soldier.attack <<" "<<soldier.Defensive <<" "<<soldier.skill <<" "<<soldier.balance <<" "<<soldier.number <<endl;cout<<marshal.role_name <<"##"<<marshal.equip_name <<" "<<marshal.attack <<" "<<marshal.Defensive <<" "<<marshal.skill <<" "<<marshal.balance <<" "<<marshal.number <<endl;cout<<Monon.role_name <<"##"<<Monon.equip_name <<" "<<Monon.attack <<" "<<Monon.Defensive <<" "<<Monon.skill <<" "<<Monon.balance <<" "<<Monon.number <<endl;*/cout<<"######################################################"<<endl;
3. 装备配置:
3.1 装备类
//装备属性
class Equipment_attributes
{
public:char name[20];int attack ; //攻击力int Defensive ; //防御力int price ; //价格int reply ; //回血能力int number ; //物品编号//自定义值Equipment_attributes(int y, int a,int r ,int h, int n){attack = y;Defensive = a;reply = r;price = h;number = n;}//默认值Equipment_attributes(void){attack = 0;Defensive = 0;reply = 0;price = 0;number = 0;}void state(void){cout<<name <<" "<<attack <<" "<<Defensive <<" "<<reply <<" "<<price <<" "<<number <<endl;}
};
3.2 创建装备
//攻击性武器 1-100;
Equipment_attributes fist (attack_init ,0,0, 0 ,1); //装备:拳头 编号 = 1
Equipment_attributes knife (5 ,0,0, 10 ,2); //装备:刀 编号 = 2
Equipment_attributes sword (10 ,0,0, 30 ,3); //装备:剑 编号 = 3
Equipment_attributes Crossbow (15 ,0,0, 40 ,4); //装备:弓弩 编号 = 4
Equipment_attributes pistol (30 ,0,0, 300 ,5); //装备:手枪 编号 = 5
Equipment_attributes artillery (artillery_min ,0,0, 500 ,6); //装备:大炮 编号 = 6//回复性武器 101-200;
Equipment_attributes bandage (0, 0, 5, 5 , 101); //装备:绷带 编号 = 101
Equipment_attributes beverage (0, 0, 10, 10, 102); //装备:饮料 编号 = 102
Equipment_attributes Painkillers (0, 0, 15, 15, 103); //装备:止痛药 编号 = 103
Equipment_attributes First_aid_kit (0, 0, 50, 50, 104); //装备:急救包 编号 = 104
Equipment_attributes Medical_kit (0, 0, 100, 100, 105); //装备:医疗箱 编号 = 105
3.3 初始化装备名称
//初始化装备名字
void init_Equipment_name(){strcpy(fist.name, "拳 头"); //装备:拳头 编号 = 1strcpy(knife.name, "小 刀"); //装备:刀 编号 = 2strcpy(sword.name, "大 剑"); //装备:剑 编号 = 3strcpy(Crossbow.name, "弓 弩"); //装备:弓弩 编号 = 4strcpy(pistol.name, "手 枪"); //装备:手枪 编号 = 5strcpy(artillery.name, "大 炮"); //装备:大炮 编号 = 6strcpy(bandage.name, "绷 带"); //装备:绷带 编号 = 101strcpy(beverage.name, "饮 料"); //装备:饮料 编号 = 102strcpy(Painkillers.name, "止痛药"); //装备:止痛药 编号 = 103strcpy(First_aid_kit.name, "急救包"); //装备:急救包 编号 = 104strcpy(Medical_kit.name, "医疗箱"); //装备:医疗箱 编号 = 105return;
}
4. 商城显示:
void mall(void){cout<<"********************** 商城 *************************"<<endl;cout<<"商 品 攻击力 防御力 回复 价格 编号 "<<endl;cout<<"######################################################"<<endl;fist.state();knife.state();sword.state();Crossbow.state();pistol.state();artillery.state();cout<<endl;bandage.state();beverage.state();Painkillers.state();First_aid_kit.state();Medical_kit.state();/*cout<<fist.name <<" "<<fist.attack <<" "<<fist.Defensive <<" "<<fist.reply <<" "<<fist.price <<" "<<fist.number <<endl;cout<<knife.name <<" "<<knife.attack <<" "<<knife.Defensive <<" "<<knife.reply <<" "<<knife.price <<" "<<knife.number <<endl;cout<<sword.name <<" "<<sword.attack <<" "<<sword.Defensive <<" "<<sword.reply <<" "<<sword.price <<" "<<sword.number <<endl;cout<<Crossbow.name <<" "<<Crossbow.attack <<" "<<Crossbow.Defensive <<" "<<Crossbow.reply <<" "<<Crossbow.price <<" "<<Crossbow.number <<endl;cout<<pistol.name <<" "<<pistol.attack <<" "<<pistol.Defensive <<" "<<pistol.reply <<" "<<pistol.price <<" "<<pistol.number <<endl;cout<<artillery.name<<" "<<artillery.attack <<" "<<artillery.Defensive <<" "<<artillery.reply <<" "<<artillery.price <<" "<<artillery.number <<endl;cout<<bandage.name <<" "<<bandage.attack <<" "<<bandage.Defensive <<" " <<bandage.reply <<" "<<bandage.price <<" "<<bandage.number <<endl;cout<<beverage.name <<" "<<beverage.attack <<" "<<beverage.Defensive <<" "<<beverage.reply <<" "<<beverage.price <<" "<<beverage.number <<endl;cout<<Painkillers.name <<" "<<Painkillers.attack <<" "<<Painkillers.Defensive <<" "<<Painkillers.reply <<" "<<Painkillers.price <<" "<<Painkillers.number <<endl;cout<<First_aid_kit.name <<" "<<First_aid_kit.attack <<" "<<First_aid_kit.Defensive <<" "<<First_aid_kit.reply <<" "<<First_aid_kit.price <<" "<<First_aid_kit.number <<endl;cout<<Medical_kit.name <<" "<<Medical_kit.attack <<" "<<Medical_kit.Defensive <<" "<<Medical_kit.reply <<" "<<Medical_kit.price <<" "<<Medical_kit.number <<endl;*/cout<<"######################################################"<<endl;return;
}
5. 游戏界面:
5.1 攻击处理函数
这里面原本大炮伤害100太高,为平衡游戏体验,所以此处将大炮伤害改为随机。
//攻击处理函数
void attack_main(role* p1, role* p2){Clear_screen(); //清屏int sc = p1 -> attack;int order = 1;cout<<p1 -> role_name <<" 用 "<<p1 -> equip_name << " 向 " <<p2 -> role_name<<" 发起了进攻"<<endl<<endl;if(strcmp(p1 -> equip_name, artillery.name) == 0){ //如果是大炮的话sc = sc + rand() % artillery_ix; //生成随机伤害}//先效果判定//先攻击护甲p2 -> Defensive = p2 -> Defensive - sc;//如果护甲破损,剩余伤害直击生命if(p2 -> Defensive < 0){cout<<p2 -> role_name <<"受到了 <<<<< "<<p1 -> role_name<<" 攻击的 "<< (- p2 -> Defensive)<<"点伤害 "<<endl<<endl;p2 -> Blood = p2 -> Blood + p2 -> Defensive;p2 -> Defensive = 0;}elsecout<<p2 -> role_name <<"挡住了 ||| "<<p1 -> role_name<<" 的 "<< sc <<" 点伤害"<<endl<<endl;cout<<"输入任意值";cin>>order;return;
}
5.2 返回对应物品编号的所以属性
Equipment_attributes* goods(int i){//获取相应物品的属性switch(i){case 1:{return &fist;break;}case 2:{return &knife;break;}case 3:{return &sword;break;}case 4:{return &Crossbow;break;}case 5:{return &pistol;break;}case 6:{return &artillery;break;}case 101:{return &bandage;break;}case 102:{return &beverage;break;}case 103:{return &Painkillers;break;}case 104:{return &First_aid_kit;break;}case 105:{return &Medical_kit;break;}//未购买装备default: return &fist;}
}
5.3 查看是否有此购物编号
//用于查看是否有此购物编号
int goods_number(int i){//获取相应物品的属性switch(i){case 1:{return fist.number;break;}case 2:{return knife.number;break;}case 3:{return sword.number;break;}case 4:{return Crossbow.number;break;}case 5:{return pistol.number;break;}case 6:{return artillery.number;break;}case 101:{return bandage.number;break;}case 102:{return beverage.number;break;}case 103:{return Painkillers.number;break;}case 104:{return First_aid_kit.number;break;}case 105:{return Medical_kit.number;break;}//未购买装备default: return -1;}
}
5.4 购买并使用商品
//购买并使用商品
int shop(role* p){int en = -1;int order = 1;while(order != 0){int GD = 1;while(GD){Clear_screen(); //清屏mall(); //显示所有装备init_ZT(); //显示人物所有数据名称p -> state(); //显示所有数据cout<<"输入物品编(0退出):";cin>>order;if(order == 0){GD = 0;en = -1; //直接退出,购买失败break;}if(goods_number(order) == -1) {Clear_screen();cout<<"输入编号无效: 任意键继续:";cin>>order;GD = 1; //退出输入}else GD = 0; //输入值有效}if(GD == 0){ //输入值有效Equipment_attributes* p3; //保存要购买商品的所有信息p3 = goods(order); //获取信息if(p -> balance < p3 -> price){cout<<endl<<endl<<"您的余额不足"<<endl<<"任意键继续 (0退出):";cin>>order;}else{ //买得起en = 1; //表示购买成功order = 0; //退出p -> balance = p -> balance - p3 ->price; //付钱Clear_screen(); //清屏cout<<endl<<"购买成功!!!"<<endl<<endl;cout<<p -> role_name<<"得到了如下提升"<<endl<<endl;if(p3 -> attack != 0){p -> attack = p3 -> attack ;//跟新攻击伤害strcpy(p -> equip_name, p3 -> name) ;//跟新武器名字cout<<"装备武器"<<p3 -> name <<"伤害变为 --》》 "<<p3 -> attack <<endl;}if(p3 -> reply != 0){int Blood_cnt;Blood_cnt = p -> Blood; //保存未使用时血量p -> Blood = p -> Blood + p3 -> reply ;//回血if(p -> Blood > 100) p -> Blood = Blood_max; //最高血量cout<<endl<<"使用了"<<p3 -> name <<"回复了"<<p -> Blood - Blood_cnt <<endl;}}}}if(en == 1){ //购买成功cout<<"任意键继续 :";cin>>order;}else{ //购买失败//和界面交换一起,使得结果是保持原样cout<<" 购买失败 "<<endl;cout<<"任意键继续:";cin>>order;//p3 = p1; p1 = p2; p2 = p3;}return en; //购买失败
}
5.5 技能加点
技能原本是直接加,但技能到80+,玩家就比较无敌了,所以也改成了随机。
int main_skill(role* p){int en = -1;int order = 1;while(order != 0){Clear_screen(); //清屏cout<<"******** 技能加点 *************"<<endl;cout<<"说明:可使用此数据增加以下数值 --- 技能大于等于 40 升级技能(2) 有1/2失败"<<endl;cout<<"退出:0 生命:1"<<endl;cout<<"技能:2 攻击:3"<<endl;cout<<"防御:4: 余额:5"<<endl;init_ZT(); //显示人物所有数据名称p -> state();cout<<"请输入您的选择(0退出):";cin>>order;cout<<endl<<endl;switch(order){case 1:{cout<<"您的生命由:"<<p -> Blood<<" 提升到了";p -> Blood = p -> Blood + p -> skill; //技能+点cout<<p -> Blood<<endl;break;}case 2:{int skill_n = 1;if(rand() % 2 == 0 && p -> skill >= skill_ix) skill_n = 0;if(skill_n == 1){cout<<"您的技能由:"<<p -> skill<<" 提升到了";p -> skill = p -> skill + p -> skill; //技能+点cout<<p -> skill<<endl;}else{cout<<"您的技能点提升失败"<<endl;cout<<"任意键继续";cin>>order;return 1;}break;}case 3:{cout<<"您的攻击力由:"<<p -> attack<<" 提升到了";p -> attack = p -> attack + p -> skill; //技能+点cout<<p -> attack<<endl;break;}case 4:{cout<<"您的防御力由:"<<p -> Defensive<<" 提升到了";p -> Defensive = p -> Defensive + p -> skill; //技能+点cout<<p -> Defensive<<endl;break;}case 5:{cout<<"您的余额由:"<<p -> balance<<" 提升到了";p -> balance = p -> balance + p -> skill; //技能+点cout<<p -> balance<<endl;break;}default: en = -1;//技能为加点}if(order > 0 && order <= 5)//成功加点 {en = 1;break; //退出循环}cout<<"输入任意值继续 (0)退出";cin>>order;}if(en == 1){cout<<"加点成功 "<<endl;cout<<"任意键继续";cin>>order;}else{cout<<"加点失败 "<<endl;cout<<"任意键继续";cin>>order;}return en; //未使用技能
}
5.6 成败判定
//判断是否击败对方
int success(role* p){if(p -> Blood <= 0) return 1; //死亡else return -1;
}
5.7 战斗主界面
//战斗界面 rj != 0;时是单人模式
void battle(role* p1, role* p2, int rj) {int order = 1;role* p3;while(order != 0){Clear_screen(); //清屏//胜利判定 因为所有操作都是由于1发出,所以只需要判定P2if(success(p2) == 1){cout<<"恭喜 "<<p1 -> role_name << "击败"<< p2 ->role_name<<endl;cout<<"游戏结束 "<<p1 -> role_name<<" 胜利"<<endl;cout<<"任意键继续 (0退出):";cin>>order;return;}cout<<"########## 战斗 ###########"<<endl;cout<<"退出游戏 0 攻击 1"<<endl;cout<<"使用物品 2 技能 3"<<endl<<endl;//交换游戏回合p3 = p1; p1 = p2; p2 = p3;init_ZT(); //显示人物所有数据名称p1 -> state();p2 -> state();cout<<endl<<p1 -> role_name <<"------>>>>>>"<<p2 -> role_name<<endl;cout<<endl;cout<<"当前是 "<<p1 -> role_name<<" 的回合"<<endl<<endl;cout<<p1 -> role_name<<"请输入您的指令:";cin>>order;switch(order){case 1:{attack_main(p1,p2); //进行攻击处理break;}case 2:{if(shop(p1) == 1){ //购买成功}else{ //购买失败//和界面交换一起,使得结果是保持原样p3 = p1; p1 = p2; p2 = p3;}break;}case 3:{if(main_skill(p1) == 1){cout<<"加点成功 "<<endl;cout<<"任意键继续 (0退出游戏):";cin>>order;}else{cout<<"加点失败 "<<endl;cout<<"任意键继续 (0退出游戏):";cin>>order;}break;}}}return;
}
5.8 游戏界面(只有双人模式)
本次并未实现单人游戏,人机还没时间写,欢迎大佬们改良。
//游戏界面
void interface(void){int order = 1;while(order != 0){Clear_screen(); //清屏cout<<"########## 游戏界面 ###########"<<endl;cout<<"双人mode 1"<<endl;cout<<"单人mode 2"<<endl;cout<<"(0退出)请选择mode:";cin>>order;if(order == 1){battle(&player,&player_2, 0); //双人模式}//单人模式else if(order == 2){}}return ;
}
6. 主函数
int main()
{init_Equipment_name(); //初始化武器名srand(time(NULL)); //初始化随机数//mall();int order;while(1){//创建英雄init_role(&soldier , "小 兵", 1);init_role(&marshal , "元 帅", 2);init_role(&Monon , "魔 龙",101);init_role(&player , "玩 家1", 0);init_role(&player_2 , "玩 家2", 1000);Clear_screen(); //清屏cout<<"########## 主页 ###########"<<endl;cout<<"查看商城:1 查看角色:2"<<endl;cout<<"进入游戏:3"<<endl;cout<<"请输入您的选择:";cin>>order; //输入命令Clear_screen(); //清屏switch(order){case 1: {mall();cout<<"任意值退出:";cin>>order;break;}case 2: {role_news();cout<<"任意值退出:";cin>>order;break;}case 3:{interface();//游戏界面break;}}}return 0;
}
三、总代码:
#include "iostream"
#include <stdlib.h>
#include <string.h>
#include <time.h>using namespace std;
#define Blood_max 100
#define skill_min 10 //最初技能值
#define skill_ix 20 //技能上限 ,后面添加会有失败可能#define attack_init 3 //最初伤害
#define money_init 299 //最初的金币#define Defensive_init 10 //最初护盾
#define artillery_min 30 //大炮伤害最小时
#define artillery_ix 60 void Clear_screen(void){//system("cls") ; //Windows系统: system("clear") ; //Linux/Unix系统://system("clear") ; //macOS系统:
}/角色配置
/**************************************************/
void init_ZT(void){ //显示人物所有数据cout<<"名 字 血量 武器名 攻击力 防御力 技能 余额 ID "<<endl;
}//角色
class role{
public:int Blood ; //血量 int balance ; //余额int attack ; //攻击力int Defensive ; //防御力int skill ; //技能 可用于+防御力,攻击力,回复血量,技能值int number ; //角色IDchar role_name[20] ; //角色名char equip_name[20] ; //武器名char Defensive_name[20] ; //防御武器名//自定义值role(int y, int a,int r ,int h, int n, int b){balance = y;attack = a;Defensive = r;skill = h;number = n;Blood = b;}role(void){balance = 0;attack = 0;Defensive = 0;skill = 0;number = 0;Blood = 0;}//行为void state(){cout<<role_name <<" ## "<<Blood <<" ## "<<equip_name <<" "<<attack <<" "<<Defensive <<" "<<skill <<" "<<balance <<" "<<number <<endl;}
};// 初始化角色信息
void init_role(role* r, const char* name, int n) {// 设置角色名strcpy(r->role_name, name);//初始化武器名strcpy(r->equip_name, "拳 头");// 设置角色属性r->balance = money_init;r->attack = attack_init;r->Defensive = Defensive_init;r->skill = skill_min;r->number = n;r->Blood = Blood_max;}//创建角色
role soldier ; //小兵
role marshal ; //元帅
role Monon ; //魔龙
role player ; //玩家
role player_2 ; //玩家2void role_news(void){cout<<"********************** 角色信息 *************************"<<endl;init_ZT(); //显示人物所有数据名称cout<<"######################################################"<<endl;soldier.state();marshal.state();Monon.state();player.state();player_2.state();/*cout<<soldier.role_name <<"##"<<soldier.equip_name <<" "<<soldier.attack <<" "<<soldier.Defensive <<" "<<soldier.skill <<" "<<soldier.balance <<" "<<soldier.number <<endl;cout<<marshal.role_name <<"##"<<marshal.equip_name <<" "<<marshal.attack <<" "<<marshal.Defensive <<" "<<marshal.skill <<" "<<marshal.balance <<" "<<marshal.number <<endl;cout<<Monon.role_name <<"##"<<Monon.equip_name <<" "<<Monon.attack <<" "<<Monon.Defensive <<" "<<Monon.skill <<" "<<Monon.balance <<" "<<Monon.number <<endl;*/cout<<"######################################################"<<endl;
}/*****************************************************************************************//********************* 装备配置 **************************///装备属性
class Equipment_attributes
{
public:char name[20];int attack ; //攻击力int Defensive ; //防御力int price ; //价格int reply ; //回血能力int number ; //物品编号//自定义值Equipment_attributes(int y, int a,int r ,int h, int n){attack = y;Defensive = a;reply = r;price = h;number = n;}//默认值Equipment_attributes(void){attack = 0;Defensive = 0;reply = 0;price = 0;number = 0;}void state(void){cout<<name <<" "<<attack <<" "<<Defensive <<" "<<reply <<" "<<price <<" "<<number <<endl;}
};//攻击性武器 1-100;
Equipment_attributes fist (attack_init ,0,0, 0 ,1); //装备:拳头 编号 = 1
Equipment_attributes knife (5 ,0,0, 10 ,2); //装备:刀 编号 = 2
Equipment_attributes sword (10 ,0,0, 30 ,3); //装备:剑 编号 = 3
Equipment_attributes Crossbow (15 ,0,0, 40 ,4); //装备:弓弩 编号 = 4
Equipment_attributes pistol (30 ,0,0, 300 ,5); //装备:手枪 编号 = 5
Equipment_attributes artillery (artillery_min ,0,0, 500 ,6); //装备:大炮 编号 = 6//回复性武器 101-200;
Equipment_attributes bandage (0, 0, 5, 5 , 101); //装备:绷带 编号 = 101
Equipment_attributes beverage (0, 0, 10, 10, 102); //装备:饮料 编号 = 102
Equipment_attributes Painkillers (0, 0, 15, 15, 103); //装备:止痛药 编号 = 103
Equipment_attributes First_aid_kit (0, 0, 50, 50, 104); //装备:急救包 编号 = 104
Equipment_attributes Medical_kit (0, 0, 100, 100, 105); //装备:医疗箱 编号 = 105//初始化装备名字
void init_Equipment_name(){strcpy(fist.name, "拳 头"); //装备:拳头 编号 = 1strcpy(knife.name, "小 刀"); //装备:刀 编号 = 2strcpy(sword.name, "大 剑"); //装备:剑 编号 = 3strcpy(Crossbow.name, "弓 弩"); //装备:弓弩 编号 = 4strcpy(pistol.name, "手 枪"); //装备:手枪 编号 = 5strcpy(artillery.name, "大 炮"); //装备:大炮 编号 = 6strcpy(bandage.name, "绷 带"); //装备:绷带 编号 = 101strcpy(beverage.name, "饮 料"); //装备:饮料 编号 = 102strcpy(Painkillers.name, "止痛药"); //装备:止痛药 编号 = 103strcpy(First_aid_kit.name, "急救包"); //装备:急救包 编号 = 104strcpy(Medical_kit.name, "医疗箱"); //装备:医疗箱 编号 = 105return;
}void mall(void){cout<<"********************** 商城 *************************"<<endl;cout<<"商 品 攻击力 防御力 回复 价格 编号 "<<endl;cout<<"######################################################"<<endl;fist.state();knife.state();sword.state();Crossbow.state();pistol.state();artillery.state();cout<<endl;bandage.state();beverage.state();Painkillers.state();First_aid_kit.state();Medical_kit.state();/*cout<<fist.name <<" "<<fist.attack <<" "<<fist.Defensive <<" "<<fist.reply <<" "<<fist.price <<" "<<fist.number <<endl;cout<<knife.name <<" "<<knife.attack <<" "<<knife.Defensive <<" "<<knife.reply <<" "<<knife.price <<" "<<knife.number <<endl;cout<<sword.name <<" "<<sword.attack <<" "<<sword.Defensive <<" "<<sword.reply <<" "<<sword.price <<" "<<sword.number <<endl;cout<<Crossbow.name <<" "<<Crossbow.attack <<" "<<Crossbow.Defensive <<" "<<Crossbow.reply <<" "<<Crossbow.price <<" "<<Crossbow.number <<endl;cout<<pistol.name <<" "<<pistol.attack <<" "<<pistol.Defensive <<" "<<pistol.reply <<" "<<pistol.price <<" "<<pistol.number <<endl;cout<<artillery.name<<" "<<artillery.attack <<" "<<artillery.Defensive <<" "<<artillery.reply <<" "<<artillery.price <<" "<<artillery.number <<endl;cout<<bandage.name <<" "<<bandage.attack <<" "<<bandage.Defensive <<" " <<bandage.reply <<" "<<bandage.price <<" "<<bandage.number <<endl;cout<<beverage.name <<" "<<beverage.attack <<" "<<beverage.Defensive <<" "<<beverage.reply <<" "<<beverage.price <<" "<<beverage.number <<endl;cout<<Painkillers.name <<" "<<Painkillers.attack <<" "<<Painkillers.Defensive <<" "<<Painkillers.reply <<" "<<Painkillers.price <<" "<<Painkillers.number <<endl;cout<<First_aid_kit.name <<" "<<First_aid_kit.attack <<" "<<First_aid_kit.Defensive <<" "<<First_aid_kit.reply <<" "<<First_aid_kit.price <<" "<<First_aid_kit.number <<endl;cout<<Medical_kit.name <<" "<<Medical_kit.attack <<" "<<Medical_kit.Defensive <<" "<<Medical_kit.reply <<" "<<Medical_kit.price <<" "<<Medical_kit.number <<endl;*/cout<<"######################################################"<<endl;return;
}
/**************************************************************************//********游戏界面*************************************/
//攻击处理函数
void attack_main(role* p1, role* p2){Clear_screen(); //清屏int sc = p1 -> attack;int order = 1;cout<<p1 -> role_name <<" 用 "<<p1 -> equip_name << " 向 " <<p2 -> role_name<<" 发起了进攻"<<endl<<endl;if(strcmp(p1 -> equip_name, artillery.name) == 0){ //如果是大炮的话sc = sc + rand() % artillery_ix; //生成随机伤害}//先效果判定//先攻击护甲p2 -> Defensive = p2 -> Defensive - sc;//如果护甲破损,剩余伤害直击生命if(p2 -> Defensive < 0){cout<<p2 -> role_name <<"受到了 <<<<< "<<p1 -> role_name<<" 攻击的 "<< (- p2 -> Defensive)<<"点伤害 "<<endl<<endl;p2 -> Blood = p2 -> Blood + p2 -> Defensive;p2 -> Defensive = 0;}elsecout<<p2 -> role_name <<"挡住了 ||| "<<p1 -> role_name<<" 的 "<< sc <<" 点伤害"<<endl<<endl;cout<<"输入任意值";cin>>order;return;
}Equipment_attributes* goods(int i){//获取相应物品的属性switch(i){case 1:{return &fist;break;}case 2:{return &knife;break;}case 3:{return &sword;break;}case 4:{return &Crossbow;break;}case 5:{return &pistol;break;}case 6:{return &artillery;break;}case 101:{return &bandage;break;}case 102:{return &beverage;break;}case 103:{return &Painkillers;break;}case 104:{return &First_aid_kit;break;}case 105:{return &Medical_kit;break;}//未购买装备default: return &fist;}
}//用于查看是否有此购物编号
int goods_number(int i){//获取相应物品的属性switch(i){case 1:{return fist.number;break;}case 2:{return knife.number;break;}case 3:{return sword.number;break;}case 4:{return Crossbow.number;break;}case 5:{return pistol.number;break;}case 6:{return artillery.number;break;}case 101:{return bandage.number;break;}case 102:{return beverage.number;break;}case 103:{return Painkillers.number;break;}case 104:{return First_aid_kit.number;break;}case 105:{return Medical_kit.number;break;}//未购买装备default: return -1;}
}//购买并使用商品
int shop(role* p){int en = -1;int order = 1;while(order != 0){int GD = 1;while(GD){Clear_screen(); //清屏mall(); //显示所有装备init_ZT(); //显示人物所有数据名称p -> state(); //显示所有数据cout<<"输入物品编(0退出):";cin>>order;if(order == 0){GD = 0;en = -1; //直接退出,购买失败break;}if(goods_number(order) == -1) {Clear_screen();cout<<"输入编号无效: 任意键继续:";cin>>order;GD = 1; //退出输入}else GD = 0; //输入值有效}if(GD == 0){ //输入值有效Equipment_attributes* p3; //保存要购买商品的所有信息p3 = goods(order); //获取信息if(p -> balance < p3 -> price){cout<<endl<<endl<<"您的余额不足"<<endl<<"任意键继续 (0退出):";cin>>order;}else{ //买得起en = 1; //表示购买成功order = 0; //退出p -> balance = p -> balance - p3 ->price; //付钱Clear_screen(); //清屏cout<<endl<<"购买成功!!!"<<endl<<endl;cout<<p -> role_name<<"得到了如下提升"<<endl<<endl;if(p3 -> attack != 0){p -> attack = p3 -> attack ;//跟新攻击伤害strcpy(p -> equip_name, p3 -> name) ;//跟新武器名字cout<<"装备武器"<<p3 -> name <<"伤害变为 --》》 "<<p3 -> attack <<endl;}if(p3 -> reply != 0){int Blood_cnt;Blood_cnt = p -> Blood; //保存未使用时血量p -> Blood = p -> Blood + p3 -> reply ;//回血if(p -> Blood > 100) p -> Blood = Blood_max; //最高血量cout<<endl<<"使用了"<<p3 -> name <<"回复了"<<p -> Blood - Blood_cnt <<endl;}}}}if(en == 1){ //购买成功cout<<"任意键继续 :";cin>>order;}else{ //购买失败//和界面交换一起,使得结果是保持原样cout<<" 购买失败 "<<endl;cout<<"任意键继续:";cin>>order;//p3 = p1; p1 = p2; p2 = p3;}return en; //购买失败
}int main_skill(role* p){int en = -1;int order = 1;while(order != 0){Clear_screen(); //清屏cout<<"******** 技能加点 *************"<<endl;cout<<"说明:可使用此数据增加以下数值 --- 技能大于等于 40 升级技能(2) 有1/2失败"<<endl;cout<<"退出:0 生命:1"<<endl;cout<<"技能:2 攻击:3"<<endl;cout<<"防御:4: 余额:5"<<endl;init_ZT(); //显示人物所有数据名称p -> state();cout<<"请输入您的选择(0退出):";cin>>order;cout<<endl<<endl;switch(order){case 1:{cout<<"您的生命由:"<<p -> Blood<<" 提升到了";p -> Blood = p -> Blood + p -> skill; //技能+点cout<<p -> Blood<<endl;break;}case 2:{int skill_n = 1;if(rand() % 2 == 0 && p -> skill >= skill_ix) skill_n = 0;if(skill_n == 1){cout<<"您的技能由:"<<p -> skill<<" 提升到了";p -> skill = p -> skill + p -> skill; //技能+点cout<<p -> skill<<endl;}else{cout<<"您的技能点提升失败"<<endl;cout<<"任意键继续";cin>>order;return 1;}break;}case 3:{cout<<"您的攻击力由:"<<p -> attack<<" 提升到了";p -> attack = p -> attack + p -> skill; //技能+点cout<<p -> attack<<endl;break;}case 4:{cout<<"您的防御力由:"<<p -> Defensive<<" 提升到了";p -> Defensive = p -> Defensive + p -> skill; //技能+点cout<<p -> Defensive<<endl;break;}case 5:{cout<<"您的余额由:"<<p -> balance<<" 提升到了";p -> balance = p -> balance + p -> skill; //技能+点cout<<p -> balance<<endl;break;}default: en = -1;//技能为加点}if(order > 0 && order <= 5)//成功加点 {en = 1;break; //退出循环}cout<<"输入任意值继续 (0)退出";cin>>order;}if(en == 1){cout<<"加点成功 "<<endl;cout<<"任意键继续";cin>>order;}else{cout<<"加点失败 "<<endl;cout<<"任意键继续";cin>>order;}return en; //未使用技能
}//判断是否击败对方
int success(role* p){if(p -> Blood <= 0) return 1; //死亡else return -1;
}//战斗界面 rj != 0;时是单人模式
void battle(role* p1, role* p2, int rj) {int order = 1;role* p3;while(order != 0){Clear_screen(); //清屏//胜利判定 因为所有操作都是由于1发出,所以只需要判定P2if(success(p2) == 1){cout<<"恭喜 "<<p1 -> role_name << "击败"<< p2 ->role_name<<endl;cout<<"游戏结束 "<<p1 -> role_name<<" 胜利"<<endl;cout<<"任意键继续 (0退出):";cin>>order;return;}cout<<"########## 战斗 ###########"<<endl;cout<<"退出游戏 0 攻击 1"<<endl;cout<<"使用物品 2 技能 3"<<endl<<endl;//交换游戏回合p3 = p1; p1 = p2; p2 = p3;init_ZT(); //显示人物所有数据名称p1 -> state();p2 -> state();cout<<endl<<p1 -> role_name <<"------>>>>>>"<<p2 -> role_name<<endl;cout<<endl;cout<<"当前是 "<<p1 -> role_name<<" 的回合"<<endl<<endl;cout<<p1 -> role_name<<"请输入您的指令:";cin>>order;switch(order){case 1:{attack_main(p1,p2); //进行攻击处理break;}case 2:{if(shop(p1) == 1){ //购买成功}else{ //购买失败//和界面交换一起,使得结果是保持原样p3 = p1; p1 = p2; p2 = p3;}break;}case 3:{if(main_skill(p1) == 1){cout<<"加点成功 "<<endl;cout<<"任意键继续 (0退出游戏):";cin>>order;}else{cout<<"加点失败 "<<endl;cout<<"任意键继续 (0退出游戏):";cin>>order;}break;}}}return;
}//游戏界面
void interface(void){int order = 1;while(order != 0){Clear_screen(); //清屏cout<<"########## 游戏界面 ###########"<<endl;cout<<"双人mode 1"<<endl;cout<<"单人mode 2"<<endl;cout<<"(0退出)请选择mode:";cin>>order;if(order == 1){battle(&player,&player_2, 0); //双人模式}//单人模式else if(order == 2){}}return ;
}*******************************************************/
int main()
{init_Equipment_name(); //初始化武器名srand(time(NULL)); //初始化随机数//mall();int order;while(1){//创建英雄init_role(&soldier , "小 兵", 1);init_role(&marshal , "元 帅", 2);init_role(&Monon , "魔 龙",101);init_role(&player , "玩 家1", 0);init_role(&player_2 , "玩 家2", 1000);Clear_screen(); //清屏cout<<"########## 主页 ###########"<<endl;cout<<"查看商城:1 查看角色:2"<<endl;cout<<"进入游戏:3"<<endl;cout<<"请输入您的选择:";cin>>order; //输入命令Clear_screen(); //清屏switch(order){case 1: {mall();cout<<"任意值退出:";cin>>order;break;}case 2: {role_news();cout<<"任意值退出:";cin>>order;break;}case 3:{interface();//游戏界面break;}}}return 0;
}
四、代码总结:
- 实现了基本的角色选择、商城购买装备、战斗系统和技能加点等功能。
- 使用了面向对象的编程方法,通过角色类和装备类的定义,使得代码结构清晰。
- 提供了基本的交互界面,玩家可以通过控制台输入进行游戏操作。
不足之处
- 缺乏注释:代码中缺乏足够的注释,使得阅读和理解代码的难度增加。
- 可读性较差:代码结构可以更清晰一些,例如可以将游戏逻辑封装成函数,提高可读性和可维护性。
- 缺乏错误处理:代码中未包含对用户输入的错误处理,导致用户输入错误时程序可能无法正确响应。
- 单一性:当前只实现了基本的战斗功能,缺乏更多元化的游戏玩法,例如任务系统、地图探索等。
改良意见
- 添加注释:在代码中加入适当的注释,解释关键的代码逻辑和功能,提高代码的可读性。
- 优化代码结构:将游戏逻辑封装成函数,提高代码的模块化和可维护性。
- 引入错误处理:在用户输入处加入错误处理,对用户输入进行验证和处理,以提高程序的稳定性。
- 扩展游戏玩法:可以考虑引入更多元化的游戏玩法,例如任务系统、地图探索等,增加游戏的可玩性和趣味性。
- 用户界面优化:可以考虑引入更友好的用户界面,例如使用图形界面或者增加更多的交互元素,提高用户体验。