【问题标题】:C++ Text-RPG Inventory systemC++ Text-RPG 库存系统
【发布时间】:2015-09-14 14:07:06
【问题描述】:

我正在构建文本-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;   
}

【问题讨论】:

  • 将该信息添加到您的项目结构中...

标签: c++ text mud


【解决方案1】:

有很多可能性可以做到这一点。

简单的方法

首先,您必须保留物品清单,而不是字符串:

vector<Item> Inventory = {Sword, Helmet}; //Player Inventory.

然后您必须使用Inventroy[i].name 搜索并使用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&lt;Item&gt;:

Item Empty{"<Empty>", 0, 0, 0};
vector<Item> Equipment{6, Empty};  //Current equipment: 6 x Empty.
...

您还可以考虑将 Inventory 设为 map&lt;string, Item&gt;,以便通过名称轻松访问

【讨论】:

  • 谢谢。还有一个问题:如何从库存中删除项目?例如,如果玩家想要丢弃物品或装备了物品(如果装备,物品不应该出现在库存中)
  • 好吧,在这种情况下,您可以 erase 库存中使用的元素。根据游戏规则,您可能还希望在为插槽装备不同的物品时将插槽的前一个物品返回到库存中。
  • 我试过 Inventory[i].name.erase() 但它只会从玩家库存中删除项目名称,而不是整个项目。
  • 问题是我无法删除整个对象及其所有数据。
  • 试试Inventory.erase(Inventory.begin()+i);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 2019-01-13
  • 1970-01-01
  • 2013-08-04
  • 2020-10-07
相关资源
最近更新 更多