C++ C++;如何在不擦除屏幕的情况下闪烁单行文本?

C++ C++;如何在不擦除屏幕的情况下闪烁单行文本?,c++,animation,io,C++,Animation,Io,操作系统:Windows 10 编译器:Visual Studio 2017 C++版本:14 我正在为学校做一个文字冒险游戏。 在某些屏幕上,我希望文本段落以闪烁的文本行结束,该行显示“按“回车”继续”,并带有动画省略号,当用户按“回车”时,该行被清除,下一段随后出现。我写了一些代码,我认为可以工作,但这一行并没有改变 Press ENTER to continue 随着时间的推移。以下是最低限度的“工作”示例: #包括 #包括 #包括 #包括,; 使用名称空间std; 使用名称空间此_线程

操作系统:Windows 10 编译器:Visual Studio 2017 C++版本:14

我正在为学校做一个文字冒险游戏。 在某些屏幕上,我希望文本段落以闪烁的文本行结束,该行显示“按“回车”继续”,并带有动画省略号,当用户按“回车”时,该行被清除,下一段随后出现。我写了一些代码,我认为可以工作,但这一行并没有改变

Press ENTER to continue
随着时间的推移。以下是最低限度的“工作”示例:

#包括
#包括
#包括
#包括,;
使用名称空间std;
使用名称空间此_线程;//睡觉,睡到
使用命名空间chrono_文本;//ns、us、ms、s、h等。
使用chrono::系统时钟;
//类来实现UI方法
类用户界面{
公众:
UI(){}
作废打印标题卡(){

这解决了Windows平台上C++的3个常见问题(我不主张第二个是一个新的或需要的解决方案):

  • 如何在不清除屏幕的情况下使一行文本“闪烁”
  • 如何删除和覆盖控制台行
  • 如何从持续运行的异步循环中运行和退出
以下是我的最低实际工作示例:

    #include <iostream>
    #include <string>
    #include <chrono>
    #include <thread>;

    using namespace std;
    using namespace this_thread;     // sleep_for, sleep_until
    using namespace chrono_literals; // ns, us, ms, s, h, etc.
    using chrono::system_clock;

    //class to implement UI methods
    class UI {
    public:
        UI() {}

        void printTitleCard() {
            cout << "+---------------------------+" << endl;
            cout << "|    \"A Text Adventure\"     |" << endl;
            cout << "|            by             |" << endl;
            cout << "|            Me             |" << endl;
            cout << "+---------------------------+" << endl << endl;
        }

        void intro() {
            cout << "Your mother warned you about getting into cars with strangers." << endl;
            cout << "She didn't say anything about vans, true, and it did have a" << endl;
            cout << "friendly moustache on the front bumper, but you knew the risks." << endl;
            cout << "Now you're waking up in the wilderness with no wallet and no clues..." << endl;
            pressEnterToContinue();
            cout << "...and there's something on your shoes." << endl;
        }

        void pressEnterToContinue() {
        //This prompts the user to press "ENTER" to continue
        while (1) {
            string str = "Press ENTER to continue";
            cout << str;
            sleep_until(system_clock::now() + 0.25s); // wait a quarter-second
            cout << string(str.length(), '\b'); // delete printed line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.25s); // wait a quarter-second
            cout << string(str.length(), '\b'); // delete printed line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.25s); // wait a quarter-second
            cout << string(str.length(), '\b'); // delete printed line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.25s); // wait a quarter-second
            cout << string(str.length(), '\b'); // delete printed line
            if (cin.get()) break;
        }
        cin.ignore();
    }
};

    void main()
    {   
        UI ui;
        //Title Card
        ui.printTitleCard();

        //Intro
        ui.intro();
    }
编辑:确定,因此仍然存在一个缺陷:如果您再次尝试运行pressEnterToContinue()函数,循环只运行一次。这可能与我如何设置循环条件有关。希望得到反馈

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <future>

using namespace std;
using namespace this_thread;     // sleep_for, sleep_until
using namespace chrono_literals; // ns, us, ms, s, h, etc.
using chrono::system_clock;

//class to implement UI methods
class UI {
public:
    UI() {}

    void printTitleCard() {
        cout << "+---------------------------+" << endl;
        cout << "|    \"A Text Adventure\"     |" << endl;
        cout << "|            by             |" << endl;
        cout << "|            Me             |" << endl;
        cout << "+---------------------------+" << endl << endl;
    }

    void intro() {
        cout << "Your mother warned you about getting into cars with strangers." << endl;
        cout << "She didn't say anything about vans, true, and it did have a" << endl;
        cout << "friendly moustache on the front bumper, but you knew the risks." << endl;
        cout << "Now you're waking up in the wilderness with no wallet and no clues..." << endl;
        pressEnterToContinue();
        cout << "...and there's something on your shoes." << endl;
    }

    void pressEnterToContinue() {

        using namespace std::literals;
        string str;

        auto f = std::async(std::launch::async, [str] {
            string userStr;
            getline(cin, userStr);
            if (userStr == "\r") {
                cout << string(str.length(), '\b'); // go to front of line
                cout << string(str.length(), ' ');; //print spaces
                cout << string(str.length(), '\b'); // go to front of line
                return;}
        });

        while (f._Is_ready()==false/*f.wait_for(1s) != std::future_status::ready*/) {
            str = "Press ENTER to continue";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            cout << string(str.length(), ' ');; //print spaces
            cout << string(str.length(), '\b'); // go to front of line
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
        }
    }
};

int main()
{   
    UI ui;
    //Title Card
    ui.printTitleCard();

    //Intro
    ui.intro();
    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用命名空间this_thread;//sleep_for,sleep_until
使用名称空间chrono_文本;//ns、us、ms、s、h等。
使用chrono::系统时钟;
//类来实现UI方法
类用户界面{
公众:
UI(){}
作废打印标题卡(){

您是否遗漏了重要信息,如操作系统(和版本)以及您使用的是哪种终端。Linux世界中使用的终端支持转义序列
VT100
escape sequence,这里有很多关于这方面的问题。如果您想要跨平台解决方案,您需要使用类似
ncurses
的库。如果您不在“打印”之间刷新您可能看不到结果…无论如何,您的方法不正确,您可以利用您的终端功能。大多数终端模拟器都接受转义序列以获得多种效果。如果您使用*nix变体,请尝试使用ncurses库。哎哟,我应该知道得更清楚。操作系统和编译器现在列在我的qu开头我相信你不能用标准库来做这件事。我相信使用库是最常用的方法,这类任务最流行的是NCURSE。对于好的终端处理,C++标准库不是很好(因为它不知道终端和它们的能力)。。在我看来,如果您想要任何类型的“高级”(如闪烁)终端处理,最好的解决方案是使用特定于操作系统的功能(如POSIX系统上的termcap,用curses库抽象,或)。
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <future>
//#include <C:/Program Files/boost/boost_1_71_0/boost/atomic.hpp>

using namespace std;
using namespace this_thread;     // sleep_for, sleep_until
using namespace chrono_literals; // ns, us, ms, s, h, etc.
using chrono::system_clock;

//class to implement UI methods
class UI {
public:
    UI() {}

    void printTitleCard() {
        cout << "+---------------------------+" << endl;
        cout << "|    \"A Text Adventure\"     |" << endl;
        cout << "|            by             |" << endl;
        cout << "|            Me             |" << endl;
        cout << "+---------------------------+" << endl << endl;
    }

    void intro() {
        cout << "Your mother warned you about getting into cars with strangers." << endl;
        cout << "She didn't say anything about vans, true, and it did have a" << endl;
        cout << "friendly moustache on the front bumper, but you knew the risks." << endl;
        cout << "Now you're waking up in the wilderness with no wallet and no clues..." << endl;
        pressEnterToContinue();
        cout << "...and there's something on your shoes." << endl;
    }

    void pressEnterToContinue() {

        using namespace std::literals;
        string str;

        auto f = std::async(std::launch::async, [str] {
            string userStr;
            getline(cin, userStr);
            if (userStr == "") {
                cout << string(str.length(), '\b'); // go to front of line
                cout << string(str.length(), ' ');; //print spaces
                cout << string(str.length(), '\b'); // go to front of line
                cin.ignore();
                return;}
        });

        while (f._Is_ready()==false/*f.wait_for(1s) != std::future_status::ready*/) {
            str = "Press ENTER to continue";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            cout << string(str.length(), ' ');; //print spaces
            cout << string(str.length(), '\b'); // go to front of line
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
        }
    }
};

void main()
{   
    UI ui;
    //Title Card
    ui.printTitleCard();

    //Intro
    ui.intro();
}
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <future>

using namespace std;
using namespace this_thread;     // sleep_for, sleep_until
using namespace chrono_literals; // ns, us, ms, s, h, etc.
using chrono::system_clock;

//class to implement UI methods
class UI {
public:
    UI() {}

    void printTitleCard() {
        cout << "+---------------------------+" << endl;
        cout << "|    \"A Text Adventure\"     |" << endl;
        cout << "|            by             |" << endl;
        cout << "|            Me             |" << endl;
        cout << "+---------------------------+" << endl << endl;
    }

    void intro() {
        cout << "Your mother warned you about getting into cars with strangers." << endl;
        cout << "She didn't say anything about vans, true, and it did have a" << endl;
        cout << "friendly moustache on the front bumper, but you knew the risks." << endl;
        cout << "Now you're waking up in the wilderness with no wallet and no clues..." << endl;
        pressEnterToContinue();
        cout << "...and there's something on your shoes." << endl;
    }

    void pressEnterToContinue() {

        using namespace std::literals;
        string str;

        auto f = std::async(std::launch::async, [str] {
            string userStr;
            getline(cin, userStr);
            if (userStr == "\r") {
                cout << string(str.length(), '\b'); // go to front of line
                cout << string(str.length(), ' ');; //print spaces
                cout << string(str.length(), '\b'); // go to front of line
                return;}
        });

        while (f._Is_ready()==false/*f.wait_for(1s) != std::future_status::ready*/) {
            str = "Press ENTER to continue";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            //repeat
            str += ".";
            cout << str;
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
            cout << string(str.length(), '\b'); // go to front of line
            cout << string(str.length(), ' ');; //print spaces
            cout << string(str.length(), '\b'); // go to front of line
            sleep_until(system_clock::now() + 0.5s); // wait a half-second
        }
    }
};

int main()
{   
    UI ui;
    //Title Card
    ui.printTitleCard();

    //Intro
    ui.intro();
    return 0;
}