简体   繁体   English

如何更新游戏板? C ++

[英]How to update a game board? c++

I'm trying to make a dungeon crawlesque game and I have this code to create a game board. 我正在尝试制作地下城爬行游戏,并且我有这段代码来创建游戏板。 I'm using 'F' as the finish point and 'P' for the player. 我将“ F”用作终点,将“ P”用作玩家。

    void Gameboard::CreateGameboard()
{
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
         GameGrid[i][j] = 'x';
        }
    }
    cout << "  0 1 2 3 4 5 6 7 8 9 10" << endl;
    cout << " +---------------------+" << endl;
    for (int i = 0; i < 10; i++)
    {
        cout << " " << "|" << GameGrid[i][0];
        for (int j = 0; j < 10; j++)
        {
            if (i == Spawn[0] && j == Spawn[0])
            {
                GameGrid[0][0] = 'P';
            }
            cout << " " << GameGrid[i][j];
        }
        cout << "|" << endl;
    }
    cout << " +---------------------+" << endl;
}

The problems I'm facing are. 我面临的问题是。 'P' is being placed in the first two slots of the board and unsure why. “ P”被放置在电路板的前两个插槽中,不确定原因。 And how would I update the board with player movement? 以及我将如何随着玩家的移动来更新董事会? I have a Player class with x,y position variables and my thought is to increment down/up based on where they're going. 我有一个带有x,y位置变量的Player类,我的想法是根据它们的去向递增/递减。 Is it required to reprint the whole board after every movement? 每次移动后是否需要重新打印整个电路板?

With your drawing of the board. 随着您的董事会的图纸。

 for (int i = 0; i < 10; i++)
    {
        cout << " " << "|" << ***GameGrid[i][0]***;
        for (int j = 0; j < 10; j++)
        {
            cout << " " << GameGrid[i][j];
        }
        cout << "|" << endl;
    }

You print out the first item of the row, then print the whole row, including the first item again. 您先打印出该行的第一项,然后再打印整行,包括第一项。 so each row will have a double up of the first item. 因此每一行都将第一项加倍。

As for your second question, clearing the screen is exactly what you'll have to do. 至于第二个问题,清除屏幕正是您要做的。 If you're using windows then you can use system("cls"); 如果您使用的是Windows,则可以使用system(“ cls”);。 to 'clear' the console and then redraw. 以“清除”控制台,然后重新绘制。 I would recommend putting the board drawing and board creation into different functions. 我建议将电路板图和电路板创建成不同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM