C++ 基于文本的冒险游戏
我正在监督一个科技夏令营,其中一个夏令营的人为一个基于文本的视频游戏创建了一些代码,但他无法显示结果。虽然程序编译和运行正确,但当选择“治疗”时,它不会增加玩家的健康,当用户选择“攻击”时,我们也会得到零。我在编程方面的知识有限,我正在尽我所能帮助他,使他在这里的经历将是愉快和充实的。如果您能提供任何帮助或建议,我们将不胜感激。代码如下:C++ 基于文本的冒险游戏,c++,class,game-engine,adventure,C++,Class,Game Engine,Adventure,我正在监督一个科技夏令营,其中一个夏令营的人为一个基于文本的视频游戏创建了一些代码,但他无法显示结果。虽然程序编译和运行正确,但当选择“治疗”时,它不会增加玩家的健康,当用户选择“攻击”时,我们也会得到零。我在编程方面的知识有限,我正在尽我所能帮助他,使他在这里的经历将是愉快和充实的。如果您能提供任何帮助或建议,我们将不胜感激。代码如下: // Test for hard stuff.cpp : Defines the entry point for the console applicatio
// Test for hard stuff.cpp : Defines the entry point for the console application.
//
// Bigger proj
// Constructors will make characters with rolling statistics
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// declaring function for hit power
//int power( int str, int def);
int command;
class character
{
public:
character();
//~character();
string name;
float str;
float def;
float health; // hit points
float regen; // health regen amount
float roll; // for random value
float ouch; // amount of attack damage
float getAttack(void);
float getHeal(void);
void setRegen(float reg);
//void setHeal(float healAmt);
private:
};
character::character()
{
srand(time_t(NULL));
str = rand() % 30 + 5;
def = rand() % 30 + 5;
health = 100;
//Output to check the constructor is running properly
cout<< "Character has been created.\n";
}
void character::setRegen( float reg )
{
regen = reg;
}
float character::getAttack()
{
//defines the magnitude/power of attack
//function shows how much damage is inflicted
// ouch is how much damage is done
roll = rand() % 20 + 1; // range between 1 &20
if (roll <= 11)
{
ouch = str - (def /2);
}
else if ((roll <= 17) && (roll >= 12))
{
ouch = (str * 2) - (def / 2);
}
else if ((roll <= 20) && (roll >= 18))
{
ouch = (str * 3) - (def / 2);
//cout << "CRITICAL HIT!!";
}
return ouch;
}
float character::getHeal()
{
//this is what happens when you chose to heal
regen = rand() % 20 + 3;
cout << "regen value= " << regen<< ".\n";
return regen;
}
/*character::~character()
{
str = 0;
def = 0;
health = 0;
// Output to check the destructor is running properly
cout << "Character has been destroyed\n";
} */
int _tmain(int argc, _TCHAR* argv[])
{
//Class objects
character user, computer;
//Hard code in a name for the computer's player
computer.name = "ZOID\n";
float attackDamage;
float healthAdded;
user.setRegen(void);
//Recieve data for the user's player
cout<< "Please enter a name for your character:\n";
cin>> user.name;
//Output name and stats to the user
cout<< "\nYour name is: " << user.name << endl;
cout << "here are your statistics: \n"
<< "strength: " << user.str << endl
<< "Defense: " << user.def << endl
<< "Health: " << user.health << endl;
cout<< "oh no an oppenent appeared!!!\n";
cout<< "you will have to fight him!" << endl<< endl;
cout << "opponent's health: 100" << endl
<< "what would you like to do: heal (1), attack(2), or run(3).\n";
cin>> command;
switch(command)
{
case 1 :
healthAdded = user.getHeal();
cout<< ""<<user.name <<" has regenerated " << healthAdded << " health.\n";
break;
case 2 :
attackDamage = user.getAttack();
cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";
break;
case 3:
cout<< ""<<user.name<<" got away!\n";
break;
default:
cout<< "Please enter a valid choice!";
} //end switch
return 0;
}
//Test for hard stuff.cpp:定义控制台应用程序的入口点。
//
//大项目
//构造函数将使用滚动统计信息生成字符
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//为命中能力声明函数
//int电源(int str、int def);
int命令;
类字符
{
公众:
字符();
//~character();
字符串名;
浮动str;
浮动def;
浮动生命值;//生命值
浮动再生;//健康再生量
浮动滚动;//用于随机值
float-ouch;//攻击伤害量
浮动攻击(无效);
浮动getHeal(无效);
无效设置重新生成(浮动寄存器);
//void setHeal(浮动healAmt);
私人:
};
character::character()
{
srand(time_t(NULL));
str=rand()%30+5;
def=rand()%30+5;
健康=100;
//用于检查构造函数是否正常运行的输出
cout一般来说,处理此类问题的方法之一是逐行检查发生了什么,并确定每一行都做了什么。有时会很长(实际上是很多次),但也能很好地确保您不会遗漏任何内容。在这种特殊情况下,让我们来看看修复问题
当您输入switch语句并点击案例1(治疗)时,代码要做的第一件事是将user.getHeal()的结果分配给healthAdded。您在这里要做的是“进入”getHeal()并查看它的功能。getHeal()获取一个regen编号,并将其分配给regen。然后它打印regen,最后返回存储在regen中的值
现在我们知道了getHeal()的作用,我们可以回到案例1:并完整地说明第一行的作用。它接受getHeal()中内置的regen值并将其分配给healthAdded
案例1:然后在break;语句之前打印healthAdded中的值。break;完成案例1
因此,您的代码在快速列表表单中所做的是:
- 生成治疗值
- 打印两次
您想要做的是根据regen值修改用户的运行状况,因此缺少一个步骤:使用在getHeal()中生成的regen编号更改user.health值
攻击伤害的问题与此类似,请尝试将您希望代码在目标方面做的事情与您看到的代码实际做的事情进行比较。我将尽力一次提供帮助。我的行号可能与您的行号略有不同,所以请随意四处看看
在:
setRegen
被声明为采用float
:
20 class character
21 {
22 public:
.
.
.
34 void setRegen(float reg);
<> >您不能通过Value>代码>。顺便说一下,在C++中,调用一个不带参数的函数时,通常不传递任何内容,而不是传递一个显式的<代码>空>代码>。但是,显式<代码>无效>代码>可以。
getHeal()
函数计算一个随机数量来治疗角色,但它实际上不会增加health
成员变量。您可以通过这种方式实现治疗,请参见第92行:
87 float character::getHeal()
88 {
89 //this is what happens when you chose to heal
90 regen = rand() % 20 + 3;
91 cout << "regen value= " << regen<< ".\n";
92 health += regen;
93 return regen;
94 } Z
您还需要更改getAttack()
的声明(原型):
…以及在main()中如何调用它:
告诉他们在程序开始时只使用一次srand
。user.getHeal
对运行状况没有任何影响,您也不会使用它。我不知道运行状况会如何变化。只提供regen,而regen尚未连接到运行状况。基于回合的迭代在哪里?regen应该在那里工作+感谢您帮助孩子们,鼓励他们以一种有趣的方式学习编程。计算机科学需要更多像您这样的人。非常感谢您的帮助@John Dibling,我希望您能看到,当我们实施了您的更改并添加了一些东西以使其工作时,露营者是多么欣喜若狂。这一切都是值得的!太棒了。告诉我我对他的代码印象深刻的露营者。他可能在编程方面有一个真正的未来!
87 float character::getHeal()
88 {
89 //this is what happens when you chose to heal
90 regen = rand() % 20 + 3;
91 cout << "regen value= " << regen<< ".\n";
92 health += regen;
93 return regen;
94 } Z
58 float character::getAttack(character& opponent)
59 {
60 //defines the magnitude/power of attack
61 //function shows how much damage is inflicted
62
63
64 // ouch is how much damage is done
65 roll = rand() % 20 + 1; // range between 1 &20
66
67 if (roll <= 11)
68 {
69 ouch = str - (def /2);
70 }
71
72 else if ((roll <= 17) && (roll >= 12))
73 {
74 ouch = (str * 2) - (def / 2);
75 }
76
77 else if ((roll <= 20) && (roll >= 18))
78 {
79 ouch = (str * 3) - (def / 2);
80 //cout << "CRITICAL HIT!!";
81 }
82
83 opponent.health -= ouch;
84
85 return ouch;
86
87 }
20 class character
21 {
22 public:
.
.
.
32 float getAttack(character& opponent);
152 case 2 :
153
154 attackDamage = user.getAttack(computer);
155
156 cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";
157
158 break;
// Test for hard stuff.cpp : Defines the entry point for the console application.
//
// Bigger proj
// Constructors will make characters with rolling statistics
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// declaring function for hit power
//int power( int str, int def);
int command;
class character
{
public:
character();
//~character();
string name;
float str;
float def;
float health; // hit points
float regen; // health regen amount
float roll; // for random value
float ouch; // amount of attack damage
float getAttack(character& opponent);
float getHeal(void);
void setRegen(float reg);
bool IsAlive() const;
//void setHeal(float healAmt);
private:
};
character::character()
{
str = rand() % 30 + 5;
def = rand() % 30 + 5;
health = 100;
//Output to check the constructor is running properly
cout<< "Character has been created.\n";
}
bool character::IsAlive() const
{
return health > 0.0f;
}
void character::setRegen( float reg )
{
regen = reg;
}
float character::getAttack(character& opponent)
{
//defines the magnitude/power of attack
//function shows how much damage is inflicted
// ouch is how much damage is done
roll = rand() % 20 + 1; // range between 1 &20
if (roll <= 11)
{
ouch = str - (def /2);
}
else if ((roll <= 17) && (roll >= 12))
{
ouch = (str * 2) - (def / 2);
}
else if ((roll <= 20) && (roll >= 18))
{
ouch = (str * 3) - (def / 2);
//cout << "CRITICAL HIT!!";
}
opponent.health -= ouch;
return ouch;
}
float character::getHeal()
{
//this is what happens when you chose to heal
regen = rand() % 20 + 3;
cout << "regen value= " << regen<< ".\n";
health += regen;
return regen;
}
/*character::~character()
{
str = 0;
def = 0;
health = 0;
// Output to check the destructor is running properly
cout << "Character has been destroyed\n";
} */
int main()
{
srand(time_t(NULL));
//Class objects
character user, computer;
//Hard code in a name for the computer's player
computer.name = "ZOID\n";
float attackDamage;
float healthAdded;
user.setRegen(42.0);
//Recieve data for the user's player
cout<< "Please enter a name for your character:\n";
cin>> user.name;
//Output name and stats to the user
cout<< "\nYour name is: " << user.name << endl;
cout << "here are your statistics: \n"
<< "strength: " << user.str << endl
<< "Defense: " << user.def << endl
<< "Health: " << user.health << endl;
cout<< "oh no an oppenent appeared!!!\n";
cout<< "you will have to fight him!" << endl<< endl;
cout << "opponent's health: 100" << endl;
while (user.IsAlive() && computer.IsAlive())
{
cout << "Str: " << user.str << "\t"
<< "Def: " << user.def << "\t"
<< "Health: " << user.health << "\t"
<< "\n";
cout << "what would you like to do: heal (1), attack(2), or run(3).\n";
cin>> command;
switch(command)
{
case 1 :
healthAdded = user.getHeal();
cout<< ""<<user.name <<" has regenerated " << healthAdded << " health.\n";
break;
case 2 :
attackDamage = user.getAttack(computer);
cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";
break;
case 3:
cout<< ""<<user.name<<" got away!\n";
break;
default:
cout<< "Please enter a valid choice!";
} //end switch
}
return 0;
}