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
|
#include <conio.h>
#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
//GetStdHandle(nStdHandle) - Retrieves a handle to the specified standard device (standard input, standard output, or standard error).
//STD_INPUT_HANDLE - The standard input device. Initially, this is the console input buffer, CONIN$.
//STD_OUTPUT_HANDLE - The standard output device. Initially, this is the active console screen buffer, CONOUT$.
//STD_ERROR_HANDLE - The standard error device. Initially, this is the active console screen buffer, CONOUT$.
COORD CursorPosition;
void PlaceCursor(int x, int y);
void CheckKey( void *dummy );
bool loop = true;
int x = 20;
int y = 20;
int main(){
cout << "WELCOME!" << endl;
cout << "USE 'W', 'S', 'A' AND 'D' TO MOVE" << endl;
cout << "PRESS 'P' TO EXIT" << endl;
PlaceCursor(x, y);
_beginthread( CheckKey, 0, NULL );
while(loop);
return 0;
}
void PlaceCursor(int x, int y)
{
CursorPosition.X = x; // Locates column
CursorPosition.Y = y; // Locates Row
SetConsoleCursorPosition(console,CursorPosition); // Sets position for next thing to be printed
}
void CheckKey( void *dummy )
{
while (loop){
int key = _getch();
if( (key == 119) || (key == 87) )//w || W
y--;
else if( (key == 115) || (key == 83) )//s || S
y++;
if( (key == 97) || (key == 65) )//a || A
x--;
else if( (key == 100) || (key == 68) )//d || D
x++;
else if( (key == 112) || (key == 80) ){//p || P
loop = false;
_endthread();
}
PlaceCursor(x, y);
//Sleep(500);
}
}
|