C++文本RPG库存系统
C++ Text-RPG Inventory system
我正在构建文本rpg库存系统,但我真的不知道如何正确创建装备项目。例如,我可以装备玩家库存中的物品,但我无法确定这是什么类型的物品(剑、盾、手套或其他东西),因为物品应该装备在合适的位置(头上戴头盔,手上拿剑等等)。有什么办法吗?
#include <iostream>
#include <vector>
#include <Windows.h>
#include <string>
using namespace std;
void Red()
{
SetConsoleTextAttribute
(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
} //Intensive red console text color.
void Green()
{
SetConsoleTextAttribute
(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
} //Intensive green console text color.
void Normal()
{
SetConsoleTextAttribute
(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
} //Default console text color.
struct Item{
string name; //Item name.
int price; //Item price.
int purpose; // 0 - Head, 1 - Neck, 2 - Torso, 3 - Hands, 4 - Legs, 5 - Feets.
int attribute; //Attack, Defense...
};
int main()
{
//Isn't the smartest way to do so...
Item Sword{
"Sword", //This item name is 'Short Sword'.
87, //Cost 87 gold pieces.
3, //Use this item with hands. :D.
10 //+10 attack.
};
string input; // input is for player commands.
vector<string> Equipment = { "<Empty>", "<Empty>", "<Empty>", "<Empty>", "<Empty>","<Empty>" }; //Current equipment.
vector<string> Inventory = {Sword.name}; //Player Inventory.
string InventorySlots[] = { "Head", "Neck", "Torso", "Hands", "Legs", "Feets" }; //Player parts where items can be equiped.
while (true){
cin >> input;
if (input == "equipment"){
for (int i = 0; i < 6; i++){
Normal();
cout << InventorySlots[i];
if (Equipment[i] == "<Empty>")
Red();
cout << " " << Equipment[i] << endl << endl;
}
Normal();
}
if (input == "equip"){
cout << "What do you want to equip? ";
cin >> input;
for (int i = 0; i < Inventory.size(); i++){
//Search for item player want to equip and equip it in the right place.
if (input == Inventory[i]){
//Inventory[i] = input;
//But how to identify what kind of item it is?
cout << "Successfully equiped!" << endl;
}
}
}
if(input == "inventory"){
for (int i = 0; i < Inventory.size(); i++){
cout << "______________________________________________________________" << endl;
cout << "| " << Inventory[i] << endl;
cout << "| Carried items " << Inventory.size() << " / " << 20 << endl;
cout << "|_____________________________________________________________" << endl;
}
}
}
system("PAUSE"); // or 'cin.get()'
return 0;
}
这样做有很多可能性。
简单的方法
首先,你必须保留物品的库存,而不是字符串:
vector<Item> Inventory = {Sword, Helmet}; //Player Inventory.
然后你必须使用Inventroy[i].name
搜索crh,并使用inventory[i].purpose
找到正确的插槽:
for(int i = 0; i < Inventory.size(); i++){
//Search for item player want to equip and equip it in the right place.
if(input == Inventory[i].name){
Equipment[Inventory[i].purpose] = Inventory[i].name;
cout << "Successfully equiped!" << endl;
}
}
略有改善
请注意,对于游戏的下一步改进,你的装备也会遇到类似的问题:由于它是一个字符串向量,你无法直接访问物品的属性,例如装备对攻击或防御的影响。
因此,您还应该将Equipment
设置为vector<Item>
:
Item Empty{"<Empty>", 0, 0, 0};
vector<Item> Equipment{6, Empty}; //Current equipment: 6 x Empty.
...
您还可以考虑将Inventory设置为map<string, Item>
,以便通过名称
相关文章:
- C++,系统无法执行指定的程序
- 在UNIX系统中使用DIR查找文件的字节大小
- 错误处理.将系统错误代码映射到泛型
- 当系统的卷被修改时,如何修改WASAPI环回捕获卷
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 在C++游戏中与库存系统作斗争
- 文件系统:复制功能的速度秘诀是什么
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 在gtest.中使用fff.h模拟系统API
- 如何制作无限制照明系统
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 如何传递多个 std::文件系统选项?
- 遍历顺序由 std::文件系统directory_iterator给出
- C++系统找不到指定的文件错误
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- C 基于文本的RPG库存系统
- RPG游戏库存系统
- RPG战斗系统无法正常工作
- C++文本RPG库存系统
- RPG库存系统教程C++