C++;二维块类型? 我在java中使用MeCeFrt2D游戏,我决定用C++来创建同样的游戏来增强C++的能力。但我有个问题。我在Java中有一个块类型枚举,其中包含该块类型的图像位置和硬度(挖掘它需要多长时间)。我发现C++中的枚举不同于java中的枚举。如何在C++中实现这个?< /P>

C++;二维块类型? 我在java中使用MeCeFrt2D游戏,我决定用C++来创建同样的游戏来增强C++的能力。但我有个问题。我在Java中有一个块类型枚举,其中包含该块类型的图像位置和硬度(挖掘它需要多长时间)。我发现C++中的枚举不同于java中的枚举。如何在C++中实现这个?< /P>,c++,enums,C++,Enums,BlockType.java: public enum BlockType { STONE("res/blocks/stone.png",3), COAL("res/blocks/coal.png", 2), AIR("res/blocks/air.png",0), GRASS("res/blocks/grass.png",1), DIRT("res/blocks/dirt.png",1), DIAMOND("res/blocks/diamond.

BlockType.java:

public enum BlockType {
    STONE("res/blocks/stone.png",3),
    COAL("res/blocks/coal.png", 2),
    AIR("res/blocks/air.png",0),
    GRASS("res/blocks/grass.png",1),
    DIRT("res/blocks/dirt.png",1),
    DIAMOND("res/blocks/diamond.png",5),
    REDSTONE("res/blocks/redstone.png",3),
    COBBLE("res/blocks/cobble.png",3),
    BRICK("res/blocks/brick.png",4),
    IRON("res/blocks/iron.png",4),
    GOLD("res/blocks/gold.png",5);
    public final String location;
    public final int hardness;
    BlockType(String location, int hardness){
    this.location = location;
    this.hardness = hardness;
    }
}

C++枚举确实以另一种方式工作

enum eMyEnum
{
    ONE = 15,
    TWO = 22
};
基本上,它们只允许您为INT值创建“名称”

对于您的情况,我将为块名称创建一个枚举:

enum blocks
{
    STONE,
    SAND,
    <...>
};

一种可能性是使用A,由
枚举
值键入,值为:

#包括
#包括
#包括
枚举块类型
{
石
煤,,
黄金
};
std::映射块类型;
BlockTypes[STONE]=std::make_pair(std::string(“res/blocks/STONE.png”),3);
BlockTypes[COAL]=std::make_pair(std::string(“res/blocks/COAL.png”),2);
BlockTypes[GOLD]=std::make_pair(std::string(“res/blocks/GOLD.png”),5);

为什么使用和
std::map
数组将做什么?(可在编译时初始化)

使用名称空间std;
结构块类型{
枚举{
斯通=0,
煤,,
最后
};
块类型(字符串位置,int硬度):位置(位置),硬度(硬度){}
常量字符串位置;
硬度常数;
静态常量块类型块[最后];
};
常量BlockType BlockType::Blocks[]={
块类型(“res/blocks/stone.png”,3),
区块类型(“res/blocks/coal.png”,2)
};
int main(){

cout我将在这里结合这两个答案,并将
enum
映射到block
struct

struct Block
{
  block(path, str) : strength(str), path(path) {}
  int str;
  std::string path;
};

enum BlockType
{
  STONE,
  COAL,
  ETC
}

std::map<BlockType, Block> blocks;
blocks[STONE] = Block("c:/block/bla.png", 1);
blocks[STONE].str; // 1
blocks[STONE].path; // "c:/block/bla.png"
struct块
{
块(路径,str):强度(str),路径(path){}
int-str;
std::字符串路径;
};
枚举块类型
{
石
煤,,
等
}
std::映射块;
块[STONE]=块(“c:/Block/bla.png”,1);
blocks[STONE].str;//1
块[STONE].path;//“c:/block/bla.png”

我将使用类似于Singerofall的答案:

enum blocks
{
    STONE,
    COAL,
    GOLD
};

struct BlockType {
    BlockType(std::string loc, int h): location(loc), hardness(h) {}
    std::string location;
    int hardness;
};

BlockType blockTypes[] = {
  BlockType("res/blocks/stone.png", 3), // STONE
  BlockType("res/blocks/coal.png", 2), // COAL
  BlockType("res/blocks/gold.png", 5) // GOLD
};

// use:
cout << "Location: " << blockTypes[STONE].location << endl;
enum块
{
石
煤,,
黄金
};
结构块类型{
块类型(标准::字符串loc,int h):位置(loc),硬度(h){
std::字符串位置;
硬度;
};
BlockType blockTypes[]={
块类型(“res/blocks/stone.png”,3),//stone
块类型(“res/blocks/coal.png”,2),//coal
BlockType(“res/blocks/gold.png”,5)//gold
};
//使用:

谢谢,我会用这个。但是这里有一个问题,如果你不介意的话:我怎么能用这个文件来使用多个文件?我是说我应该如何在头文件中声明结构或枚举?对不起,我是C++的NoOB,所以如果你回答我的话我会很高兴。
#include <string>
#include <map>
#include <utility>

enum BlockType
{
    STONE,
    COAL,
    GOLD
};

std::map<BlockType, std::pair<std::string, int>> BlockTypes;

BlockTypes[STONE] = std::make_pair(std::string("res/blocks/stone.png"), 3);
BlockTypes[COAL]  = std::make_pair(std::string("res/blocks/coal.png"),  2);
BlockTypes[GOLD]  = std::make_pair(std::string("res/blocks/gold.png"),  5);
using namespace std;

struct BlockType {
    enum {
       STONE = 0,
       COAL,
       LAST
    };
    BlockType(string location, int hardness) : location(location), hardness(hardness) {}
    const string location;
    const int hardness;
    static const BlockType Blocks[LAST];
};

const BlockType BlockType::Blocks[] = {
    BlockType("res/blocks/stone.png", 3),
    BlockType("res/blocks/coal.png", 2)
};

int main() {
    cout << BlockType::Blocks[BlockType::STONE].location << `\n`;
    return 0;
}
struct Block
{
  block(path, str) : strength(str), path(path) {}
  int str;
  std::string path;
};

enum BlockType
{
  STONE,
  COAL,
  ETC
}

std::map<BlockType, Block> blocks;
blocks[STONE] = Block("c:/block/bla.png", 1);
blocks[STONE].str; // 1
blocks[STONE].path; // "c:/block/bla.png"
enum blocks
{
    STONE,
    COAL,
    GOLD
};

struct BlockType {
    BlockType(std::string loc, int h): location(loc), hardness(h) {}
    std::string location;
    int hardness;
};

BlockType blockTypes[] = {
  BlockType("res/blocks/stone.png", 3), // STONE
  BlockType("res/blocks/coal.png", 2), // COAL
  BlockType("res/blocks/gold.png", 5) // GOLD
};

// use:
cout << "Location: " << blockTypes[STONE].location << endl;