![]() |
| | #16 |
| Banned Join Date: Sep 2002
Posts: 6,334
| hmm... |
| RoD is offline |
| | #17 |
| Registered User Join Date: Aug 2002
Posts: 1,330
| hmmm what? You can do it! There, does that help? |
| BMJ is offline |
| | #18 |
| Banned Join Date: Sep 2002
Posts: 6,334
| haha no :P I'm tired and getting off ina bit, i'll take a crack at it in the morning, cause i know its right in front of me. |
| RoD is offline |
| | #19 |
| Registered User 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();
}
Brendan
__________________ Draco dormiens nunquam titallandus. Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html |
| harryP is offline |
| | #20 |
| Banned Join Date: Sep 2002
Posts: 6,334
| Usually i can get things i do wrong like "that", but i have never done this before i am so stuck, i tried harry p, nadda: I will edit this post with a link to dload the program so u see what i mean. edit:// www.nelie.org/steveprog/Caverns.zip Code: int GetKey()
{
cin.ignore(1,'\n');
return getch();
}
int main ()
{
openmessage(); /* Call opening message*/
/* Disable cursor in console
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE
), ENABLE_LINE_INPUT);*/
do/* Start loop*/
{
/* Checks to see if the points are at 100
if they are, user wins and game ends.*/
if (points == 100)
{
cout <<"You have reached 100 points, you win!!\n\n";
getch();
return 0;
}
/* Checks to see if health is at a value of 0
if it is the user has lost and game ends.*/
if (health == 0)
{
cout <<"Game Over!\n\n";
getch();
return 0;
}
/* This displays every loop.*/
directions();
cout <<"Select Direction: ";
GetKey();
if (input == K_UP)
{
User_Location_Y = User_Location_Y++;
}
if (input == K_DOWN)
{
User_Location_Y = User_Location_Y--;
}
if (input == K_LEFT)
{
User_Location_X = User_Location_X--;
}
if (input == K_RIGHT)
{
User_Location_X = User_Location_X++;
}
if (input == K_END)
{
return 0;
}
Last edited by RoD; 10-05-2002 at 07:33 AM. |
| RoD is offline |
| | #21 |
| Banned Join Date: Sep 2002
Posts: 6,334
| Also i am noticing my code is getting sloppy and such as i expand it, so i want to break it into several .cpp's. So lets say i have Openmsg.cpp or Directions.cpp instead of having the code for em in the main.cpp, how would i call them to display or be executed at a certain point? Example: i have a cpp that needs to be executed after one point and before another that are in the main.cpp, how do i call it to execute? |
| RoD is offline |
| | #22 |
| Registered User 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;
}
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 |
| harryP is offline |
| | #23 |
| Banned Join Date: Sep 2002
Posts: 6,334
| hmm it was kinda owrking, but i had to hit the key like 700 times first grr, i'm wiping out all the code its such a mess, let me see if i can start clean and get it working. |
| RoD is offline |
| | #24 |
| Banned Join Date: Sep 2002
Posts: 6,334
| I got a little sumthing now. Heres the little code it has now, as well as the output i get when i hit any given key in the console. Its only accepting these keys and its not working right at all, i am so damn lost rite now its ........ing me off. Code: /* Steven Billington Silent_Death17@hotmail.com Caverns.cpp September 27, 2002 This is my first game ever. Its a relativley simple program as it should be cause its newbie made. Its a console based text RPG. Welcome to Caverns, enjoy! */ /**************************** ***************************** Updated September 28, 2002 ***************************** ****************************/ /* These are our Preprocessor directives*/ #include <iostream.h> #include <windows.h> #include <iomanip.h> /* For input/output*/ #include <stdlib.h> /* For clear screen*/ #include <conio.h> /* For getch() - makes program pause till user user presses a key*/ /* Declare variables for game-play. The names should explain it, email me if you don't understand something.*/ int User_X = 0; int User_Y = 0; int end_game = 50; /* Our variable for users choices throught game play.*/ char input; /* Declare main as variable type integer.*/ void openmessage () { cout <<setw(50) <<"***************************"<<"\n"; cout <<setw(50) <<"**** Caverns ***"<<"\n"; cout <<setw(50) <<"**** Steven Billington ***"<<"\n"; cout <<setw(50) <<"***************************"<<"\n"; } void directions () { cout <<"North = Up Arrow\nSouth = Down Arrow\nWest = Left Arrow\nEast = Right Arrow\n"; cout <<"\n"; cout <<"Home = Information\n"; cout <<"\n"; cout <<"Insert = Health\nP = Points\n"; cout <<"\n"; cout <<"End = Exit\n"; cout <<"\n"; } // gets a key int main () { openmessage(); /* Call opening message*/ cout <<"\n"; directions(); getch(); do { if (User_X == 6) { cout << "You can not continue in this direction.\n"; User_X = User_X--; getch(); } else if(User_X == -6) { cout << "You can not continue in this direction.\n"; User_X = User_X++; getch(); } else if(User_Y == 6) { cout << "You can not continue in this direction.\n"; User_Y = User_Y--; getch(); } else if(User_Y == -6) { cout << "You can not continue in this direction.\n"; User_Y = User_Y++; getch(); } //------------------------------------------------------------------------- // Game over if 100 turns expire //------------------------------------------------------------------------- end_game = end_game--; //Game_Over is reduced by 1 each loop when if(end_game == 0) //it reaches 0 the game ends { system("cls"); cout << "Game Over."; getch(); return 0; //This teminates the program } input = getch(); cout <<"Select Action: "; getch(); switch(input) { case VK_UP: cout << "You venture north.\n\n"; User_Y = User_Y++; break; //Include break on the case VK_RIGHT: cout << "You venture east.\n\n"; //same line saves space User_X = User_X++; break; //most people say its case VK_DOWN: cout << "You venture south.\n\n"; //best to put it on a User_Y = User_Y--; break; //new line but this code case VK_LEFT: cout << "You venture west.\n\n"; //is readable enough User_X = User_X--; break; } } while(end_game!=1); return 0; } Heres the output now *************************** Code:
***************************
**** Caverns ***
**** Steven Billington ***
***************************
North = Up Arrow
South = Down Arrow
West = Left Arrow
East = Right Arrow
Home = Information
Insert = Health
P = Points
End = Exit
Select Action: Select Action: Select Action: Select Action: Select Action: Selec
t Action: Select Action: Select Action: Select Action: Select Action: Select Act
ion: Select Action: Select Action: Select Action: Select Action: Select Action:
Select Action: Select Action: Select Action: Select Action: Select Action: Selec
t Action: Select Action: Select Action: Select Action: Select Action: Select Act
ion: Select Action: Select Action: Select Action: Select Action: Select Action:
Select Action: Select Action: Select Action: Select Action: Select Action: Selec
t Action: Select Action: Select Action: Select Action: Select Action: Select Act
ion: Select Action: Select Action: Select Act
|
| RoD is offline |
| | #25 |
| I lurk Join Date: Aug 2002
Posts: 1,361
| You're not going to able to use Virtual Keys with getch(), either use the function BMJ gave you or don't use virtual keys. In MY opinion, I think you should stay away from using the arrow keys for now and work on the design of your game. I choose 'wsad' for moving around, you can change it if you like. Your game loop is extremely hard to follow, I think it can be done much more efficiently... something like: PHP Code: Last edited by Eibro; 10-05-2002 at 03:11 PM. |
| Eibro is offline |
| | #26 |
| Registered User Join Date: Sep 2002
Posts: 124
| I saw you used 'getch()' after showing the directions, which is of course fine. But after that's been done, the same issue occurs if you usen cin before cin.get(). That's the point of the GetKey() function. Here's some code to explain more in depth what I'm talking about: Code: <#includes>
#include "functions.h" // Pretend most functions are in this header
// gets a key
int GetKey() // created by Red_Baron who got help with it from salem
{
int input=getch();
if (input==224)
input=getch();
if (input==0)
input=256+getch();
return input;
}
// game loop
int main()
{
int key;
Openmsg();
Directions();
getch();
for( ;; )
{
key = GetKey();
switch(key)
{
case K_UP:
cout << "North\n";
break;
case K_DOWN:
cout << "South\n";
break;
case K_LEFT:
cout << "West\n";
break;
case K_RIGHT:
cout << "East\n";
break;
case K_ESCAPE:
cout << "Exiting...\n";
key = GetKey();
return 0;
}
}
return 0;
}
__________________ Draco dormiens nunquam titallandus. Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html |
| harryP is offline |
| | #27 |
| Registered User Join Date: Aug 2002
Posts: 1,330
| I don't see why this is so hard... PHP Code: |
| BMJ is offline |
| | #28 |
| Registered User Join Date: Aug 2002
Posts: 1,330
| And then you don't need getch() to pause anymore; Use Windows to your advantage: PHP Code: |
| BMJ is offline |
| | #29 |
| x4000 Join Date: Jun 2002 Location: Outer Space!
Posts: 542
| How did you do that code formatting.. I mean that colours etc.. You didn't format them YOURSELF did you???
__________________ what does signature stand for? |
| Ruski is offline |
| | #30 |
| x4000 Join Date: Jun 2002 Location: Outer Space!
Posts: 542
| By the way.. I have the same problem using the direction keys as well... I tried GetKeyState() but virtual keys don't work (excluding VK_RETURN) (Console) .. I also tried that getch() but when you press one of the arrows.. it just says p: Code: int main()
{
char choice[1];
choice[0] = getch();
cout << choice[0];
return 0;
}
__________________ what does signature stand for? |
| Ruski is offline |
| Thread Tools | |
| Display Modes | |
|
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 |