大多数(如果不是全部)游戏会记住您拥有的东西(金钱,村庄规模,经理人数等),这样,当您再次开始游戏时,您仍然拥有上次退出游戏时获得的一切。我正在编写一个需要记住值的游戏,并在程序开始时首先声明了它们。但是由于money
等于零,因此每次用户再次玩游戏时,他们的收入都会被重置。
我最终将对大多数变量使用它,但是我正在积极研究Tutorial();
。我将需要本教程仅运行一次,因此我需要该程序在每次程序启动时检查用户是否已完成了本教程。我尝试设置一个名为isTutorialCompleted
的布尔值,并在运行函数时在main中检查它。代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted = false;
void initApp () {
}
void Tutorial() {
cout << "Please Enter your name" << "\n";
cin >> createManager;
createdManagers.push_back(createManager);
cout << "\n" << "You have created Manager " << createManager;
}
int main() {
initApp();
if (isTutorialCompleted == false) {
Tutorial();
}
}
由于每次我重新启动程序时布尔值都会重置,因此这不起作用,因此我尝试使布尔值最初未定义,然后在Tutorial中进行更改。代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted;
void initApp () {
}
void Tutorial() {
isTutorialCompleted = false;
cout << "Please Enter your name" << "\n";
cin >> createManager;
createdManagers.push_back(createManager);
cout << "\n" << "You have created Manager " << createManager;
isTutorialCompleted = true;
}
int main() {
initApp();
Tutorial();
}
此问题是它不会保留该值。 isTutorialCompleted
将返回到程序中未定义的位置,并最终在教程开始时变为false。最后,我尝试检查initApp();
中的布尔值。代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted;
void initApp () {
//Check if tutorial is completed
if (isTutorialCompleted = true) {
Tutorial();
}
}
void Tutorial() {
cout << "Please Enter your name" << "\n";
cin >> createManager;
createdManagers.push_back(createManager);
cout << "\n" << "You have created Manager " << createManager;
isTutorialCompleted = true;
}
int main() {
initApp();
}
因此,最终的问题是:如何永久存储变量,以便在重新启动程序时不会重置存储的变量?
答案 0 :(得分:-3)
每当游戏开始时,就从配置文件存储和导入。