Thread: FAQ: Directional Keys - Useing in Console

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    I don't see why this is so hard...
    PHP Code:
    /*    Steven Billington 
        [email][email protected][/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;
        


  2. #2
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    And then you don't need getch() to pause anymore; Use Windows to your advantage:
    PHP Code:
    void Pause()
    {
         while (!
    Keypress(input)) {};


  3. #3
    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.

  4. #4
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    Use the number pad arrows, they can be assigned simply by inserting the relevant number.

  5. #5
    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

  6. #6
    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

  7. #7
    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

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    FAQ: Directional Keys - Useing in Console

    Ok, i am trying to use the directional arrows to decide where the user moves too. I used to use letters off the keyboard, but with the use of barons keys.h i hoped to use the directionals.

    Unfortunatly none of the K_NAME 's are working, these are what is defined in the .h file. I have attahced the .h file and pasted the code to the program, i want to know what i am doing wrong, this is my first time trying this.

    Code:
    /*	Steven Billington 
    	[email protected]
    	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 <iomanip.h>
    #include "keys.h"
    #include <iostream.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_Location_X = 0;
    int User_Location_Y = 0;
    int Amulet = 0;
    int Quarter_Staff = 0;
    int Chainmail_Armor = 0;
    int Heal_Potion = 0;
    int Spell_Book = 0;
    int magic = 100;
    int points = 0;
    int health = 100;
    int protection = 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";
    	
    
    	/*Take user input and display opening messages
    	The user name can be up to 20 characters long*/
    	char name[20];
    
    	cout <<"Please enter your first name and press enter twice: ";
    	cin >>name; /* Store users name*/
    
    	cout <<"You may see controls at any time by pressing (HOME)\n\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*/
    
    	getch();
    
    
    	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;
    	}
    
    	
    	
    	
    	
    	/* Check user position.*/
    	if (User_Location_Y == 25)
    	{
    		cout <<"You cannot continue in this direction\n";
    		User_Location_Y--;
    		getch();
    	}
    
    	else if (User_Location_Y == -25)
    	{
    		cout <<"You cannot continue in this direction\n";
    		User_Location_Y++;
    		getch();
    	}
    
    	else if (User_Location_X == 25)
    	{
    		cout <<"You cannot continue in this direction\n";
    		User_Location_X--;
    		getch();
    	}
    
    	else if (User_Location_X == -25)
    	{
    		cout <<"You cannot continue in this direction\n";
    		User_Location_X++;
    	}
    
    	
    	/* This decrements by 1 every loop, when it hits 0 game ends.*/
    	end_game--;
    
    	if (end_game == 0)
    	{
    
    		system("cls");
    		cout <<"You have used all your energy, GAME OVER!\n";
    		return 0;
    	}
    
    	/* This displays every loop.*/
    	system("cls");
    
    	directions();
    	cout <<"Select a Direction: ";
    	cin >>input; 
    	cout <<"\n";
    
    	/* Key for user initiated exit.*/
    	if (input == K_END)
    	{
    		system("cls");
    		return 0;
    	}
    
    	/* Key for information menu.*/
    	if (input == K_HOME)
    	{
    		cout <<"North (1)\nSouth (2)\nEast(3)\nWest (4)\nQuit (Q)\nUse Heal (H)\n";
    		cout <<"Spell (S)\nWeapon (W)\nRun (R)\nHelp (I)\nLife (L)\nPoints (P)\n\n";
    	}
    
    	/* Key for checking health status.*/
    	if (input == K_INSERT)
    	{
    		cout <<"Health is "<<health<<"\n\n";
    	}
    
    	/* Key for checking point status.*/
    	if (input == K_P || input == K_p)
    	{
    		cout <<"Points are "<<points<<"\n\n";
    	}
    
    	/* Update user position and update accordingly.*/	
    	if (input == K_UP)
    	{
    		User_Location_Y = User_Location_Y++;
    	}
    
    	if (input == K_DOWN)
    	{
    		User_Location_Y = User_Location_Y--;
    	}
    
    	if (input == K_RIGHT)
    	{
    		User_Location_X = User_Location_X++;
    	}
    
    	if (input == K_LEFT)
    	{
    		User_Location_X = User_Location_X--;
    	}
    
    
    	/***************************************************************
    	****************************************************************
    	Code for Amulet
    	****************************************************************
    	***************************************************************/
    	if (User_Location_X == 0 && User_Location_Y == 5)
    	{
    		if (Amulet == 1)
    		{
    			cout <<"You have returned to where you found Soris's Amulet.\n\n";
    					
    		}
    
    		else if (Amulet == 0)
    		{
    			cout <<"You found Soris's Amulet!\n\n";
    			cout <<"You feel protected and are awarded ten points.\n\n";
    			Amulet = 1;
    			
    		}
    	}
    
    	/***************************************************************
    	****************************************************************
    	Code for Quarter_Staff
    	****************************************************************
    	***************************************************************/
    	if (User_Location_X == 7 && User_Location_Y == 0)
    	{
    		if (Quarter_Staff == 1)
    		{
    			cout <<"You find the marble pillar on which your quarter staff had been.\n\n";
    			
    		}
    
    		else if (Quarter_Staff == 0)
    		{
    			cout <<"You have aquired a Quarter Staff! You feel secure with your weapon.\n\n";
    			Quarter_Staff = 1;
    			points = points + 10;
    			
    		}
    	}
    
    	/***************************************************************
    	****************************************************************
    	Code for Chainmail_Armor
    	****************************************************************
    	***************************************************************/
    	if (User_Location_X == 0 && User_Location_Y == 17)
    	{
    		if (Chainmail_Armor == 1)
    		{
    			cout <<"You see an old crate where a chainmail armor once rested.\n\n";
    		}
    
    		else if (Chainmail_Armor == 0)
    		{
    			cout <<"You have found an old set of Chainmail Armor! It appears in good shape.\n\n";
    			int user_choice;
    			cout <<"Equip Y/N? ";
    			cin >>user_choice;
    
    			if (user_choice == 'Y' || user_choice == 'y')
    			{
    				cout <<"You equip the chainmail.\n\n";
    				Chainmail_Armor = 1;
    				protection++;
    			}
    
    			if (user_choice == 'N' || user_choice == 'n')
    			{
    				cout <<"You drop the chainmail.\n\n";
    				Chainmail_Armor = 0;
    			}
    		}
    	}
    
    	/***************************************************************
    	****************************************************************
    	Code for Heal_Potion
    	****************************************************************
    	***************************************************************/
    	if (User_Location_X == -7 && User_Location_Y == 0)
    	{
    		if (Heal_Potion == 1 || Heal_Potion == 2)
    		{
    			cout <<"You have stumbled upon an empty bag, once containing a heal potion.\n\n";
    		}
    
    		else if (Heal_Potion == 0)
    		{
    			cout <<"You have uncoverd a bag, in it contains a heal potion, which you pick up.\n\n";
    			cout <<"You may use this at anytime by pressing (H).\n\n";
    			Heal_Potion = 1;
    			points = points + 10;
    		}
    	}
    
    	/***************************************************************
    	****************************************************************
    	Code for Heal_Potion Use.
    	****************************************************************
    	***************************************************************/
    
    	if (input == 'H' || input == 'h')
    	{
    		if (Heal_Potion == 1)
    		{
    			cout <<"You drink the potion.\n\n";
    			Heal_Potion = 2;
    			health = health + 5;
    		}
    
    		else if (Heal_Potion == 0)
    		{
    			cout <<"You don't have a health potion.\n\n";
    		}
    
    		else if (Heal_Potion == 2)
    		{
    			cout <<"You already used your potion.\n\n";
    		}
    	}
    	
    	/***************************************************************
    	****************************************************************
    	Code for Spell_Book
    	****************************************************************
    	***************************************************************/
    	if (User_Location_X == 0 && User_Location_Y == -9)
    	{
    		if (Spell_Book == 1)
    		{
    			cout <<"You feel enchanted as you find where a spell book once sat.\n\n";
    		}
    
    		else if (Spell_Book == 0)
    		{
    			cout <<"You find an enchanted spell book!\n\n";
    			points = points + 10;
    			Spell_Book = 1;
    		}
    	}
    
    	/***************************************************************
    	****************************************************************
    	Code for Monster one
    	****************************************************************
    	***************************************************************/
    	if (User_Location_X == 2)
    	{
    		cout <<"You encounter a troll!\n\n";
    		cout <<"Spell (S)\nWeapon (W)\nRun (R)\n\n";
    		cin >>input;
    
    		if (input == 'S' || input == 's')
    		{
    			if (magic <= 1)
    			{
    				cout <<"You have no magic ability left!\n\n";
    				cout <<"The troll strikes you! You lose 10 health points!\n\n";
    				health = health - 10;
    				cout <<"Points are "<<points<<"\n\n";
    				cout <<"Health is "<<health<<"\n\n";
    			}
    
    			else if (magic >= 5)
    			{
    			cout <<"You cast a spell, but the troll is immune!\n\n";
    			cout <<"The troll strikes you! You lose 10 health points!\n\n";
    			health = health - 10;
    			magic = magic - 50;
    			cout <<"Points are "<<points<<"\n\n";
    			cout <<"Health is "<<health<<"\n\n";
    			}
    		}
    
    		else if (input == 'W' || input == 'w')
    		{
    			cout <<"You strike and kill the troll!\n\n";
    			cout <<"You gain 20 points!\n\n";
    			points = points + 20;
    			cout <<"Points are "<<points<<"\n\n";
    			cout <<"Health is "<<health<<"\n\n";
    		}
    
    		else if (input == 'R' || input == 'r')
    		{
    			cout <<"You try to escape....you escape but the troll hits you.\n\n";
    			cout <<"You lose 20 health points\n\n";
    			health = health - 20;
    			cout <<"Points are "<<points<<"\n\n";
    			cout <<"Health is "<<health<<"\n\n";
    		}
    	}
    
    	/***************************************************************
    	****************************************************************
    	Code for Monster two
    	****************************************************************
    	***************************************************************/
    	if (User_Location_Y == -4)
    	{
    		cout <<"You encounter a wombat!\n\n";
    		cout <<"Spell (S)\nWeapon (W)\nRun (R)\n\n";
    		cin >>input;
    
    		if (input == 'S' || input == 's')
    		{
    			if (magic <= 1)
    			{
    				cout <<"You have no magic ability left!\n\n";
    				cout <<"The troll strikes you! You lose 10 health points!\n\n";
    				health = health - 10;
    				cout <<"Points are "<<points<<"\n\n";
    				cout <<"Health is "<<health<<"\n\n";
    			}
    
    			else if (magic >= 5)
    			{
    				cout <<"You cast a spell, but the wombat is immune!\n\n";
    				cout <<"The wombat strikes you! You lose 20 health points!\n\n";
    				health = health - 20;
    				magic = magic - 50;
    				cout <<"Points are "<<points<<"\n\n";
    				cout <<"Health is "<<health<<"\n\n";
    			}
    		}
    
    		else if (input == 'W' || input == 'w')
    		{
    			cout <<"You strike and kill the wombat!\n\n";
    			cout <<"You gain 50 points!\n\n";
    			points = points + 50;
    			cout <<"Points are "<<points<<"\n\n";
    			cout <<"Health is "<<health<<"\n\n";
    		}
    
    		else if (input == 'R' || input == 'r')
    		{
    			cout <<"You try to escape....you escape but the wombat hits you.\n\n";
    			cout <<"You lose 20 health points\n\n";
    			health = health - 20;
    			cout <<"Points are "<<points<<"\n\n";
    			cout <<"Health is "<<health<<"\n\n";
    		}
    	}
    
    	/***************************************************************
    	****************************************************************
    	Code for Monster three - impossible to beat
    	****************************************************************
    	***************************************************************/
    	if (User_Location_X == -11)
    	{
    		cout <<"You encounter a powerful Ghost!\n\n";
    		cout <<"Spell (S)\nWeapon (W)\nRun (R)\n\n";
    		cin >>input;
    
    		if (input == 'S' || input == 's')
    		{
    			cout <<"You cast a spell, but the ghost resists!!\n\n";
    			cout <<"The ghost strikes you! You lose 100 health points!\n\n";
    			health = 0;
    			cout <<"Points are "<<points<<"\n\n";
    			cout <<"Health is "<<health<<"\n\n";
    		}
    
    		else if (input == 'W' || input == 'w')
    		{
    			cout <<"You try to hit the ghost, but miss!\n\n";
    			points = points - 20;
    			cout <<"Points are "<<points<<"\n\n";
    			cout <<"Health is "<<health<<"\n\n";
    		}
    
    		else if (input == 'R' || input == 'r')
    		{
    			cout <<"You try to escape....you escape but the ghost hits you.\n\n";
    			cout <<"You lose 100 health points\n\n";
    			health = 0;
    			cout <<"Points are "<<points<<"\n\n";
    			cout <<"Health is "<<health<<"\n\n";
    		}
    	}
    
    
    	}
    	/* Loop does these actions while end_game 
    	does NOT qual true.*/
    	while(end_game!=1);
         
    
    	/* Return value for main.*/
    	return 0;
    }

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Hmm, why do you need to post all your code for something that small. It's just a matter of chnaging some evaluation values, as lightatdawn said.
    Its not that because the K_UP n such are defined to they numeric value in the .h file.

    Thnx BMJ i'll try that.

  10. #10
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    ok, well he still doesn't need to post all his code.
    Simple man, i said i didn't know what was wrong and it was my first time, i didn't know where the error was and rather then having to post more code later i gave it all at once. Deal man, deal.

    Thnx BMJ i think that did the trick.

  11. #11
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i added the VK code as indicated above, and got these errors:

    Compiling...
    Caverns.cpp
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(92) : error C2065: 'STD_IN' : undeclared identifier
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(92) : error C2146: syntax error : missing ')' before identifier 'PUT_HANDLE'
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(92) : error C2059: syntax error : ')'
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(105) : warning C4244: '=' : conversion from 'unsigned short' to 'char', possible loss of data
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(107) : error C2065: 'STD_INPUT_' : undeclared identifier
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(107) : error C2146: syntax error : missing ')' before identifier 'HANDLE'
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(107) : error C2059: syntax error : ')'
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(224) : error C2065: 'VK_P' : undeclared identifier
    D:\MyProjects\Game related 1\Caverns\Caverns.cpp(224) : error C2065: 'VK_p' : undeclared identifier
    Error executing cl.exe.

    Caverns.obj - 8 error(s), 1 warning(s)


    heres the code from beginning to end of section thats in trouble:

    Code:
    /*	Steven Billington 
    	[email protected]
    	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 <iomanip.h>
    #include <windows.h>
    #include "keys.h"
    #include <iostream.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_Location_X = 0;
    int User_Location_Y = 0;
    int Amulet = 0;
    int Quarter_Staff = 0;
    int Chainmail_Armor = 0;
    int Heal_Potion = 0;
    int Spell_Book = 0;
    int magic = 100;
    int points = 0;
    int health = 100;
    int protection = 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";
    	
    
    	/*Take user input and display opening messages
    	The user name can be up to 20 characters long*/
    	char name[20];
    
    	cout <<"Please enter your first name and press enter twice: ";
    	cin >>name; /* Store users name*/
    
    	cout <<"You may see controls at any time by pressing (HOME)\n\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";
    }
    
    
    bool Keypress(char &Key)
    {
    	INPUT_RECORD Event;
    	DWORD NumberOfEvents, EventsRead, EventCounter;
    
     	GetNumberOfConsoleInputEvents(GetStdHandle(STD_IN
    PUT_HANDLE), &NumberOfEvents);
    
    	if (NumberOfEvents == 0)
    		return false;
    	
    	for (EventCounter = 0; EventCounter < NumberOfEvents; EventCounter++)
    	{
     		PeekConsoleInput(GetStdHandle(STD_INPUT_HANDLE),
     &Event, 1, &EventsRead);
    		if ((Event.EventType == KEY_EVENT) && ((Event.Event.KeyEvent.bKeyDown)))
    		{
     			ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE)
    , &Event, 1, &EventsRead);
    			Key = Event.Event.KeyEvent.wVirtualKeyCode;
    			if  (!(FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_
    HANDLE))))
    				exit(0);
    			return true;
    		}
    		else
     			ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE)
    , &Event, 1, &EventsRead);
    	}
    
    	return false;
    }
    
    
    int main ()
    {
    	openmessage(); /* Call opening message*/
    
    	getch();
    
    
    
    	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;
    	}
    
    	
    	
    	
    	
    	/* Check user position.*/
    	if (User_Location_Y == 25)
    	{
    		cout <<"You cannot continue in this direction\n";
    		User_Location_Y--;
    		getch();
    	}
    
    	else if (User_Location_Y == -25)
    	{
    		cout <<"You cannot continue in this direction\n";
    		User_Location_Y++;
    		getch();
    	}
    
    	else if (User_Location_X == 25)
    	{
    		cout <<"You cannot continue in this direction\n";
    		User_Location_X--;
    		getch();
    	}
    
    	else if (User_Location_X == -25)
    	{
    		cout <<"You cannot continue in this direction\n";
    		User_Location_X++;
    	}
    
    	
    	/* This decrements by 1 every loop, when it hits 0 game ends.*/
    	end_game--;
    
    	if (end_game == 0)
    	{
    
    		system("cls");
    		cout <<"You have used all your energy, GAME OVER!\n";
    		return 0;
    	}
    
    	/* This displays every loop.*/
    	system("cls");
    
    	directions();
    	cout <<"Select a Direction: ";
    	cin >>input; 
    	cout <<"\n";
    
    	char key = 0;
    	for (;;)
    	{	
    
    	/* Key for user initiated exit.*/
    	if (Keypress(key))
    		{
    			if (key == VK_LEFT)
    				User_Location_X = 	User_Location_X--;
    			if (key == VK_RIGHT)
    				User_Location_X = 	User_Location_X++;
    			if (key == VK_DOWN)
    				User_Location_Y = 	User_Location_Y--;
    			if (key == VK_UP)
    				User_Location_Y = 	User_Location_Y++;
    			if (key == VK_ESCAPE)
    				return 0;
    			if (key == VK_HOME)
    				directions();
    			if (key == VK_INSERT)
    				cout <<"Health is: "<<health<<"\n";
    			if (key == VK_P || key == VK_p)
    				cout <<"Points are: "<<points<<"\n";
    		}
    	}

  12. #12
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    man i am off today bmj lol, earlier i made a new thread cause my open message function wasn't working, here i had forgot to call it! wow i need sleep for once i guess.

    Thnx for the code..


    I can't use the number pad directionals, they are going to serve another purpose.

  13. #13
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    When i push up it doens't work, but errors are gone. I think it has to do with how i am taking input. I used to use letters, heres the section of the code, where u can c at the top how it takes it in (it needs enter pushed it needs to be automatic)

    Code:
    /* This displays every loop.*/
    	system("cls");
    
    	directions();
    	cout <<"Select a Direction: ";
    	cin >>input; 
    	cout <<"\n";
    
    	char key = 0;
    	for (;;)
    	{	
    
    	/* Key for user initiated exit.*/
    	if (Keypress(key))
    		{
    			if (key == VK_LEFT)
    				User_Location_X = 	User_Location_X--;
    			if (key == VK_RIGHT)
    				User_Location_X = 	User_Location_X++;
    			if (key == VK_DOWN)
    				User_Location_Y = 	User_Location_Y--;
    			if (key == VK_UP)
    				User_Location_Y = 	User_Location_Y++;
    			if (key == VK_ESCAPE)
    				return 0;
    			if (key == VK_HOME)
    				directions();
    			if (key == VK_INSERT)
    				cout <<"Health is: "<<health<<"\n";
    		}
    	}

  14. #14
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i just realized it is taking NO input from the keyboard, AT ALL.

    hmm...

  15. #15
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. directional keys @ console
    By ipe in forum C Programming
    Replies: 1
    Last Post: 03-13-2003, 06:25 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. Arrow keys in console?
    By SyntaxBubble in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2002, 06:12 PM