一.“打怪小游戏”代码来源
http://blog.csdn.net/zjx120307/article/details/126221342?spm=1001.2014.3001.5506
二.运行环境及运行结果
1.运行环境:Windows11操作系统,C++运行环境
2.运行截图:
(1)功能6演示
(2)功能1演示
(3)功能2演示
(4)功能3演示
(5)功能4演示
原代码:
点击查看代码
#include <iostream>
using namespace std;
double shengmingli = 2000;//定义主角初始生命力
int gongjili = 150;//定义主角初始攻击力
int fangyuli = 200;//定义主角初始防御力
int money = 20;//定义主角初始金钱数量
bool guoguan;//定义是否通关判定
void wuqidian();//定义武器店函数
void yaodian();//定义药店函数
void guaiwu1();//定义小怪物函数
void guaiwu2();//定义大怪物函数
int main()
{
cout << "欢迎你开始玩打怪物小游戏!\n";
cout << "小镇\n";
cout << "一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店" << endl;
cout << "和一家武器店。\n";
int xiaozhen;//定义选择项目
cout << "1.去武器店" << endl;
cout << "2.去药品店" << endl;
cout << "3.去打小怪物" << endl;
cout << "4.去打大怪物" << endl;
cout << "5.退出游戏" << endl;
cout << "6.显示你的状态" << endl;
cin >> xiaozhen;
while (xiaozhen != 5)//输入5时退出游戏
{
if (shengmingli <= 0)//主角生命力小于等于0时游戏结束
{
cout << "你死啦!" << endl;
break;
}
if (guoguan)
{
cout << "恭喜通关!" << endl;
break;
}
if (xiaozhen == 6)//输入6可检测自己的状态
{
cout << "你的生命力:" << shengmingli << endl;
cout << "你的攻击力:" << gongjili << endl;
cout << "你的防御力:" << fangyuli << endl;
cout << "你拥有的钱:" << money << endl;
}
else
switch (xiaozhen)
{
case 1: wuqidian(); break;
case 2: yaodian(); break;
case 3: guaiwu1(); break;
case 4: guaiwu2(); break;
default: cout << "请不要乱选!" << endl; break;
}
cin >> xiaozhen;
}
if (xiaozhen == 5)
{
cout << "正在退出游戏……" << endl;
}
cin.get();
cin.get();
return 0;
}
void wuqidian()
{
cout << "欢迎来到武器店!" << endl;
cout << "1、买小刀(1M加2攻击力)" << endl;
cout << "2、买短剑(2M加20攻击力)" << endl;
cout << "3、买大砍刀(5M加40攻击力)" << endl;
cout << "4、买双节棍(7M加60攻击力)" << endl;
cout << "5、买盾牌(2M加30防御力)" << endl;
cout << "6、买铠甲(5M加60防御力)" << endl;
cout << "7、离开武器店" << endl;
int wuqidian;
cin >> wuqidian;
while (wuqidian != 7)//输入7时结束函数
{
switch (wuqidian)
{
case 1: if (money < 10)
cout << "你的钱不够" << endl;//钱不够时返回Flase
else
cout << "购买成功!" << endl;//钱足够时返回True
gongjili += 2;
money -= 1;
break;
case 2: if (money < 80)
cout << "你的钱不够" << endl;
else
cout << "购买成功!" << endl;
gongjili += 20;
money -= 80;
break;
case 3: if (money < 140)
cout << "你的钱不够" << endl;
else
cout << "购买成功!" << endl;
gongjili += 40;
money -= 140;
break;
case 4: if (money < 200)
cout << "你的钱不够" << endl;
else
cout << "购买成功!" << endl;
gongjili += 60;
money -= 200;
break;
case 5: if (money < 60)
cout << "你的钱不够" << endl;
else
cout << "购买成功!" << endl;
fangyuli += 30;
money -= 60;
break;
fangyuli += 60;
money -= 100;
break;
default: cout << "无" << endl;
break;
}
cin >> wuqidian;
}
if (wuqidian == 7)
{ //返回main()主函数
cout << "欢迎下次再来!" << endl;
cout << "欢迎你开始玩打怪物小游戏!\n";
cout << "小镇\n";
cout << "一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店" << endl;
cout << "和一家武器店。\n";
cout << "1.去武器店" << endl;
cout << "2.去药品店" << endl;
cout << "3.去打小怪物" << endl;
cout << "4.去打大怪物" << endl;
cout << "5.退出游戏" << endl;
cout << "6.显示你的状态" << endl;
}
}
/*
yaodian()的设置与wuqidian()相同,可参照阅读.
*/
void yaodian()
{
cout << "欢迎来到药品店!" << endl;
cout << "1、买1号补血药(10M加200生命)" << endl;
cout << "2、买2号补血药(50M加1000生命力)" << endl;
cout << "3、买3号补血药(100M加2200生命力)" << endl;
cout << "4、离开武器店" << endl;
int yaodian;
cin >> yaodian;
while (yaodian != 4)
{
switch (yaodian)
{
case 1: if (money < 10)
cout << "你的钱不够" << endl;
else
cout << "购买成功!" << endl;
shengmingli += 200;
money -= 10;
break;
case 2: if (money < 50)
cout << "你的钱不够" << endl;
else
cout << "购买成功!" << endl;
shengmingli += 1000;
money -= 50;
break;
case 3: if (money < 100)
cout << "你的钱不够" << endl;
else
cout << "购买成功!" << endl;
shengmingli += 2200;
money -= 100;
break;
default: cout << "无" << endl;
break;
}
cin >> yaodian;
}
if (yaodian == 4)
{
cout << "欢迎下次再来!" << endl;
cout << "欢迎你开始玩打怪物小游戏!\n";
cout << "小镇\n";
cout << "一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店" << endl;
cout << "和一家武器店。\n";
cout << "1.去武器店" << endl;
cout << "2.去药品店" << endl;
cout << "3.去打小怪物" << endl;
cout << "4.去打大怪物" << endl;
cout << "5.退出游戏" << endl;
cout << "6.显示你的状态" << endl;
}
}
/*这里是两个战斗函数,使用指针来处理.避免造成内存崩溃.*/
void guaiwu1()
{
cout << "开始与小怪物战斗!!!" << endl;
double* g_shengmingli = new double;//定义怪物生命
int* g_gongjili = new int;//定义怪物攻击力
int* g_fangyuli = new int;//定义怪物防御力
int* g_money = new int;//定义怪物金钱
*g_shengmingli = 100;
*g_gongjili = 5;
*g_fangyuli = 3;
*g_money = 5;
double* tongji1 = new double;//用来计算主角对怪物的杀伤
double* tongji2 = new double;//用来计算怪物对主角的杀伤
*tongji1 = 0;
*tongji2 = 0;
int* huihe = new int;//定义回合数
*huihe = 1;
cout << "你开始对小怪物进行攻击!" << endl;
int* xuanze = new int;
/*
攻击计算公式
杀伤=攻击力*2-防御力
玩家每回合可以选择攻击与逃跑
*/
while ((*g_shengmingli) > 0 && shengmingli > 0 && (*xuanze) != 2)
{
cout << "现在是" << "第" << *huihe << "回合!" << endl;
cout << "请选择你的动作:\n";
cout << "1、攻击\n2、逃跑\n";
cin >> *xuanze;
switch ((*xuanze))
{
case 1: cout << "你对小怪物发动了攻击!" << endl;
*g_shengmingli -= gongjili * 2 - (*g_fangyuli);
*tongji1 = gongjili * 2 - (*g_fangyuli);
cout << "你打掉了小怪物" << *tongji1 << "的生命!" << endl;
cout << "小怪物还剩" << (*g_shengmingli) - (*tongji1) << "点生命" << endl;
shengmingli -= (*g_gongjili) * 2 - fangyuli;
*tongji2 = (*g_gongjili) * 2 - fangyuli;
cout << "小怪物对你发动了攻击!" << endl;
cout << "小怪物打掉了你" << *tongji2 << "的生命!" << endl;
cout << "你还剩" << shengmingli - (*tongji2) << "点生命" << endl; break;
case 2: cout << "你决定逃跑!" << endl;
cout << "逃跑成功!" << endl; continue;
default: cout << "请不要乱选!" << endl;
}
(*huihe)++;
}
if ((*g_shengmingli) <= 0)
{//杀死怪物后的返回
cout << "小怪物被你杀死了!你真厉害!!!" << endl;
money += (*g_money);
cout << "欢迎你开始玩打怪物小游戏!\n";
cout << "小镇\n";
cout << "一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店" << endl;
cout << "和一家武器店。\n";
cout << "1.去武器店" << endl;
cout << "2.去药品店" << endl;
cout << "3.去打小怪物" << endl;
cout << "4.去打大怪物" << endl;
cout << "5.退出游戏" << endl;
cout << "6.显示你的状态" << endl;
}
else
if (shengmingli <= 0)
{//被怪物杀死后的返回
cout << "你被小怪物杀死了!游戏结束!!!" << endl;
}
else
if ((*xuanze) == 2)
{//逃跑的返回
cout << "你逃回了小镇!" << endl;
cout << "欢迎你开始玩打怪物小游戏!\n";
cout << "小镇\n";
cout << "一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店" << endl;
cout << "和一家武器店。\n";
cout << "1.去武器店" << endl;
cout << "2.去药品店" << endl;
cout << "3.去打小怪物" << endl;
cout << "4.去打大怪物" << endl;
cout << "5.退出游戏" << endl;
cout << "6.显示你的状态" << endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete g_money;
delete tongji1;
delete tongji2;
}
/*
设置均与void函数guaiwu1()相同,可参照上例阅读.
*/
void guaiwu2()
{
cout << "开始与大怪物战斗!!!" << endl;
double* g_shengmingli = new double;
int* g_gongjili = new int;
int* g_fangyuli = new int;
*g_shengmingli = 3600;
*g_gongjili = 500;
*g_fangyuli = 500;
double* tongji1 = new double;
double* tongji2 = new double;
*tongji1 = 0;
*tongji2 = 0;
int* huihe = new int;
*huihe = 1;
cout << "你开始对大怪物进行攻击!" << endl;
int* xuanze = new int;
while ((*g_shengmingli) > 0 && shengmingli > 0 && (*xuanze) != 2)
{
cout << "现在是" << "第" << *huihe << "回合!" << endl;
cout << "请选择你的动作:\n";
cout << "1、攻击\n2、逃跑\n";
cin >> *xuanze;
switch ((*xuanze))
{
case 1: cout << "你对大怪物发动了攻击!" << endl;
*g_shengmingli -= gongjili * 2 - (*g_fangyuli);
*tongji1 = gongjili * 2 - (*g_fangyuli);
cout << "你打掉了大怪物" << *tongji1 << "的生命!" << endl;
cout << "大怪物还剩" << (*g_shengmingli) - (*tongji1) << "点生命" << endl;
shengmingli -= (*g_gongjili) * 2 - fangyuli;
*tongji2 = (*g_gongjili) * 2 - fangyuli;
cout << "大怪物对你发动了攻击!" << endl;
cout << "大怪物打掉了你" << *tongji2 << "的生命!" << endl;
cout << "你还剩" << shengmingli - (*tongji2) << "点生命" << endl; break;
case 2: cout << "你决定逃跑!" << endl;
cout << "逃跑成功!" << endl; continue;
default: cout << "请不要乱选!" << endl;
}
(*huihe)++;
}
if ((*g_shengmingli) <= 0)
{
cout << "大怪物被你杀死了!你真厉害!!!" << endl;
guoguan = true;
cout << "欢迎你开始玩打怪物小游戏!\n";
cout << "小镇\n";
cout << "一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店" << endl;
cout << "和一家武器店。\n";
cout << "1.去武器店" << endl;
cout << "2.去药品店" << endl;
cout << "3.去打小怪物" << endl;
cout << "4.去打大怪物" << endl;
cout << "5.退出游戏" << endl;
cout << "6.显示你的状态" << endl;
}
else
if (shengmingli <= 0)
{
cout << "你被大怪物杀死了!游戏结束!!!" << endl;
}
else
if ((*xuanze) == 2)
{
cout << "你逃回了小镇!" << endl;
cout << "欢迎你开始玩打怪物小游戏!\n";
cout << "小镇\n";
cout << "一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店" << endl;
cout << "和一家武器店。\n";
cout << "1.去武器店" << endl;
cout << "2.去药品店" << endl;
cout << "3.去打小怪物" << endl;
cout << "4.去打大怪物" << endl;
cout << "5.退出游戏" << endl;
cout << "6.显示你的状态" << endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete tongji1;
delete tongji2;
}
## 三.使用体验问题及调整方案
1.首先宏观上交互体验不佳,界面设置方面过于拥挤。
2.首尾闭环不够清晰,当成功攻击怪物后直接显示结束游戏(功能3.4)强制结束玩家的游戏进程,应该设置为返回游戏主页,让用户自主选择是否继续游戏。
3.游戏内容设置不够完整,整个游戏机制是基于玩家的金钱,生命力,攻击力足够支撑损耗的情况,除了初始化赋初值之外,整个过程中没有其他赚取金钱,生命力的方式。玩家生命力耗尽之后只能通过重开游戏,这让玩家在本游戏中的体验缺少连贯性。应该在多加一些功能用于赚钱金钱和生命力,例如增加“打工赚钱”功能获得金钱,“购买武林秘籍”功能提高攻击力,不仅提升游戏完整性,也增强玩家体验感。
4.逻辑方面存在错误处理:在各个函数中,应该考虑一些错误处理,比如当金钱不够时,应该提示用户而不是静默失败。
5.代码方面重复过多,展示菜单的代码在“武器店”“药品店”函数中重复出现,可以将其定义为函数适时调用。
四.重构测试截图
1.功能调整:增加“打工赚钱” “购买武功秘籍”两个功能函数,代码与运行结果
“打工赚钱功能,点击查看代码”
void dagong() {
cout << "开始打工赚钱!" << endl;
// 实现打工赚钱功能
int workType; // 工作类型
int workTime; // 工作时间
int earnedMoney; // 赚得的钱
cout << "请选择工作类型:" << endl;
cout << "1. 搬粮食(每小时1元)" << endl;
cout << "2. 客栈小二(每小时2元)" << endl;
cout << "3. 胡同杂技表演(每小时3元)" << endl;
cout << "4. 跑腿送货(每小时2元)" << endl;
cin >> workType;
cout << "请输入你要工作的小时数:";
cin >> workTime;
switch (workType)
{
case 1:
earnedMoney = workTime * 1;
break;
case 2:
earnedMoney = workTime * 2;
break;
case 3:
earnedMoney = workTime * 3;
break;
case 4:
earnedMoney = workTime * 2;
break;
default:
earnedMoney = 0;
break;
}
money += earnedMoney;
cout << "你工作了 " << workTime << " 小时,赚了 " << earnedMoney << " 元钱!" << endl;
cout << "现在你总共有 " << money << " 元钱。" << endl;
//zhuye();
}
购买武功秘籍功能,点击查看代码
void buyMartialArt() {
cout << "欢迎来到武功秘籍店!" << endl;
// 实现购买武功秘籍功能
int martialArt; // 武功秘籍选择
cout << "请选择要购买的武功秘籍:" << endl;
cout << "1. 葵花宝典(2元,加20点攻击力)" << endl;
cout << "2. 独孤九剑(3元,加25点攻击力)" << endl;
cout << "3. 凌波微步(4元,加30点攻击力)" << endl;
cout << "4. 北冥神功(5元,加35点攻击力)" << endl;
cin >> martialArt;
int cost = 0;
int gongjili = 0;
switch (martialArt)
{
case 1:
cost = 2;
gongjili = 20;
break;
case 2:
cost = 3;
gongjili = 25;
break;
case 3:
cost = 4;
gongjili = 30;
break;
case 4:
cost = 5;
gongjili = 35;
break;
default:
break;
}
if (money >= cost)
{
money -= cost;
gongjili += gongjili;
cout << "你购买了武功秘籍,当前攻击力为:" << gongjili << endl;
cout << "钱数剩余:" << money << endl;
}
else
{
cout << "金钱不足,无法购买武功秘籍。" << endl;
}
}
2.将菜单页面代码定义为函数
点击查看代码
void zhuye() {
cout << "欢迎下次再来!" << endl;
// cout << "欢迎你开始玩打怪物小游戏!\n";
//cout << "小镇\n";
//cout << "一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店" << endl;
//cout << "和一家武器店。\n";
cout << " " << endl;
cout << " " << endl;
cout << "******小镇*******" << endl;
cout << "1.去武器店" << endl;
cout << "2.去药品店" << endl;
cout << "3.去打小怪物" << endl;
cout << "4.去打大怪物" << endl;
cout << "5.退出游戏" << endl;
cout << "6.显示你的状态" << endl;
cout << "7. 打工赚钱" << endl; // 增加选项
cout << "8. 购买武功秘籍" << endl; // 增加选项
cout << "————————————————————" << endl; // 增加选项
cout << " " << endl;
cout << " " << endl;
}