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
|
#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 = 5, y=10; // Set where to start printing
int key; // Changed to an integer, instead of a char
#define ENTER 13
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80
int main()
{
removeCursor();
std::cout<<"\tUse arrow keys: \x1a \x1b \x19 \x18";
gotoXY(x, y);
std::cout<<((char)('\x01'));
do
{
key = _getch();
switch(key)
{
case RIGHT:
gotoXY(x, y);
std::cout<<' ';
x++;
gotoXY(x, y);
std::cout<<((char)('\x01'));
break;
case LEFT:
gotoXY(x, y);
std::cout<<' ';
x--;
gotoXY(x, y);
std::cout<<((char)('\x01'));
break;
case UP:
gotoXY(x, y);
std::cout<<' ';
y--;
gotoXY(x, y);
std::cout<<((char)('\x01'));
break;
case DOWN:
gotoXY(x, y);
std::cout<<' ';
y++;
gotoXY(x, y);
std::cout<<((char)('\x01'));
}
}while(key != ENTER);
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);
}
|