FAQ: Directional Keys - Useing in Console [Archive] - C Board

PDA

View Full Version : FAQ: Directional Keys - Useing in Console


RoD
10-04-2002, 04:23 PM
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.


/* 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;
}

BMJ
10-04-2002, 04:51 PM
You need to use Windows' virtual keys :)

lightatdawn
10-04-2002, 04:57 PM
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

BMJ
10-04-2002, 05:19 PM
*sigh*

Example of virtual key usage:#include <windows.h>
#include <iostream>

bool Keypress(char &Key)
{
INPUT_RECORD Event;
DWORD NumberOfEvents, EventsRead, EventCounter;

GetNumberOfConsoleInputEvents(GetStdHandle(STD_INP UT_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;
}

Driveway
10-04-2002, 05:26 PM
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.

BMJ
10-04-2002, 05:27 PM
No, it's not; What I did was totally different - as his subject says 'using directional keys'

RoD
10-04-2002, 05:29 PM
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.

Driveway
10-04-2002, 05:29 PM
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.

BMJ
10-04-2002, 05:33 PM
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.

RoD
10-04-2002, 06:15 PM
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.

RoD
10-04-2002, 06:27 PM
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:


/* 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";
}
}

BMJ
10-04-2002, 06:35 PM
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)

Kirdra
10-04-2002, 08:42 PM
Use the number pad arrows, they can be assigned simply by inserting the relevant number.

RoD
10-04-2002, 09:22 PM
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
10-04-2002, 09:34 PM
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)


/* 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
10-04-2002, 10:03 PM
i just realized it is taking NO input from the keyboard, AT ALL.

hmm...

BMJ
10-04-2002, 10:22 PM
hmmm what?

You can do it!

There, does that help?

RoD
10-04-2002, 10:25 PM
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.

harryP
10-05-2002, 12:02 AM
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.

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

That should work. You can then use baron's code. Hope that helped!

Brendan

RoD
10-05-2002, 08:19 AM
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


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;
}

RoD
10-05-2002, 09:03 AM
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?

harryP
10-05-2002, 11:22 AM
I don't know about your multiple .cpp files problem, but this is actually what the GetKey() function looks like:

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

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

I had to add the ending code tag, I'd forgotten it ;)

RoD
10-05-2002, 12:40 PM
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
10-05-2002, 02:02 PM
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.


/* 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
***************************



***************************
**** 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

Eibro
10-05-2002, 04:04 PM
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:


#define MODIFY_X 0
#define MODIFY_Y 1

#define INCREMENT 1
#define DECREMENT 0

bool move(bool moveWhich, bool 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 = 0; i < 100; i++) // 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_X, DECREMENT))
{
cout << "You cannot continue in that direction!" << endl;
--i;
continue;
} else {
cout << "You venture west" << endl;
// Extra handling;
}
break;
case 's':
if (!move(MODIFY_Y, DECREMENT))
{
cout << "You cannot continue in that direction!" << endl;
--i;
continue;
} else {
cout << "You venture south" << endl;
// Extra handling
}
break;
case 'd':
if (!move(MODIFY_X, INCREMENT))
{
cout << "You cannot continue in that direction!" << endl;
--i;
continue;
} else {
cout << "You venture east" << endl;
// Extra handling
}
break;
case 'w':
if (!move(MODIFY_Y, INCREMENT))
{
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;

harryP
10-05-2002, 06:35 PM
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:

<#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;
}

BMJ
10-05-2002, 10:59 PM
I don't see why this is so hard.../* 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;

/* 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 NumberOfEvents, EventsRead, EventCounter;

GetNumberOfConsoleInputEvents(GetStdHandle(STD_INP UT_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;
}

/* 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_UP: cout << "You venture north.\n\n"; Sleep(500);
User_Y = User_Y++; break; //Include break on the
case VK_RIGHT: cout << "You venture east.\n\n"; Sleep(500); //same line saves space
User_X = User_X++; break; //most people say its
case VK_DOWN: cout << "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_LEFT: cout << "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
10-05-2002, 11:02 PM
And then you don't need getch() to pause anymore; Use Windows to your advantage:void Pause()
{
while (!Keypress(input)) {};
}

Ruski
10-06-2002, 02:42 AM
How did you do that code formatting.. I mean that colours etc.. You didn't format them YOURSELF did you???

Ruski
10-06-2002, 02:45 AM
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:

int main()
{
char choice[1];
choice[0] = getch();
cout << choice[0];
return 0;
}

Ruski
10-06-2002, 02:47 AM
Testing PHP...

int main()
{
cout << "Hello World!";
return 0;
}

Ruski
10-06-2002, 02:48 AM
YEY!! PHP!!.. But the code colours are kinda anti-C++, maybe we should ask an admin to change them... :p

Sang-drax
10-06-2002, 04:58 AM
Originally posted by BMJ

end_game = end_game--; //Game_Over is reduced by 1



Change this!!

end_game--;

RoD
10-06-2002, 02:13 PM
thnx bmj, i been under alot of stress maybe thats why it was so hard.

Inquirer
10-06-2002, 02:22 PM
Yes. I agree with sangdrax. you need to change the way you increment and decrement. i gor teally woird things coming out of my early progs when i would use x = x++. Use x++ by itself, or x += 1. (except with your variables in there.)

RoD
10-06-2002, 02:35 PM
yea i have been scince i posted that code i went back and changed it, thnx for pointing it out anyway tho.

Divx
10-06-2002, 02:37 PM
The virtual key codes are found in winuser.h (which including windows.h will include)

Here are a couple of macros you can use to check the state of a key.

KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

Using this method relieves the problem of having to get key input from the standard input.

Check out the win32 console API for more neat ideas.

If for whatever reason they VK codes don't seem to be defined here you go:
#define VK_LBUTTON 0x01 // Left mouse button
#define VK_RBUTTON 0x02 // Right mouse button
#define VK_CANCEL 0x03 // Control-break processing
#define VK_MBUTTON 0x04 // Middle mouse button (three-button mouse)

#define VK_BACK 0x08 // backspace key
#define VK_TAB 0x09 // tab key

#define VK_CLEAR 0x0C // clear key
#define VK_RETURN 0x0D // enter key

#define VK_SHIFT 0x10 // shift key
#define VK_CONTROL 0x11 // ctrl key
#define VK_MENU 0x12 // alt key
#define VK_PAUSE 0x13 // pause key
#define VK_CAPITAL 0x14 // caps lock key

#define VK_ESCAPE 0x1B // esc key
#define VK_SPACE 0x20 // spacebar
#define VK_PRIOR 0x21 // page up key
#define VK_NEXT 0x22 // page down key
#define VK_END 0x23 // end key
#define VK_HOME 0x24 // home key
#define VK_LEFT 0x25 // left arrow key
#define VK_UP 0x26 // up arrow key
#define VK_RIGHT 0x27 // right arrow key
#define VK_DOWN 0x28 // down arrow key
#define VK_SELECT 0x29 // select key
#define VK_EXECUTE 0x2B // execute key
#define VK_SNAPSHOT 0x2C // print screen key
#define VK_INSERT 0x2D // ins key
#define VK_DELETE 0x2E // del key
#define VK_HELP 0x2F // help key
#define VK_0 0x30 // 0 key
#define VK_1 0x31 // 1 key
#define VK_2 0x32 // 2 key
#define VK_3 0x33 // 3 key
#define VK_4 0x34 // 4 key
#define VK_5 0x35 // 5 key
#define VK_6 0x36 // 6 key
#define VK_7 0x37 // 7 key
#define VK_8 0x38 // 8 key
#define VK_9 0x39 // 9 key

#define VK_A 0x41 // a key
#define VK_B 0x42 // b key
#define VK_C 0x43 // c key
#define VK_D 0x44 // d key
#define VK_E 0x45 // e key
#define VK_F 0x46 // f key
#define VK_G 0x47 // g key
#define VK_H 0x48 // h key
#define VK_I 0x49 // i key
#define VK_J 0x4A // j key
#define VK_K 0x4B // k key
#define VK_L 0x4C // l key
#define VK_M 0x4D // m key
#define VK_N 0x4E // n key
#define VK_O 0x4F // o key
#define VK_P 0x50 // p key
#define VK_Q 0x51 // q key
#define VK_R 0x52 // r key
#define VK_S 0x53 // s key
#define VK_T 0x54 // t key
#define VK_U 0x55 // u key
#define VK_V 0x56 // v key
#define VK_W 0x57 // w key
#define VK_X 0x58 // x key
#define VK_Y 0x59 // y key
#define VK_Z 0x5A // z key
#define VK_LWIN 0x5B // Left Windows key (Microsoft Natural Keyboard)
#define VK_RWIN 0x5C // Right Windows key (Microsoft Natural Keyboard)
#define VK_APPS 0x5D // Applications key (Microsoft Natural Keyboard)

#define VK_NUMPAD0 0x60 // Numeric keypad 0 key
#define VK_NUMPAD1 0x61 // Numeric keypad 1 key
#define VK_NUMPAD2 0x62 // Numeric keypad 2 key
#define VK_NUMPAD3 0x63 // Numeric keypad 3 key
#define VK_NUMPAD4 0x64 // Numeric keypad 4 key
#define VK_NUMPAD5 0x65 // Numeric keypad 5 key
#define VK_NUMPAD6 0x66 // Numeric keypad 6 key
#define VK_NUMPAD7 0x67 // Numeric keypad 7 key
#define VK_NUMPAD8 0x68 // Numeric keypad 8 key
#define VK_NUMPAD9 0x69 // Numeric keypad 9 key
#define VK_MULTIPLY 0x6A // Multiply key
#define VK_ADD 0x6B // Add key
#define VK_SEPARATOR 0x6C // Separator key
#define VK_SUBTRACT 0x6D // Subtract key
#define VK_DECIMAL 0x6E // Decimal key
#define VK_DIVIDE 0x6F // Divide key
#define VK_F1 0x70 // f1 key
#define VK_F2 0x71 // f2 key
#define VK_F3 0x72 // f3 key
#define VK_F4 0x73 // f4 key
#define VK_F5 0x74 // f5 key
#define VK_F6 0x75 // f6 key
#define VK_F7 0x76 // f7 key
#define VK_F8 0x77 // f8 key
#define VK_F9 0x78 // f9 key
#define VK_F10 0x79 // f10 key
#define VK_F11 0x7A // f11 key
#define VK_F12 0x7B // f12 key
#define VK_F13 0x7C // f13 key
#define VK_F14 0x7D // f14 key
#define VK_F15 0x7E // f15 key
#define VK_F16 0x7F // f16 key
#define VK_F17 0x80H // f17 key
#define VK_F18 0x81H // f18 key
#define VK_F19 0x82H // f19 key
#define VK_F20 0x83H // f20 key
#define VK_F21 0x84H // f21 key
#define VK_F22 0x85H // f22 key
#define VK_F23 0x86H // f23 key
#define VK_F24 0x87H // f24 key

#define VK_NUMLOCK 0x90 // num lock key
#define VK_SCROLL 0x91 // scroll lock key

#define VK_ATTN 0xF6 // Attn key
#define VK_CRSEL 0xF7 // CrSel key
#define VK_EXSEL 0xF8 // ExSel key
#define VK_EREOF 0xF9 // Erase EOF key
#define VK_PLAY 0xFA // Play key
#define VK_ZOOM 0xFB // Zoom key
#define VK_NONAME 0xFC // Reserved for future use.
#define VK_PA1 0xFD // PA1 key
#define VK_OEM_CLEAR 0xFE // Clear key

RoD
10-06-2002, 05:18 PM
wow divx u just totally lost me. My problem isn't really getting it to recognize keys being hit, that it does. After i hit up 14 billion times to get the prog going, which interns ventures me north then ouputs half of what it should then does it again. It should come up, tell me to pick a direction, then i hit say up and it goes once. It has to be something with my loop, heres the crazy ass output:

NOTE: i didn't cut off, this is right outta the console.


***************************
**** 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: You venture north.

Select Action: You venture north.

Select Action: You venture north.

Select Action: You venture north.

Select Action: You venture north.

Select Action: You venture north.

You can not continue in this direction.
Select Action: You venture north.

You can not continue in this direction.
Select Action: You venture north.

You can not continue in this direction.
Select Action: You venture north.

You can not continue in this direction.
Select Action: You venture north.

You can not continue in this direction.
Select Action: You venture east.

Select Action: You venture east.

Select Action: You venture east.

Select Action: You venture east.

Select Action: You venture west.

Select Action: You venture west.

Select Action: You venture west.

Select Action: You venture west.

Select Action: You venture west.

Select Action: Not an option!

Select Action: Not an option!

Select Action: You venture west.

Select Action: You venture west.

Select Action: You venture west.

Select Action: You venture west.

Select Action: You venture west.

You can not continue in this direction.
Select Action: Not an option!

Select Action: You venture west.

You




Now this would normally be right if it didn't take 40 thousand key hits to react, and didn't cut off text...

Divx
10-06-2002, 05:42 PM
if (KEY_DOWN(vk_code))
{
while (!KEY_UP(vk_code))
{
// do action while key is down and not up
}
// do whatever when key is let go

// Suggest doing this to flush the standard input stream buffer
FlushConsoleInputBuffer (GetStdHandle(STD_INPUT_HANDLE));
}


Use that, hit it once and it'll execute once and won't continue executing (unless you want it to).