Home > Software engineering >  How do you make RPG-like scrolling text. C on linux
How do you make RPG-like scrolling text. C on linux

Time:09-29

I'm a beginner coder making a simple text 'choose your own adventure' game, but I want it to scroll out the text like an RPG instead of just spitting out the text. Does anyone know how to do that?

CodePudding user response:

I believe that the ncurses library is exactly what you are looking for. It allows you lower access to the terminal text, to produce things like fullscreen text, like this:

Picture of ncurses terminal window

A tutorial for how to use it can be found here, and you can download version 6.3 here.

It is used in many apps, like Gnu nano. A full list of apps using ncurses can be found here.

CodePudding user response:

I'm gonna assume your just supposed to write to a console.

std::string str = "Your text";
for(char& current_char : str) {
    std::cout << current_char << std::flush;
    sleep(1000);
}
std::cout << std::endl;

The for loop is going to iterate over each character in the string. It will then output it to cout. std::flush is here to force the output to be update without using std::endl which would return carriage and we don't want that.

sleep will pause for an amount of time in milliseconds, here 1000 millisecs (1sec). Be careful though, sleep is a windows function, on linux use usleep. If you need it to be cross-platform you should probably use the thread sleeping functions or make something yourself with chrono

And finally, we use std::endl to return carriage and update the output.

  • Related