如何从c++的主脚本修改函数中的变量

How do I modify variables in a function from the main script in C++

本文关键字:修改 函数 变量 脚本 c++      更新时间:2023-10-16

我已经看了几个小时了。我正在制作一个小游戏在c++和我试图找出我如何可以从一个函数内部编辑变量。我可以把变量放在main()中并编辑它们,没有问题,但我不知道我需要做什么来编辑一个函数。函数如下:

void HeroSelect(string& hero)
{
    int gold = 20, health = 5, attack = 5, stats[2] = { health, attack };
    string weapon;
    cout << "Please choose your hero.n";
    cin >> hero;
    if (hero == "Warrior" || hero == "warrior")
    {
        weapon = "Broad Sword";
        cout << "You are a Warrior wielding a " << weapon << endl;
        cout << "Good choice.n"
            << "You start out with " << gold << " gold." << endl
            << "You have " << stats[0]<< " health and " << stats[1] << " attack.n";
    }

以上是函数定义,我希望能够在玩家受到攻击或获得更强大武器时编辑生命值。下面是脚本的main()。

void main()
{
    double enter, room1, room2, room3, room4, BossRoom;
    int,   gold = 20; //This is used as part of check inventory and will be subtracted when necessary
    string hero, weapon;

    //Set decimal two places
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    Intro(enter);
    cout << "The first step in this adventure is to choose your hero. You can choose a warrior, a wizard, or an archer.n";
    HeroSelect(hero);

假设他得到了一件新武器,它给了+1攻击我如何在函数中反映它?我会为此创建一个单独的函数吗?

当玩家在游戏中,他们可以随时输入"属性",当他们这样做时,我希望它能够显示他们当前的生命值和攻击,然后基本上只是循环回到房间的开始或他们所处的战斗。

我知道如果我在main()中完成了所有的循环和所有的if-else语句,我可以对变量进行更改,但我想尝试一下,并保持我的主脚本干净。我将stats作为一个数组,因为我认为将数组作为列表打印是很好的。任何帮助都太好了。谢谢!

您可能需要复习一下c++和OOP。你的想法不错,但你可以做得更好。(如果你真的想以一种特定的方式做某件事,并且你能够以这种方式做这件事,那么无论如何都要这样做:P)。

正如你在评论中提到的,游戏设计的重新设计可能是有序的。你会想要建立一个英雄职业,用多个变量或子类作为属性(例如:力量,生命值,武器职业,护甲职业等)。如果你计划拥有多种武器,那么武器类别也是个好主意,例如你可以定义武器属性:攻击伤害,攻击率,武器耐久性等等。

在这些类中,你可以创建"辅助函数",然后你可以用它来改变相关对象的状态。辅助功能可以是允许你增加/减少英雄生命值的公共功能,也可以是增加攻击伤害的辅助功能?

看起来你在学习上做得很好,所以希望你能把这个想法形象化。无论哪种方式,都要坚持下去,并在必要时自由地要求更深入的答案!当你准备好测试时,我很想看看你的游戏!

有几种方法可以处理这个问题。第一种方法是创建一个类,在ejsd1989的注释和答案中有介绍。

第二种选择是通过在main中创建这些变量来使它们成为全局变量,并通过引用将它们传递给每个函数。这使你的主界面像你想要的那样干净。

例如:

void HeroSelect(string& hero, int &gold, int &health)

第三个选项与第二个非常相似,它是将这些变量存储在数组中,然后使用每个函数修改数组。

本质上,它归结为创建一个类或将变量传递给您希望使用的函数。

Main不是一个脚本,在设计上存在一些概念性问题。逐字逐句的回答是:通过引用传递函数参数。但这不是解决这个问题的好办法。请考虑这个例子:

uint menu;
Hero * hero;
Room entrance, next[5];
entrance.Enter();
std::cout << "1st step in this adventure is to choose your hero." <<std::endl;
std::cout << "You can choose " <<std::endl;
std::cout << "(1)       a warrior " << std::endl;
std::cout << "(2) ,     a wizard" << std::endl;
std::cout << "(3) , or an archer." << std::endl;
std::cout << "Please choose your hero." << std::endl;
std::cin >> menu;
switch( menu )
{
    case  3 : { hero = new Archer;  break; }
    case  2 : { hero = new Wizard;  break; }
    default : { hero = new Warrior; break; }
}
hero->Weapon();
hero->Gold();
hero->Health();
hero->Attack();
std::cout << "Which room to enter? 1..5" << std::endl;
std::cin >> menu;
next[menu - 1].Enter();
if( 5 == menu )
{
    std::cout << "You are attacked by a troll!" << std::endl;
    hero->SetHealth( hero->GetHealth() >> 1 );
}
hero->Health();

你可以这样处理英雄类型,房间实例。找到它们可以泛化的属性,并通过成员或继承产生差异。更仔细地设计它,你可以最小化例如大量计数和重复的代码分支。

class Room
{
    static uint cntr;
    std::string name;
public:
    Room() : name ( "Room#" ) { if( cntr < 5 ) { name[ name.size() - 1 ] = cntr + '0';  } else { name = "BossRoom"; }  ++cntr; }
    void Enter() { std::cout << "You have entered " << name << "!" << std::endl; }
};
uint Room::cntr;
struct Limint
{
    uint max;
    uint curr;
    Limint( uint init ) : max ( init ), curr( init ) {}
};
class Hero
{
protected:
    std::string     type;
    uint            gold;
    Limint          hp;
    Limint          attack;
    std::string     weapon;
public:
    Hero() : type( "Hero" ), gold( 20 ), hp( 50 ), attack(5), weapon( "Kitchen knife" ) {}
    uint GetHealth() { return hp.curr; }
    void SetHealth( uint health ) { health = std::min( health, hp.max ); hp.curr = health; }
    void Weapon() { std::cout << "You are a " << type << " wielding a " << weapon << ". Good choice!" << std::endl; }
    void Gold() { std::cout << "You start out with " << gold << " gold." << std::endl; }
    void Health() { std::cout << "You have " << hp.curr << " / " << hp.max << " health." << std::endl; }
    void Attack() { std::cout << "You have " << attack.curr << " / " << attack.max << " attack." << std::endl; }
};
class Warrior : public Hero
{
public:
    Warrior() { type = "Warrior"; weapon = "Broad sword"; }
};
class Wizard : public Hero
{
public:
    Wizard() { type = "Wizard"; weapon = "Magic stick"; }
};
class Archer : public Hero
{
public:
    Archer() { type = "Archer"; weapon = "Crossbow"; }
};

玩得开心!