1

我想为我正在制作的 40x20 字符块制作动画cout。我想清除控制台,system("cls");然后立即出现下一个字符块。目前,下一个区块是打字机风格。

对我的问题最简单的答案就是一次拥有一个 20 行 x 40 个字符的 oss 流 cout,而不是打字机风格。

主要.cpp:

    mazeCreator.cout();
    Sleep(5000);
    system("cls");

cout()

void MazeCreator::cout() {
    char wallChar = (char) 219;
    char pavedChar = (char) 176;
    char lightChar = ' ';
    char startChar = 'S';
    char finishChar = 'F';
    char errorChar = '!';
    char removedWallChar = 'R';
    char landmarkLocationChar = 'L';

    ostringstream oss;
    for (int row = 0; row < rows; row++) {
        oss << " ";
        for (int col = 0; col < columns; col++) {
            if (mazeArray[row][col] == wall)
                oss << wallChar;
            else if (mazeArray[row][col] == paved)
                oss << pavedChar;
            else if (mazeArray[row][col] == light)
                oss << lightChar;
            else if (mazeArray[row][col] == start)
                oss << startChar;
            else if (mazeArray[row][col] == finish)
                oss << finishChar;
            else if (mazeArray[row][col] == removedWall)
                oss << removedWallChar;
            else if (mazeArray[row][col] == landmarkLocation)
                oss << landmarkLocationChar;
            else
                oss << errorChar;
        }
        oss << "\n";
    }
    oss << "\n\n";

    cout << oss.str();
}
4

2 回答 2

2

您可以在代码中维护两个二维数组,一个包含屏幕上的当前字符块(我们称之为它cur),一个包含下一个块(我们称之为它next)。

假设cur存储现在屏幕上的块。通过写入next数组来设置下一个块。当您准备好将其放在屏幕上时,请同时循环播放,并且cur针对它们不同的字符,使用跳转到该位置并写入新字符。nextSetConsoleCursorPosition

完成后,复制nextinto的内容cur并继续下一个块。

更新:这是一个例子:

class console_buffer
{
public:
    console_buffer(int rows, int columns) 
                   // start out with spaces
                 : cur(rows, vector<char>(columns, ' ')), 
                   next(rows, vector<char>(columns, ' '))
    {
    }

    void sync()
    {
        // Loop over all positions
        for (int row = 0; row < cur.size(); ++row)
            for (int col = 0; col < cur[row].size(); ++col)

                // If the character at this position has changed
                if (cur[row][col] != next[row][col])
                {
                    // Move cursor to position
                    COORD c = {row, col};
                    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);

                    // Overwrite character
                    cout.put(next[row][col]);
                }

         // 'next' is the new 'cur'
         cur = next;
    }

    void put(char c, int row, int col)
    {
        next[row][col] = c;
    }
private:
    vector<vector<char> > cur;
    vector<vector<char> > next;
};

...

int main()
{
    console_buffer buf(40, 20);

    // set up first block
    ... some calls to buf.put() ...

    // make first block appear on screen
    buf.sync();

    // set up next block
    ... some calls to buf.put()

    // make next block appear on screen
    buf.sync();

    // etc.
}
于 2012-05-07T04:27:07.223 回答
0

您可以使用CreateConsoleScreenBuffer实现双缓冲。这些方面的东西应该起作用。我用过一次,很久以前,所以它可能并不完美。

HANDLE current = GetStdHandle (STD_OUTPUT_HANDLE);

HANDLE buffer = CreateConsoleScreenBuffer (
    GENERIC_WRITE,
    0,
    NULL,
    CONSOLE_TEXTMODE_BUFFER,
    NULL
);

WriteConsole (/*fill with what you're drawing*/);

system ("cls"); //clear this screen before swapping    
SetConsoleActiveScreenBuffer (buffer);

WriteConsole (/*do it to the other one now*/);

system ("cls");    
SetConsoleActiveScreenBuffer (current); //swap again

//repeat as needed

CloseHandle (buffer); //clean up
于 2012-05-07T04:26:22.400 回答