![]() |
| | #1 |
| Banned Join Date: Sep 2002
Posts: 6,334
| FAQ: Directional Keys - Useing in Console 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 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 <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; } |
| RoD is offline |
| | #2 |
| Registered User Join Date: Aug 2002
Posts: 1,330
| You need to use Windows' virtual keys |
| BMJ is offline |
| | #3 |
| Peace Join Date: Aug 2001
Posts: 1,512
| Theres no way I'm reading all that but try this: if (input == 77) ... //Right if (input == 75) ... //Left if (input == 80) ... //Down if (input == 72) ... //Up |
| lightatdawn is offline |
| | #4 |
| Registered User Join Date: Aug 2002
Posts: 1,330
| *sigh* Example of virtual key usage: Code: #include <windows.h>
#include <iostream>
bool Keypress(char &Key)
{
INPUT_RECORD Event;
DWORD NumberOfEvents, EventsRead, EventCounter;
GetNumberOfConsoleInputEvents(GetStdHandle(STD_INPUT_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()
{
char key = 0;
for (;;)
{
if (Keypress(key))
{
if (key == VK_LEFT)
std::cout << "Left\n";
if (key == VK_RIGHT)
std::cout << "Right\n";
if (key == VK_DOWN)
std::cout << "Down\n";
if (key == VK_UP)
std::cout << "Up\n";
if (key == VK_ESCAPE)
return 0;
}
}
return 0;
}
Last edited by BMJ; 10-04-2002 at 04:33 PM. |
| BMJ is offline |
| | #5 |
| . Join Date: May 2002
Posts: 469
| 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. |
| Driveway is offline |
| | #6 |
| Registered User Join Date: Aug 2002
Posts: 1,330
| No, it's not; What I did was totally different - as his subject says 'using directional keys' Last edited by BMJ; 10-04-2002 at 04:31 PM. |
| BMJ is offline |
| | #7 | |
| Banned Join Date: Sep 2002
Posts: 6,334
| Quote:
Thnx BMJ i'll try that. | |
| RoD is offline |
| | #8 |
| . Join Date: May 2002
Posts: 469
| ok, well he still doesn't need to post all his code. And why use the arrow keys? That feels to cramped and is too far away from the other keys. |
| Driveway is offline |
| | #9 |
| Registered User Join Date: Aug 2002
Posts: 1,330
| Well for a game, keypress catching is much nicer than standard inputting; who wants to type "left" and press enter when you can just press the left arrow and have the action take place. You don't even have to know how the function KeyPress works, just how to use it, which is simple. EDIT: Driveway - whatever. Last edited by BMJ; 10-04-2002 at 04:36 PM. |
| BMJ is offline |
| | #10 | |
| Banned Join Date: Sep 2002
Posts: 6,334
| Quote:
Thnx BMJ i think that did the trick. | |
| RoD is offline |
| | #11 |
| Banned Join Date: Sep 2002
Posts: 6,334
| 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 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 <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"; } } |
| RoD is offline |
| | #12 |
| Registered User Join Date: Aug 2002
Posts: 1,330
| LOL! Dude, you copied the code above which got cut off! Copy it from this... (btw: don't forget to add a way to get out of the infinite for loop, like with escape) |
| BMJ is offline |
| | #13 |
| Registered User Join Date: Aug 2002
Posts: 102
| Use the number pad arrows, they can be assigned simply by inserting the relevant number. |
| Kirdra is offline |
| | #14 |
| Banned Join Date: Sep 2002
Posts: 6,334
| 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. |
| RoD is offline |
| | #15 |
| Banned Join Date: Sep 2002
Posts: 6,334
| 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";
}
}
|
| RoD is offline |
| Thread Tools | |
| Display Modes | |
|
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 |