PDA

View Full Version : Few things...



LouDu
02-28-2003, 05:25 PM
ok i am a new C++ dos program, and i am very intertesed in persuiong at it more. I take a class in schoola nd work at my house, and read books about programing, and am starting to build my own TEXT RPG, i am not having to many problems yet, but i jsut encouintered one.

For example you playing the game and exploring aound, and i want to have it so say for exampke at ANY time you hit 'kkkkkkkkkkkkkkkkkkkkkkkkkk' and hit <enter> a map of the town were you are comes up and fromt aht you can cchoose were to go useing if else statements how do you do it so the menu can popup at any time? thats is my question. Thanks alot all :)

Prelude
02-28-2003, 09:04 PM
You poll for command strings:


// disclaimer: pseudocode may cause cancer

while ( true ) { // Game loop type deal
if ( kbhit() ) {
cmd_str = get_cmd_str();
process ( cmd_str );
}
}

kbhit() is often placed in the conio.h header, but since it isn't a standard header it may be somewhere else.

-Prelude

loudu
03-01-2003, 08:10 AM
thanks alot, bnut as a i said i'm a major newb:) lol and i dont really understand to mcuh can you elaborate

Prelude
03-01-2003, 08:28 AM
> i dont really understand to mcuh can you elaborate
There is nothing complicated in the code I gave you, perhaps you should start working with something easier than a game.

-Prelude

LouDu
03-01-2003, 08:30 AM
can you elaborate on the code and help me understand it more...?

Prelude
03-01-2003, 08:44 AM
>can you elaborate on the code and help me understand it more...?
It loops forever, checks to see if a key on the keyboard was hit, then processes input if it was.

-Prelude

LouDu
03-01-2003, 08:49 AM
i may sound like a broken record, but i'm so lost.


this is what i know:

i know basic cin cout statements, if else staements intergers, chars, floats, do while staements, and a few other little things and i am trying to work wiht what i haveso if i have no idea what you are talking about

Prelude
03-01-2003, 09:16 AM
This uses what you've learned so far:


#include <iostream>
#include <conio.h>

int main()
{
do {
if ( kbhit() ) { // If a key is hit
int cmd;

if ( std::cin>> cmd ) // Read an integer
// If cin succeeds, print it
std::cout<<"You entered: "<< cmd <<std::endl;
}
} while ( true ); // Loop forever, condition always true
}

-Prelude

LouDu
03-01-2003, 10:26 AM
thanks alot any other tips or suggestions for gettign started making a text rpg i would greatly appericate thanks again

LouDu
03-01-2003, 10:33 AM
Ok i implemented the code, and well its not what i am looking for, or i am doing something wrong this is what i have..



#include <iostream>
#include <conio.h>
#include <iomanip.h>


int cmd;


int main()
{

cout << "hello world..... " ;

do {
if ( kbhit() )

{
if ( std::cin>> cmd )
std::cout<< "blah blah blah " << cmd <<std::endl;
}

} while ( true );
}

Nuke
03-30-2003, 12:47 AM
Yeah, I know this is an old message an you probably figured it out, but if you didn't.. you could use "getch()" or "getche()"

getch() = get character
getche() = same but echo's it back to the screen.

Syntax goes something like this:



#include <conio.h> //I'm pretty sure it's in this header.
#include <stdlib.h> //this is for the system command and printf

int main()
{
char variable; //defines variable as type character
variable = getch(); //get character and throw it into variable
printf("%c\n",variable); //prints it out (also can be done with cout)

system("PAUSE"); //press any key
return 0; //exit
}


Using this you can have it set with if statements and loops so that when someone enters a "k" display the map. :)