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
|
#include <iostream>
#include <Windows.h>
#include <conio.h>
void gotoXY(int, int);
void removeCursor(void);
//Global scope variables
int posX(5), posY(10);
int x, y;
char key;
int main(){
removeCursor();
std::cout<<"\tUse arrow keys: \x1a \x1b \x19 \x18";
while(key != 'q'){
key = getch();
gotoXY(x, y);
std::cout<<' ';
gotoXY(posX,posY);
std::cout<<((char)('\x01'));
x = posX, y = posY;
if(key == 0x4d){ posX++; } //Go Right - VK_RIGHT, 77, 0x4d
if(key == 0x4b){ posX--; } //Go Left - VK_LEFT, 75, 0x4b
if(key == 0x50){ posY++; } //Go Down - VK_DOWN, 80, 0x50
if(key == 0x48){ posY--; } //Go Up - VK_UP, 72, 0x48
}
return 0;
}
void gotoXY(int x, int y){
COORD c;
c.X = x, c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void removeCursor(void){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 1;
cci.bVisible = false; //false = invisible
SetConsoleCursorInfo(handle, &cci);
}
|