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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <iostream>
#include <windows.h>
using namespace std;
struct Object
{
public:
int x,y;
char sprite;
Object()
{
x=y=2; //default location (2,2);
sprite='@'; //default sprite (@);
}
char draw();
};
namespace global
{
const static char esc = 27;//Escape;
const static char cr = 13;//Carriage return;
const static char etx = 3;//Ctrl-C; end of text;
const static int SIZEX = 38;//Map size(X);
const static int SIZEY = 20;//Map size(Y);
char map[SIZEY][SIZEX] =
{"#####################################",
"# # # # # # # # # #",
"# # # # # # # # # #",
"# # # # # # # # # #",
"#####################################",
"#####################################",
"# # # # # # # # # #",
"# # # # # # # # # #",
"# # # # # # # # # #",
"#####################################",
"#####################################",
"# # # # # # # # # #",
"# # # # # # # # # #",
"# # # # # # # # # #",
"#####################################",
"#####################################",
"# # # # # # # # # #",
"# # # # # # # # # #",
"# # # # # # # # # #",
"#####################################"};
Object player;
char input;
}
void position(int x, int y)
{
static HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
static COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle,coord);
}
void draw()
{
position(0,0);//Reposition cursor to 0,0 position of command prompt.
for(int y=0;y<global::SIZEY;++y)
{
for(int x=0;x<global::SIZEX;++x)
{
if(x == global::player.x && y == global::player.y)
{
cout<<global::player.sprite;
}
else
{
cout<<global::map[y][x];
}
}
cout<<endl;
}
}
bool event(char &input)
{
if(GetAsyncKeyState(0x57))//Detect for 'w' Key. Up
{
--global::player.y;
return true;
}
if(GetAsyncKeyState(0x53))//Detect for 's' Key. Down
{
++global::player.y;
return true;
}
if(GetAsyncKeyState(0x41))//Detect for 'a' Key. Left
{
--global::player.x;
return true;
}
if(GetAsyncKeyState(0x44))//Detect for 'd' Key. Right
{
++global::player.x;
return true;
}
if(GetAsyncKeyState(VK_ESCAPE))//Detect for 'ESCAPE' Key. Escape
{
input = global::esc;
return false;
}
if(GetAsyncKeyState(VK_CONTROL) && GetAsyncKeyState(0x43))//Detect for 'control' and 'c' Key. Ctrl-C
{
input = global::esc;
return false;
}
//Unimplemented key entered:
return false;
}
void debug()
{
cout<<"x: "<<global::player.x<<" y: "<<global::player.y<<" "<<endl;//Extra space to clear the debug location.
}
int main()
{
draw();
debug();//Post x and y location
do
{
if(event(global::input))
{
draw();//Draw the map.
debug();//Post x and y location
}
}while(global::input!=global::esc && global::input!=global::etx);
return 0;
}
|