简体   繁体   English

如何停止控制台应用程序中的闪烁?

[英]How do I stop flickering in a console application?

I made a simple Ping pong game in c++ through visual studio community(in windows). 我通过Visual Studio社区(在Windows中)用c ++制作了一个简单的乒乓游戏。 I made it, but the final product keeps flickering and it does not look good. 我做到了,但最终产品一直闪烁,而且看起来也不好。

I am new to c++. 我是C ++的新手。 And this is one of the first big projects I tried making. 这是我尝试进行的首批大型项目之一。 Doing some research, I found out that I made a console app, and to stop the flicker, I will have to remake my app in Graphics, which I can't,so I am looking for a way to modify my console app to reduce flicker. 做一些研究,我发现自己制作了一个控制台应用程序,并且要停止闪烁,我将不得不在Graphics中重新制作我的应用程序,但是我做不到,所以我正在寻找一种方法来修改控制台应用程序以减少闪烁。

My draw function-: 我的绘画功能-:

 void Draw()
{
    system("cls");
    for (int i = 0; i < width + 2; i++)
        cout << "\xB2";
    cout << endl;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int ballx = ball->getX();
            int bally = ball->getY();
            int player1x = player1->getX();
            int player2x = player2->getX();
            int player1y = player1->getY();
            int player2y = player2->getY();

            if (j == 0)
                cout << "\xB2";

            if (ballx == j && bally == i)
                cout << "O";//ball
            else if (player1x == j && player1y == i)
                cout << "\xDB";//player1_segment0
            else if (player2x == j && player2y == i)
                cout << "\xDB";//player2_segment0
            /*Prints different segments of player1 in y-dir*/
            else if (player1x == j && player1y + 1 == i)
                cout << "\xDB";//player1_segment1
            else if (player1x == j && player1y + 2 == i)
                cout << "\xDB";//player1_segment2
            else if (player1x == j && player1y + 3 == i)
                cout << "\xDB";//player1_segment3
            /*Prints different segments of player2 in y-dir*/
            else if (player2x == j && player2y + 1 == i)
                cout << "\xDB";//player2_segment1
            else if (player2x == j && player2y + 2 == i)
                cout << "\xDB";//player2_segment2
            else if (player2x == j && player2y + 3 == i)
                cout << "\xDB";//player2_segment3

            else
                cout << " ";

            if (j == width - 1)
                cout << "\xB2";
        }
        cout << endl;
    }

    for (int i = 0; i < width + 2; i++)
        cout << "\xB2";
    cout << endl;
    cout << "Score 1: " << score1 << endl;
    cout << "Score 2: " << score2 << endl;
}

Thanks. 谢谢。

This is what it looks like without flickers 这是没有闪烁的样子

Instead of 'system("cls");' 而不是'system(“ cls”);' implement code to "delete" the moving elements, then paint them in the new position. 实施代码以“删除”移动的元素,然后将它们绘制在新位置。

Clearing the screen will make the entire screen go black before you start drawing, causing flicker. 在开始绘制之前,清除屏幕会使整个屏幕变黑,从而导致闪烁。

I'd recommend looking into the Windows Console API (assuming you're happy for this to work only in windows). 我建议您研究Windows控制台API (假设您很高兴只在Windows中使用此功能)。 Set the cursor position and then draw everything. 设置光标位置,然后绘制所有内容。

COORD coord;
coord.X = 0;
coord.Y = 0;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

This is not code I've tested, and I don't know if it will work correctly with cout. 这不是我测试过的代码,我不知道它是否可以在cout中正常工作。 You might want to replace look at other operations such as 'WriteConsole(...)' 您可能需要替换一下其他操作,例如“ WriteConsole(...)”

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

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