Sign in to follow this  

Text Based Game Programming...

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Using C++, I know all about cout and cin, but for txt based games, how can i clear the entire screen at once and then reprint out everything? Also, how can i get keyboard input? I mean like the left key, the space key, etc, using standard C++ libraries?

Share this post


Link to post
Share on other sites
If you're developing solely for windows use system("cls") or generally on Linux system("clear").

I'm not sure that there are C++ standard keyboard input routines. I do know that something along the lines of _kbhit is used for VC++.

Share this post


Link to post
Share on other sites
How? I am talking about like doing

cout << line1 << std::endl << line2 << std::endl << ... lineN << std::endl;

now how can I clear the command prompt?

Never mind... answered

However, there is know standard way to clear the prompt, that is preferably from the Standard Template Library?

Share this post


Link to post
Share on other sites
Well, if your looking to be able to use code for both linux and windows, do this:

#ifdef _WIN32
#define CLEAR "cls"
#endif
#ifdef _LINUX
#define CLEAR "clear"
#endif
//then,
//in main do this
system(CLEAR);//works in both win32 AND linux!


//or do this...
#include <conio.h>

//then in main...
clearscr();


[edit]
included conio.

Share this post


Link to post
Share on other sites
as far as getting key commands
getch() will get you the keys, though you will often need to print out
what key number you got back(or find the numbers somewhere) to figure
out what each key returns... non needed for letter/numbers/symbols, but
is needed for enter(13), backspace(8), escape(27)... everything you can
type can be compared to 'a' 'b' ' '...


#include <conio.h>
#include <iostream>
...
{
...
if ( _kbhit() )//check that a key was pressed
{
char key = getch(); //since this would block you code if no key has been pressed

dispatch(key); // go do something with your key
}
...
}

dispatch(char key)
{
if ( key == 8 ) cout<<"Backspace!\n"; //Or whatever you feel like doing here
if ( key == 13 ) cout<<"Enter!\n";
if ( key >= 'a' && key <= 'z' ) cout<<key;
}


Share this post


Link to post
Share on other sites

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this