C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 10-04-2002, 09:03 PM   #16
RoD
Banned
 
RoD's Avatar
 
Join Date: Sep 2002
Posts: 6,334
i just realized it is taking NO input from the keyboard, AT ALL.

hmm...
RoD is offline  
Old 10-04-2002, 09:22 PM   #17
BMJ
Registered User
 
Join Date: Aug 2002
Posts: 1,330
hmmm what?

You can do it!

There, does that help?
BMJ is offline  
Old 10-04-2002, 09:25 PM   #18
RoD
Banned
 
RoD's Avatar
 
Join Date: Sep 2002
Posts: 6,334
haha no :P

I'm tired and getting off ina bit, i'll take a crack at it in the morning, cause i know its right in front of me.
RoD is offline  
Old 10-04-2002, 11:02 PM   #19
Registered User
 
harryP's Avatar
 
Join Date: Sep 2002
Posts: 124
Perhaps I'm wrong, but the way I do it is with getch(). Because it takes the ASCII value of the key pressed, which is what is defined in red_baron's code. In my game, I had to do that, too. cin won't let you do what you're trying to do, mate. I made my own function, GetKey(), because I had to use cin.ignore(0 as well.
Code:
int GetKey()
{
     cin.ignore(1,'\n');
     return getch();
}
That should work. You can then use baron's code. Hope that helped!

Brendan
__________________
Draco dormiens nunquam titallandus.
Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html
harryP is offline  
Old 10-05-2002, 07:19 AM   #20
RoD
Banned
 
RoD's Avatar
 
Join Date: Sep 2002
Posts: 6,334
Usually i can get things i do wrong like "that", but i have never done this before i am so stuck, i tried harry p, nadda: I will edit this post with a link to dload the program so u see what i mean.

edit:// www.nelie.org/steveprog/Caverns.zip

Code:
int GetKey()
{
     cin.ignore(1,'\n');
     return getch();
}




int main ()
{
	openmessage(); /* Call opening message*/

	
	

	/* Disable cursor in console
	SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
	), ENABLE_LINE_INPUT);*/

	do/* Start loop*/
	{

		/* Checks to see if the points are at 100
	if they are, user wins and game ends.*/
	if (points == 100)
	{
		cout <<"You have reached 100 points, you win!!\n\n";

		getch();

		return 0;
	}

	/* Checks to see if health is at a value of 0
	if it is the user has lost and game ends.*/
	if (health == 0)
	{
		cout <<"Game Over!\n\n";
		getch();
		return 0;
	}

	
	/* This displays every loop.*/
	directions();
	
	cout <<"Select Direction: ";
	GetKey();

	if (input == K_UP)
	{
		User_Location_Y = User_Location_Y++;
	}

	if (input == K_DOWN)
	{
		User_Location_Y = User_Location_Y--;
	}

	if (input == K_LEFT)
	{
		User_Location_X = User_Location_X--;
	}

	if (input == K_RIGHT)
	{
		User_Location_X = User_Location_X++;
	}

	if (input == K_END)
	{
		return 0;
	}

Last edited by RoD; 10-05-2002 at 07:33 AM.
RoD is offline  
Old 10-05-2002, 08:03 AM   #21
RoD
Banned
 
RoD's Avatar
 
Join Date: Sep 2002
Posts: 6,334
Also i am noticing my code is getting sloppy and such as i expand it, so i want to break it into several .cpp's.

So lets say i have Openmsg.cpp or Directions.cpp instead of having the code for em in the main.cpp, how would i call them to display or be executed at a certain point?

Example:

i have a cpp that needs to be executed after one point and before another that are in the main.cpp, how do i call it to execute?
RoD is offline  
Old 10-05-2002, 10:22 AM   #22
Registered User
 
harryP's Avatar
 
Join Date: Sep 2002
Posts: 124
I don't know about your multiple .cpp files problem, but this is actually what the GetKey() function looks like:
Code:
// gets a key
int GetKey() // created by Red_Baron who got help with it from salem
{
	int input=getch();
	if (input==224)
		input=getch();
	if (input==0)
		input=256+getch();
	return input;
}
Then, in my game loop, I just called that function at the beginning of the loop with a variable.
Code:
int key;

...

for(;;)
{
     key = GetKey();

     ...

     switch (key)
     {
     case K_UP:
          ...
     case K_DOWN:
          ...
     case K_LEFT:
          ...
     case K_RIGHT:
          ...
}
If that doesn't work, then I'm as stumped as you!

Brendan

[EDIT]I had to add the ending code tag, I'd forgotten it [/EDIT]
__________________
Draco dormiens nunquam titallandus.
Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html
harryP is offline  
Old 10-05-2002, 11:40 AM   #23
RoD
Banned
 
RoD's Avatar
 
Join Date: Sep 2002
Posts: 6,334
hmm it was kinda owrking, but i had to hit the key like 700 times first

grr, i'm wiping out all the code its such a mess, let me see if i can start clean and get it working.
RoD is offline  
Old 10-05-2002, 01:02 PM   #24
RoD
Banned
 
RoD's Avatar
 
Join Date: Sep 2002
Posts: 6,334
I got a little sumthing now. Heres the little code it has now, as well as the output i get when i hit any given key in the console.

Its only accepting these keys and its not working right at all, i am so damn lost rite now its ........ing me off.

Code:
/*	Steven Billington 
	Silent_Death17@hotmail.com
	Caverns.cpp
	September 27, 2002 

	This is my first game ever. Its a relativley simple program
	as it should be cause its newbie made. Its a console based text RPG.

	Welcome to Caverns, enjoy!

*/

/****************************
*****************************
Updated September 28, 2002
*****************************
****************************/

/* These are our Preprocessor directives*/
#include <iostream.h>
#include <windows.h>
#include <iomanip.h> /* For input/output*/
#include <stdlib.h> /* For clear screen*/
#include <conio.h> /* For getch() - makes program pause till user
				      user presses a key*/

	
/* Declare variables for game-play.
The names should explain it, email me
if you don't understand something.*/
int User_X = 0;
int User_Y = 0;
int end_game = 50;

/* Our variable for users choices throught
game play.*/
char input;


/* Declare main as variable type integer.*/
void openmessage ()
{

	cout <<setw(50) <<"***************************"<<"\n";
	cout <<setw(50) <<"****     Caverns        ***"<<"\n";
	cout <<setw(50) <<"****  Steven Billington ***"<<"\n";
	cout <<setw(50) <<"***************************"<<"\n";
}

void directions ()
{
	cout <<"North = Up Arrow\nSouth = Down Arrow\nWest = Left Arrow\nEast = Right Arrow\n";
	cout <<"\n";
	cout <<"Home = Information\n";
	cout <<"\n";
	cout <<"Insert = Health\nP = Points\n";
	cout <<"\n";
	cout <<"End = Exit\n";
	cout <<"\n";
}


			  // gets a key






int main ()
{
	openmessage(); /* Call opening message*/

	cout <<"\n";

	directions();
	getch();
	
	do
	{

		if (User_X  == 6)
        {
			cout << "You can not continue in this direction.\n";
			User_X = User_X--;
			getch();
        }
    
		else if(User_X == -6)
		{
			cout << "You can not continue in this direction.\n";
			User_X = User_X++;
			getch();
		} 

		else if(User_Y == 6)
		{
			cout << "You can not continue in this direction.\n";
			User_Y = User_Y--;
			getch();
		}   

		else if(User_Y == -6)
		{
			cout << "You can not continue in this direction.\n";
			User_Y = User_Y++;
			getch();
		}

//-------------------------------------------------------------------------
//  Game over if 100 turns expire
//-------------------------------------------------------------------------

end_game = end_game--;         //Game_Over is reduced by 1 each loop when    
if(end_game == 0)                              //it reaches 0 the game ends
    {
    system("cls");
    cout << "Game Over.";
    getch();
    return 0;                                   //This teminates the program
    }

		input = getch();	
		cout <<"Select Action: ";
		getch();

		switch(input)
		{
			case VK_UP: cout << "You venture north.\n\n";
				User_Y = User_Y++; break;     //Include break on the 
			case VK_RIGHT: cout << "You venture east.\n\n";            //same line saves space
				User_X = User_X++; break;     //most people say its
			case VK_DOWN: cout << "You venture south.\n\n";           //best to put it on a  
				User_Y = User_Y--; break;     //new line but this code 
			case VK_LEFT: cout << "You venture west.\n\n";            //is readable enough
				User_X = User_X--; break;
		} 
		
	}
	while(end_game!=1);
	


	
	return 0;
	
}
***************************
Heres the output now
***************************

Code:
                       ***************************
                       ****     Caverns        ***
                       ****  Steven Billington ***
                       ***************************

North = Up Arrow
South = Down Arrow
West = Left Arrow
East = Right Arrow

Home = Information

Insert = Health
P = Points

End = Exit

Select Action: Select Action: Select Action: Select Action: Select Action: Selec
t Action: Select Action: Select Action: Select Action: Select Action: Select Act
ion: Select Action: Select Action: Select Action: Select Action: Select Action:
Select Action: Select Action: Select Action: Select Action: Select Action: Selec
t Action: Select Action: Select Action: Select Action: Select Action: Select Act
ion: Select Action: Select Action: Select Action: Select Action: Select Action:
Select Action: Select Action: Select Action: Select Action: Select Action: Selec
t Action: Select Action: Select Action: Select Action: Select Action: Select Act
ion: Select Action: Select Action: Select Act
RoD is offline  
Old 10-05-2002, 03:04 PM   #25
I lurk
 
Join Date: Aug 2002
Posts: 1,361
You're not going to able to use Virtual Keys with getch(), either use the function BMJ gave you or don't use virtual keys. In MY opinion, I think you should stay away from using the arrow keys for now and work on the design of your game. I choose 'wsad' for moving around, you can change it if you like.

Your game loop is extremely hard to follow, I think it can be done much more efficiently... something like:

PHP Code:
#define MODIFY_X 0
#define MODIFY_Y 1

#define INCREMENT 1
#define DECREMENT 0

bool move(bool moveWhichbool increment)
{
    if (
increment)
    {
        if (
moveWhich// Incrementing y
        
{
            if (
User_Y == 5)
            {
                return 
0;
            } else {
                
User_Y++;
                return 
1;
            }
        } else {
            if (
User_X == 5)
            {
                return 
0;
            } else {
                
User_X++;
                return 
1;
            }
        }
    } else {
        if (
moveWhich// Decrementing y
        
{
            if (
User_Y == -5)
            {
                return 
0;
            } else {
                
User_Y--;
                return 
1;
            }
        } else {
            if (
User_X == -5)
            {
                
cout << "You cannot continue in that direction!" << endl;
                return 
0;
            } else {
                
User_X--;
                return 
1;
            }
        }
    }        
        
}


for (
int i 0100i++)  // 100 moves
{
    
cout << "Select Action: ";
    while (!
kbhit()); // Normally we'd put any processing that'd have to be done
              // while waiting for input (like animation)

    
int keyHit getch();
    
cout << endl;

    switch(
tolower(keyHit))
    {
    case 
'a':
        if (!
move(MODIFY_XDECREMENT))
        {
            
cout << "You cannot continue in that direction!" << endl;
            --
i;
            continue;
        } else {
            
cout << "You venture west" << endl;
            
// Extra handling;
        
}
        break;
    case 
's':
        if (!
move(MODIFY_YDECREMENT))
        {
            
cout << "You cannot continue in that direction!" << endl;
            --
i;
            continue;
        } else {
            
cout << "You venture south" << endl;
            
// Extra handling
        
}
        break;
    case 
'd':
        if (!
move(MODIFY_XINCREMENT))
        {
            
cout << "You cannot continue in that direction!" << endl;
            --
i;
            continue;
        } else {
            
cout << "You venture east" << endl;
            
// Extra handling
        
}
        break;
    case 
'w':
        if (!
move(MODIFY_YINCREMENT))
        {
            
cout << "You cannot continue in that direction!" << endl;
            --
i;
            continue;
        } else {
            
cout << "You venture north" << endl;
            
// Extra handling
        
}
        break;
    default:
        
cout << "Input error!" << endl;
        --
i;      // Decrement i, we didn't actually go anywhere
        
continue; // Skip back up top and start again
    
}
    
// Winning conditions
}

// Losing
cout << endl << "Game Over!" << endl;
getch();
return 
0

Last edited by Eibro; 10-05-2002 at 03:11 PM.
Eibro is offline  
Old 10-05-2002, 05:35 PM   #26
Registered User
 
harryP's Avatar
 
Join Date: Sep 2002
Posts: 124
I saw you used 'getch()' after showing the directions, which is of course fine. But after that's been done, the same issue occurs if you usen cin before cin.get(). That's the point of the GetKey() function. Here's some code to explain more in depth what I'm talking about:
Code:
<#includes>
#include "functions.h" // Pretend most functions are in this header
// gets a key
int GetKey() // created by Red_Baron who got help with it from salem
{
     int input=getch();
     if (input==224)
	input=getch();
     if (input==0)
	input=256+getch();
     return input;
}

// game loop
int main()
{
     int key;
     Openmsg();
     Directions();
     getch();

     for( ;; )
     {
          key = GetKey();

          switch(key)
          {
          case K_UP:
               cout << "North\n";
               break;
          case K_DOWN:
               cout << "South\n";
               break;
          case K_LEFT:
               cout << "West\n";
               break;
          case K_RIGHT:
               cout << "East\n";
               break;
          case K_ESCAPE:
               cout << "Exiting...\n";
               key = GetKey();
               return 0;
          }
     }
     return 0;
}
__________________
Draco dormiens nunquam titallandus.
Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html
harryP is offline  
Old 10-05-2002, 09:59 PM   #27
BMJ
Registered User
 
Join Date: Aug 2002
Posts: 1,330
I don't see why this is so hard...
PHP Code:
/*    Steven Billington 
    [email]Silent_Death17@hotmail.com[/email]
    Caverns.cpp
    September 27, 2002 

    This is my first game ever. Its a relativley simple program
    as it should be cause its newbie made. Its a console based text RPG.

    Welcome to Caverns, enjoy!

*/

/****************************
*****************************
Updated September 28, 2002
*****************************
****************************/

/* These are our Preprocessor directives*/
#include <iostream.h>
#include <windows.h>
#include <iomanip.h> /* For input/output*/
#include <stdlib.h> /* For clear screen*/
#include <conio.h> /* For getch() - makes program pause till user
                      
user presses a key*/

    
/* Declare variables for game-play.
The names should explain it, email me
if you don't understand something.*/
int User_X 0;
int User_Y 0;
int end_game 50;

/* Our variable for users choices throught
game play.*/
char input;

/* Keypress function uses the Windows message queue to check the console input buffer
   for keypresses... if a key has been pressed while in the console, the function will
   return true, and Key will become the virtual key code of the key that was pressed
   and the event will be removed from the queue. */
bool Keypress(char &Key)
{
    
INPUT_RECORD Event;
    
DWORD NumberOfEventsEventsReadEventCounter;

     
GetNumberOfConsoleInputEvents(GetStdHandle(STD_INPUT_HANDLE), &NumberOfEvents);

    if (
NumberOfEvents == 0)
        return 
false;
    
    for (
EventCounter 0EventCounter NumberOfEventsEventCounter++)
    {
         
PeekConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event1, &EventsRead);
        if ((
Event.EventType == KEY_EVENT) && ((Event.Event.KeyEvent.bKeyDown)))
        {
             
ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event1, &EventsRead);
            
Key Event.Event.KeyEvent.wVirtualKeyCode;
            if  (!(
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE))))
                exit(
0);
            return 
true;
        }
        else
             
ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event1, &EventsRead);
    }

    return 
false;
}

/* Declare main as variable type integer.*/
void openmessage ()
{

    
cout <<setw(50) <<"***************************"<<"\n";
    
cout <<setw(50) <<"****     Caverns        ***"<<"\n";
    
cout <<setw(50) <<"****  Steven Billington ***"<<"\n";
    
cout <<setw(50) <<"***************************"<<"\n";
}

void directions ()
{
    
cout <<"North = Up Arrow\nSouth = Down Arrow\nWest = Left Arrow\nEast = Right Arrow\n";
    
cout <<"\n";
    
cout <<"Home = Information\n";
    
cout <<"\n";
    
cout <<"Insert = Health\nP = Points\n";
    
cout <<"\n";
    
cout <<"End = Exit\n";
    
cout <<"\n";
}

int main ()
{
    
openmessage(); /* Call opening message*/

    
cout <<"\n";

    
directions();
    while (!
Keypress(input)) {};
    
    do
    {

        if (
User_X  == 6)
        {
            
cout << "You can not continue in this direction.\n";
            
User_X User_X--;
            
getch();
        }
    
        else if(
User_X == -6)
        {
            
cout << "You can not continue in this direction.\n";
            
User_X User_X++;
            
getch();
        } 

        else if(
User_Y == 6)
        {
            
cout << "You can not continue in this direction.\n";
            
User_Y User_Y--;
            
getch();
        }   

        else if(
User_Y == -6)
        {
            
cout << "You can not continue in this direction.\n";
            
User_Y User_Y++;
            
getch();
        }

//-------------------------------------------------------------------------
//  Game over if 100 turns expire
//-------------------------------------------------------------------------

end_game end_game--;         //Game_Over is reduced by 1 each loop when    
if(end_game == 0)                              //it reaches 0 the game ends
    
{
    
system("cls");
    
cout << "Game Over.";
    
getch();
    return 
0;                                   //This teminates the program
    
}

        
cout <<"Select Action: ";

        do {} while (!
Keypress(input));

          switch(
input)
           {
               case 
VK_UPcout << "You venture north.\n\n"Sleep(500);
                   
User_Y User_Y++; break;     //Include break on the 
               
case VK_RIGHTcout << "You venture east.\n\n"Sleep(500);  //same line saves space
                   
User_X User_X++; break;     //most people say its
               
case VK_DOWNcout << "You venture south.\n\n"Sleep(500);  //best to put it on a  
                   
User_Y User_Y--; break;     //new line but this code 
               
case VK_LEFTcout << "You venture west.\n\n"Sleep(500);   //is readable enough
                   
User_X User_X--; break;
            default: 
cout << "Not an option!\n\n"Sleep(500);
           }
    }
    while(
end_game!=1);
    
    return 
0;
    

BMJ is offline  
Old 10-05-2002, 10:02 PM   #28
BMJ
Registered User
 
Join Date: Aug 2002
Posts: 1,330
And then you don't need getch() to pause anymore; Use Windows to your advantage:
PHP Code:
void Pause()
{
     while (!
Keypress(input)) {};

BMJ is offline  
Old 10-06-2002, 01:42 AM   #29
x4000
 
Ruski's Avatar
 
Join Date: Jun 2002
Location: Outer Space!
Posts: 542
How did you do that code formatting.. I mean that colours etc.. You didn't format them YOURSELF did you???
__________________
what does signature stand for?
Ruski is offline  
Old 10-06-2002, 01:45 AM   #30
x4000
 
Ruski's Avatar
 
Join Date: Jun 2002
Location: Outer Space!
Posts: 542
By the way.. I have the same problem using the direction keys as well... I tried GetKeyState() but virtual keys don't work (excluding VK_RETURN) (Console) .. I also tried that getch() but when you press one of the arrows.. it just says p:
Code:
int main()
{
  char choice[1];
  choice[0] = getch();
  cout << choice[0];
  return 0;
}
__________________
what does signature stand for?
Ruski is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wiki FAQ dwks General Discussions 192 04-29-2008 01:17 PM
directional keys @ console ipe C Programming 1 03-13-2003 06:25 AM
Directional Keys - Useing in Console RoD C++ Programming 38 10-06-2002 04:42 PM
Arrow keys in console? SyntaxBubble C++ Programming 3 02-02-2002 06:12 PM


All times are GMT -6. The time now is 02:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22