如何在不使用全局的情况下在函数之间使用多个变量

How do I go about using many variables between functions without using global?

本文关键字:之间 变量 函数 全局 情况下      更新时间:2023-10-16

利用我在编程课上学到的知识,我正在用C++制作一个基于文本的回合制小型战斗游戏。请记住,我可能不知道如何使用或意识到更复杂的概念。

我在游戏中有很多变量,用于检查玩家是否有特定物品的bools,用于健康、护甲等的int。我知道我不应该使用全局变量,所以我打算使用指针在函数之间传递变量。

通常这不会是个问题:

attack(int *health, int *armor);

但我有一个类似的东西:

attack(int *health, int *armor, int *enemyHealth, int *enemyArmor, ......etc);

键入变得乏味,使用全局变量将是一个即时解决方案。但我想知道另一种方式。此外,有时在调用函数时,我不需要传递"*armor",例如,如果玩家没有穿任何盔甲。这也意味着我需要创建很多这样的:

if (*armor != nullptr)
{
    do things
}

解决方案?如果不清楚,请提前道歉。我是这个网站的新手,可能需要一段时间才能理解你的答案。谢谢大家!!:)

以下是完整的代码(由于我刚刚遇到这个问题,所以这个问题的实例还不多,但查看我的函数调用会让它变得更清楚)

避免传递数百个变量的方法是使用类或结构来组织数据。其想法是将所有相关变量收集在一个"用户定义类型"中,该类型为游戏的某些部分建模。

作为一个例子,我写了一个非常小的冒险游戏,让你看看它是如何工作的:

温蒂的冒险;Bob

#include <string>
#include <iostream>
// this just declares the makeup
// of weapons, it doesn't create any
struct weapon
{
    std::string name;
    int damage;
};
// this just declares the makeup
// of players, it doesn't create any
// NOTE: it contains a weapon
struct player
{
    bool alive;
    std::string name;
    bool male;
    int health;
    int armor;
    weapon weap;
};
// This performs a single attack
void attack(player& a, player& b)
{
    std::cout << "n";
    std::cout << a.name;
    std::cout << " strikes at ";
    std::cout << b.name;
    std::cout << " with " << (a.male?"his":"her") << " ";
    std::cout << a.weap.name;
    int damage = std::rand() % a.weap.damage;
    if(damage == 0)
    {
        std::cout << " and " << (a.male?"he":"she") << " misses.";
        return;
    }
    int wound = damage - b.armor;
    if(wound <= 0)
    {
        std::cout << " causing no harm.";
        return;
    }
    std::cout << " inflicting ";
    std::cout << wound;
    std::cout << " damage";
    b.health -= wound;
    if(b.health < 0)
    {
        b.alive = false;
        std::cout << " killing " << (b.male?"him":"her") << " dead!";
    }
    else if(b.health < 10)
    {
        std::cout << " causing " << (b.male?"him":"her") << " to stumble!";
    }
    else if(b.health < 20)
    {
        std::cout << " causing " << (b.male?"him":"her") << " to stagger!";
    }
    else if(b.health < 30)
    {
        std::cout << " irritating " << (b.male?"him":"her") << " somewhat.";
    }
}
int main()
{
    std::srand(std::time(0));
    std::cout << "Welcome to the Adventures of Wendy & Bobn";
    // We actually create a player here
    player bob;
    // Give it relevant data
    bob.alive = true;
    bob.name = "Bob";
    bob.male = true;
    bob.armor = 4;
    bob.health = 100;
    bob.weap.name = "knife";
    bob.weap.damage = 16;
    // same with another player
    player wendy;
    wendy.alive = true;
    wendy.name = "Wendy";
    wendy.male = false;
    wendy.armor = 3;
    wendy.health = 100;
    wendy.weap.name = "blade";
    wendy.weap.damage = 20;
    // Keep fighting till someone dies!
    while(bob.alive && wendy.alive)
    {
        attack(bob, wendy);
        if(wendy.alive && bob.alive)
            attack(wendy, bob);
    }
}

您可以将变量放入struct中并传递。

在C++中,您会优先使用&通过引用传递,但这在普通的C.中不可用

在C++中,您还可以优先使用class而不是struct,并限制对变量的访问,使它们只能通过类的成员函数更新。这会给你更好的控制。但我离题了–以struct开始!;-)

我同意其他人的观点,并说:是的,你应该看看类。他们之所以这么说是因为一种叫做封装的东西。这个想法是将相关的东西组合在一个类中,并让其他所有东西使用接口与该类交互。

前任。假设你设计了一个玩家角色的游戏,每次玩家被击中时,她都必须减速。所以,你会把健康和速度作为私人变量。这样,任何课外活动都无法改变他们。然后你会有一个叫做"takeHit"(或类似的东西)的函数,这个函数会减少生命值并降低玩家速度。这样,你就不会在不降低速度的情况下意外降低生命值。这使您可以轻松地编写符合您的设计的代码。它还使您更容易思考代码,因为大多数时候,您只关心接口允许您对对象做什么。

更新:哦,我还应该提到,当你在一个类中创建你的玩家时,可以很容易地拥有多个玩家。