i just realized it is taking NO input from the keyboard, AT ALL.
hmm...
i just realized it is taking NO input from the keyboard, AT ALL.
hmm...
hmmm what?
You can do it!
There, does that help?
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.
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.
That should work. You can then use baron's code. Hope that helped!Code:int GetKey() { cin.ignore(1,'\n'); return getch(); }
Brendan
Draco dormiens nunquam titallandus.
Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html
Usually i can get things i do wrong like "that", but i have never done this before i am so stuck, i tried harry p, nadda: I will edit this post with a link to dload the program so u see what i mean.
edit:// www.nelie.org/steveprog/Caverns.zip
Code:int GetKey() { cin.ignore(1,'\n'); return getch(); } int main () { openmessage(); /* Call opening message*/ /* Disable cursor in console SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE ), ENABLE_LINE_INPUT);*/ do/* Start loop*/ { /* Checks to see if the points are at 100 if they are, user wins and game ends.*/ if (points == 100) { cout <<"You have reached 100 points, you win!!\n\n"; getch(); return 0; } /* Checks to see if health is at a value of 0 if it is the user has lost and game ends.*/ if (health == 0) { cout <<"Game Over!\n\n"; getch(); return 0; } /* This displays every loop.*/ directions(); cout <<"Select Direction: "; GetKey(); if (input == K_UP) { User_Location_Y = User_Location_Y++; } if (input == K_DOWN) { User_Location_Y = User_Location_Y--; } if (input == K_LEFT) { User_Location_X = User_Location_X--; } if (input == K_RIGHT) { User_Location_X = User_Location_X++; } if (input == K_END) { return 0; }
Last edited by RoD; 10-05-2002 at 07:33 AM.
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?
I don't know about your multiple .cpp files problem, but this is actually what the GetKey() function looks like:
Then, in my game loop, I just called that function at the beginning of the loop with a variable.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; }
If that doesn't work, then I'm as stumped as you!Code:int key; ... for(;;) { key = GetKey(); ... switch (key) { case K_UP: ... case K_DOWN: ... case K_LEFT: ... case K_RIGHT: ... }
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
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.
I got a little sumthing now. Heres the little code it has now, as well as the output i get when i hit any given key in the console.
Its only accepting these keys and its not working right at all, i am so damn lost rite now its ........ing me off.
***************************Code:/* Steven Billington Silent_Death17@hotmail.com Caverns.cpp September 27, 2002 This is my first game ever. Its a relativley simple program as it should be cause its newbie made. Its a console based text RPG. Welcome to Caverns, enjoy! */ /**************************** ***************************** Updated September 28, 2002 ***************************** ****************************/ /* These are our Preprocessor directives*/ #include <iostream.h> #include <windows.h> #include <iomanip.h> /* For input/output*/ #include <stdlib.h> /* For clear screen*/ #include <conio.h> /* For getch() - makes program pause till user user presses a key*/ /* Declare variables for game-play. The names should explain it, email me if you don't understand something.*/ int User_X = 0; int User_Y = 0; int end_game = 50; /* Our variable for users choices throught game play.*/ char input; /* Declare main as variable type integer.*/ void openmessage () { cout <<setw(50) <<"***************************"<<"\n"; cout <<setw(50) <<"**** Caverns ***"<<"\n"; cout <<setw(50) <<"**** Steven Billington ***"<<"\n"; cout <<setw(50) <<"***************************"<<"\n"; } void directions () { cout <<"North = Up Arrow\nSouth = Down Arrow\nWest = Left Arrow\nEast = Right Arrow\n"; cout <<"\n"; cout <<"Home = Information\n"; cout <<"\n"; cout <<"Insert = Health\nP = Points\n"; cout <<"\n"; cout <<"End = Exit\n"; cout <<"\n"; } // gets a key int main () { openmessage(); /* Call opening message*/ cout <<"\n"; directions(); getch(); do { if (User_X == 6) { cout << "You can not continue in this direction.\n"; User_X = User_X--; getch(); } else if(User_X == -6) { cout << "You can not continue in this direction.\n"; User_X = User_X++; getch(); } else if(User_Y == 6) { cout << "You can not continue in this direction.\n"; User_Y = User_Y--; getch(); } else if(User_Y == -6) { cout << "You can not continue in this direction.\n"; User_Y = User_Y++; getch(); } //------------------------------------------------------------------------- // Game over if 100 turns expire //------------------------------------------------------------------------- end_game = end_game--; //Game_Over is reduced by 1 each loop when if(end_game == 0) //it reaches 0 the game ends { system("cls"); cout << "Game Over."; getch(); return 0; //This teminates the program } input = getch(); cout <<"Select Action: "; getch(); switch(input) { case VK_UP: cout << "You venture north.\n\n"; User_Y = User_Y++; break; //Include break on the case VK_RIGHT: cout << "You venture east.\n\n"; //same line saves space User_X = User_X++; break; //most people say its case VK_DOWN: cout << "You venture south.\n\n"; //best to put it on a User_Y = User_Y--; break; //new line but this code case VK_LEFT: cout << "You venture west.\n\n"; //is readable enough User_X = User_X--; break; } } while(end_game!=1); return 0; }
Heres the output now
***************************
Code:*************************** **** Caverns *** **** Steven Billington *** *************************** North = Up Arrow South = Down Arrow West = Left Arrow East = Right Arrow Home = Information Insert = Health P = Points End = Exit Select Action: Select Action: Select Action: Select Action: Select Action: Selec t Action: Select Action: Select Action: Select Action: Select Action: Select Act ion: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Selec t Action: Select Action: Select Action: Select Action: Select Action: Select Act ion: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Select Action: Selec t Action: Select Action: Select Action: Select Action: Select Action: Select Act ion: Select Action: Select Action: Select Act
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 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;
Last edited by Eibro; 10-05-2002 at 03:11 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:
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
I don't see why this is so hard...PHP Code:
/* Steven Billington
[email]Silent_Death17@hotmail.com[/email]
Caverns.cpp
September 27, 2002
This is my first game ever. Its a relativley simple program
as it should be cause its newbie made. Its a console based text RPG.
Welcome to Caverns, enjoy!
*/
/****************************
*****************************
Updated September 28, 2002
*****************************
****************************/
/* These are our Preprocessor directives*/
#include <iostream.h>
#include <windows.h>
#include <iomanip.h> /* For input/output*/
#include <stdlib.h> /* For clear screen*/
#include <conio.h> /* For getch() - makes program pause till user
user presses a key*/
/* Declare variables for game-play.
The names should explain it, email me
if you don't understand something.*/
int User_X = 0;
int User_Y = 0;
int end_game = 50;
/* Our variable for users choices throught
game play.*/
char input;
/* Keypress function uses the Windows message queue to check the console input buffer
for keypresses... if a key has been pressed while in the console, the function will
return true, and Key will become the virtual key code of the key that was pressed
and the event will be removed from the queue. */
bool Keypress(char &Key)
{
INPUT_RECORD Event;
DWORD 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;
}
/* 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;
}
And then you don't need getch() to pause anymore; Use Windows to your advantage:PHP Code:
void Pause()
{
while (!Keypress(input)) {};
}
How did you do that code formatting.. I mean that colours etc.. You didn't format them YOURSELF did you???
what does signature stand for?
By the way.. I have the same problem using the direction keys as well... I tried GetKeyState() but virtual keys don't work (excluding VK_RETURN) (Console) .. I also tried that getch() but when you press one of the arrows.. it just says p:
Code:int main() { char choice[1]; choice[0] = getch(); cout << choice[0]; return 0; }
what does signature stand for?