0

我是一个非常新手的 C++ 编码器,我开始制作一个控制台冒险游戏。我的冒险游戏目前包含一个玩家角色,该角色在 80 个字符宽度 x 40 行的控制台应用程序窗口内四处走动。

我不确定如何为我的游戏存储地图。每张地图将由 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);

我不确定这是否完全是显示字符的正确方法,但我认为只在 for 循环中找出每个字符并不是一个好主意。

所以.. 假设我的控制台窗口(在上面的代码中)是 10 个字符宽和 5 行高。在上面的代码中,我在 Char 中有一个地图,因此在加载每个地图时,我会将每个地图放在自己的数组中。

我正在考虑将整个地图放入单个字符中,但随后仅通过在 for 循环中偏移 x 和 y 来显示我需要的内容。

mapA[x + cWidth * y].Char.AsciiChar = map[y+offset][x+offset];

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

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

有了偏移量,我可以在 5 行上分别显示 '1234567890' 和 5 行上的 'ABCDEFGHIJ'。

所以简而言之,我想知道最有效的方法,我应该有多个字符吗?我应该创建一个类吗?那么我可以将字符存储为颜色吗?(在 C++ 中,类'对我来说仍然是新的)。

我应该只在地图中绘制地形,然后添加对象(房屋、树木)吗?或者只是手动在地图中绘制它?

我想我只是想了太久,需要一点方向谢谢!

4

1 回答 1

2

我会做的方式是创建一个地图

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;
        }
    }

}

然后主要你会有类似的东西......

while(true)
{
     for(int i=0; i<maxheight; i++)
    {
        for(int j=0; j< maxwidth; j++)
        {
            map[i][j]->Display();
        }
    }

}

我希望所有这些示例代码对您有所帮助并回答了您的问题。我没有调试或查找任何语法错误。如果代码中有任何错误,您将不得不修复它们!

祝你好运!

于 2012-04-06T03:33:15.007 回答