如何在函数之间使用多个变量而不使用全局变量? 使用我在编程类中学习的内容,我在C++中制作了一个基于文本的小回合回合战斗游戏。请记住,我可能不知道如何使用或了解更复杂的概念
我在游戏中有很多变量,bools用于检查玩家是否拥有某些物品,int表示生命值、护甲等。我知道我不应该使用全局变量,所以我打算使用指针在函数之间传递变量 通常这不会是一个问题:如何在函数之间使用多个变量而不使用全局变量? 使用我在编程类中学习的内容,我在C++中制作了一个基于文本的小回合回合战斗游戏。请记住,我可能不知道如何使用或了解更复杂的概念,c++,variables,pointers,global,C++,Variables,Pointers,Global,我在游戏中有很多变量,bools用于检查玩家是否拥有某些物品,int表示生命值、护甲等。我知道我不应该使用全局变量,所以我打算使用指针在函数之间传递变量 通常这不会是一个问题: attack(int *health, int *armor); 但我有类似的东西: attack(int *health, int *armor, int *enemyHealth, int *enemyArmor, ......etc); 打字变得单调乏味,使用全局变量将是一个即时解决方案。但我想知道另一种方法。
attack(int *health, int *armor);
但我有类似的东西:
attack(int *health, int *armor, int *enemyHealth, int *enemyArmor, ......etc);
打字变得单调乏味,使用全局变量将是一个即时解决方案。但我想知道另一种方法。此外,有时在调用函数时,我不需要传递“*盔甲”,例如,如果玩家没有佩戴盔甲。这也意味着我需要创建很多这样的内容:
if (*armor != nullptr)
{
do things
}
解决方案?如果不清楚,请提前道歉。我是这个网站的新手,可能需要一段时间才能理解你的答案。
谢谢大家!!:)
是完整的代码(这个问题的实例还不多,因为我刚刚遇到了它,但是查看我的函数调用会让它变得更清晰)您可以将变量放入结构中并传递它 <>在C++中,您最好通过引用使用,使用<代码>和<代码>,但是在C.</P>中不可用。
C++中,你也优先使用<代码>类< /C> >代替
!;-)开始)struct
更新:哦,我还应该提到,当你在一个类中创建你的播放器时,可以很容易地拥有多个播放器。避免传递数百个变量的方法是使用类或结构来组织你的数据。其思想是将所有相关变量收集在一个“用户定义类型”中,以模拟游戏的某些部分 作为一个例子,我为您编写了一个非常小的冒险游戏,让您了解这是如何工作的: 温迪和鲍勃历险记
#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 & Bob\n";
// 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);
}
}
#包括
#包括
//这只是宣布化妆
//对于武器,它不会产生任何影响
结构武器
{
std::字符串名;
智力损伤;
};
//这只是宣布化妆
//对于玩家来说,它不会产生任何影响
//注意:它包含一个武器
结构播放器
{
布尔活着;
std::字符串名;
布尔男性;
国际卫生组织;
内部装甲;
武器武器;
};
//这将执行一次攻击
无效攻击(玩家a、玩家b)
{
STD:我想现在是时候学习类/结构。拿起一本C++书。你用一个结构,用指针传递它。或者你用参考而不是指针,如果你在C++中。这似乎是普遍的共识:哈哈,我们实际上是在第一次考试之后学习那些。我现在就把那本书打开,然后拿一本。广告开始,谢谢人类!