-1

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. How can I do that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

2

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 full screen 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 applications, like GNU nano. A full list of applications using ncurses can be found here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cs1349459
  • 911
  • 9
  • 27
  • Which link is about text scrolling? Also it seems like ncurses does stuff with C not C++. – Jacob Thompson Sep 28 '22 at 21:05
  • @JacobThompson It will work just fine with C++, and I believe that using the `pad` function is required. I think you can use pads, and using the `prefresh` command with different values as the second and third argument, scroll the window. I am not sure, as I am inexperienced in the ways of `ncurses`, but I looked in there links for resources: https://stackoverflow.com/questions/6912889/what-is-the-recommended-way-to-implement-text-scrolling-in-ncurses https://stackoverflow.com/questions/10133489/wrapping-text-in-pads-using-ncurses-in-c – cs1349459 Sep 28 '22 at 21:49
0

I'm going to assume you're 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 milliseconds (1 second). 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AnFer
  • 1
  • 2
  • Will I have to do the for loop around every block of std::cout, or do you do it around every std::cout? Also what does the & and : do in the for loop? – Jacob Thompson Sep 28 '22 at 20:56
  • Every time you wanna call `std::cout` you would have to do this. So you would probably make a function that you pass your text to, and call said function instead of std::cout. And the '&' is here to make `current_char` a reference to the string's character. A "simpler" kind of pointer. That prevents the character from having to be copied which would slow the loop down. Have a quick look at what references are, they are quite useful. – AnFer Sep 29 '22 at 07:07