Hey all,
I'm going to start out by saying that I know I'm just a very beginner at this. I'm trying to develop my skills by working on a console-based text game. For our purposes today, think NetHack.
Basically, I'm trying to get it so I can have a character (represented by the '@' symbol) move around the screen, using the arrow keys. I have it working, kind of. Here's my program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include<iostream>
#include<conio.h>
using namespace std;
#define CONSOLE_MAX_WIDTH 80
#define CONSOLE_MAX_HEIGHT 12
void PrintMap(int PlayerXLoc, int PlayerYLoc);
int main()
{
unsigned char com; // User-input command
int cx = 40, cy = 10; // Current character's x-y location
do {
system("cls");
PrintMap(cx, cy);
//Get Command
com = _getch();
switch(com)
{
case 224:
com = _getch();
if(com == 72)
cy--;
else if(com == 75)
cx--;
else if(com == 80)
cy++;
else if(com == 77)
cx++;
}
if(cx < 0)
cx = 0;
else if(cx > CONSOLE_MAX_WIDTH - 1)
cx = CONSOLE_MAX_WIDTH - 1;
else if(cy < 0)
cy = 0;
else if(cy > CONSOLE_MAX_HEIGHT - 1)
cy = CONSOLE_MAX_HEIGHT - 1;
} while(com != 'q');
return 0;
}
void PrintMap(int PlayerXLoc, int PlayerYLoc)
{
// py: current 'y' output location, px: current 'x' output location
for(int py = 0; py < CONSOLE_MAX_HEIGHT; py++)
{
for(int px = 0; px < CONSOLE_MAX_WIDTH; px++)
{
if( px == PlayerXLoc && py == PlayerYLoc)
cout << "@";
else
cout << " ";
}
cout << endl;
}
}
|
Basically, it was just thrown together in a couple minutes. I know there are many bad programming practices being put to use there. I have a few specific questions though: Firstly, should I be using _getch() to get the character's movement input, or is there a better way? That's the only way I know of the capture an arrow key's input.
Secondly, is there a better way to refresh the screen? I know that right now, it kind of works, but as I add more to the program (such as walls the character can not pass), the whole screen will flash whenever I move. I would rather avoid this if I could. I also feel bad as someone in the beginner stages of programming getting in the habit of using a system() call. Maybe I'm wrong, but it just feels like a bad practice.
Thirdly, is there a function to call which will return the console's width and height values in terms of the number of characters? Right now, as you can see, I just have them hardcoded for a standard 80x14 character screen. It says 12 because in my print map function, there's an endl after every line, including the last one, bringing it to 13. Then when I call getch it seems to add another newline with a blinking cursor at the bottom (Something I'd also like to get rid of).
Lastly, is all of this going to be worth the effort without any win32, DirectX or OpenGL programming? The point of this game was to make something somewhat interesting using just the console, so I could get practice before delving into more complex topics.
Some things I want to implement in the future which may affect the answer to the last question:
- Real-time changes to the playing field (NPC's moving around the screen, maybe having the chance to move every second or two)
- User-defined key bindings
Thanks a bunch for any input! I'd rather know the answers ahead of time so I don't need to redo the entire program later, when I actually get into developing it.