控制台游戏地图是巨字符还是多字符? 我是一个新手C++程序员,我开始制作控制台冒险游戏。 我的冒险游戏目前由一个玩家角色组成,该角色在控制台应用程序窗口内走动,宽度为80个字符,行数为40

控制台游戏地图是巨字符还是多字符? 我是一个新手C++程序员,我开始制作控制台冒险游戏。 我的冒险游戏目前由一个玩家角色组成,该角色在控制台应用程序窗口内走动,宽度为80个字符,行数为40,c++,multidimensional-array,char,console-application,C++,Multidimensional Array,Char,Console Application,我不知道如何为我的游戏存储地图。每个地图将由80 x 40 ASCII字符和颜色属性组成 我是否应该将每个80 x 40地图存储在其自己的字符中?所以一张地图看起来像 int cHeight = 5; // Reduced size for this example int cHeight = 10; // Reduced size for this example // Set up the characters: char map[cHeight][cWidth+1] = {

我不知道如何为我的游戏存储地图。每个地图将由80 x 40 ASCII字符和颜色属性组成

我是否应该将每个80 x 40地图存储在其自己的字符中?所以一张地图看起来像

int cHeight = 5; // Reduced size for this example
int cHeight = 10; // Reduced size for this example  

// Set up the characters:

char map[cHeight][cWidth+1] = {
    "1234567890",
    "1234567890",
    "1234567890",
    "1234567890",
    "1234567890",
};

CHAR_INFO mapA[cWidth * cHeight];

for (int y = 0; y < cHeight; ++y) {                                                                 
    for (int x = 0; x < cWidth; ++x) {                                                                                                                                                                              
        mapA[x + cWidth * y].Char.AsciiChar = map[y][x];                        
        mapA[x + cWidth * y].Attributes = FOREGROUND_BLUE | Black; //I have an enum setup with background colours.          
    }
}

// Set up the positions:
COORD charBufSize = {cWidth,cHeight};
COORD characterPos = {0,0};
SMALL_RECT writeArea = {0,0,cWidth-1,cHeight-1}; 

// Write the characters:
WriteConsoleOutputA(wHnd, mapA, charBufSize, characterPos, &writeArea);
while(true)
{
     for(int i=0; i<maxheight; i++)
    {
        for(int j=0; j< maxwidth; j++)
        {
            map[i][j]->Display();
        }
    }

}
所以地图看起来更像这样

char map[cHeight][cWidth+1] = {
    "1234567890ABCDEFGHIJ",
    "1234567890ABCDEFGHIJ",
    "1234567890ABCDEFGHIJ",
    "1234567890ABCDEFGHIJ",
    "1234567890ABCDEFGHIJ",
};
使用偏移量,我可以在5行上分别显示“1234567890”和“ABCDEFGHIJ”

总之,我想知道最有效的方法,我应该有多个字符吗?我应该创建一个类吗?然后我可以把字符和颜色储存起来?(在c++中,“类”对我来说仍然是新的)

我是否应该只在地图中绘制地形,然后添加对象(房屋、树木)? 或者只是在地图上手工画出来

我想我只是想得太久了,需要一些指导
谢谢

我的方法是创建一个

Node* map[height][width]
这意味着您将创建指向Node*元素的映射,您可以将Node*元素定义为

class Node{
char displayCharacter;
int posx,poxy
unsigned int r;  //red
unsigned int g;  //green
unsigned int b;  //blue
unsigned int a;  //alpha
display(); // This function will know how to display a node using the colour etc
};
然后你可以,例如,如果你想创建一个房子,你可以给它模型的中心点等等。。。引出一个函数

void createHouse(Node* center)
{
    if((center->posh > 0)&&(center->posh< maxheight))
    {
        if(map[center->posy-1][center->posx]!=NULL)
        {
             map[center->posy-1][center->posx]->displayCharacter = '_';
             map[center->posy-1][center->posx]->r = 255;
        }
    }

}
void createHouse(节点*中心)
{
如果((中心->豪华>0)和(&(中心->豪华<最大高度))
{
if(map[center->posy-1][center->posx]!=NULL)
{
map[center->posy-1][center->posx]->displayCharacter='';
图[center->posy-1][center->posx]->r=255;
}
}
}
那么大体上你会有一些像

int cHeight = 5; // Reduced size for this example
int cHeight = 10; // Reduced size for this example  

// Set up the characters:

char map[cHeight][cWidth+1] = {
    "1234567890",
    "1234567890",
    "1234567890",
    "1234567890",
    "1234567890",
};

CHAR_INFO mapA[cWidth * cHeight];

for (int y = 0; y < cHeight; ++y) {                                                                 
    for (int x = 0; x < cWidth; ++x) {                                                                                                                                                                              
        mapA[x + cWidth * y].Char.AsciiChar = map[y][x];                        
        mapA[x + cWidth * y].Attributes = FOREGROUND_BLUE | Black; //I have an enum setup with background colours.          
    }
}

// Set up the positions:
COORD charBufSize = {cWidth,cHeight};
COORD characterPos = {0,0};
SMALL_RECT writeArea = {0,0,cWidth-1,cHeight-1}; 

// Write the characters:
WriteConsoleOutputA(wHnd, mapA, charBufSize, characterPos, &writeArea);
while(true)
{
     for(int i=0; i<maxheight; i++)
    {
        for(int j=0; j< maxwidth; j++)
        {
            map[i][j]->Display();
        }
    }

}
while(true)
{
对于(int i=0;iDisplay();
}
}
}
我希望所有这些示例代码对您有所帮助,并回答了您的问题。我没有调试或查找任何语法错误。如果代码中有任何错误,您将不得不修复它们


祝你好运!

这个问题最好是在中问!我甚至不知道它的存在。谢谢。我认为你更大的问题是你用C(或下一个最好的过程语言)思考太多。因此,如果这个练习应该是有教育意义的,我建议你尝试做a)不带数组b)不使用原始指针,c)不带关键字
for
while
。这是可能的(虽然可能稍微不那么有效,但是在你的应用程序中不应该伤害),它会强迫你使用和学习一些C++结构和库。我在学校里学习,只在第5周学习,所以我们还没有真正触及到很多。但我很享受,所以我要做这个小项目。我不知道如何完成你说的任何事情。a) 我将如何存储数据?b) 你说的原始指针是什么意思?c) 我不知道从哪里开始:)任何一个像样的地方我都能读到这些东西?这当然给了我一些方向,我很可能会采取这种方法。是否值得在单独的节点/地图中为世界地图创建每个屏幕(80x40)或为整个世界创建一个节点/地图并仅显示相关部分?最好的选择是在程序上开始创建世界地图,然后在移动时仅显示世界的相关部分。例如,获得玩家的位置,+-10上下是你应该渲染/绘制/显示的。如果这回答了您的问题,请选择问题旁边的勾选选项。上面写着1 Hanks Chris我来试试。我有点困惑,如果map是一个未初始化的局部变量,我该如何使用map[h][w]?我想我误解了你的密码。