توی ویندور این کدی که قرار دادم با فشردن arrow key مکان نما را جابجا می کنه توی linux هم باید از n cursor استفاده کنید.
#include <windows.h>
#include <conio.h>
#ifdef _WIN32
void gotoxy(int x, int y)
{
static HANDLE h = NULL;
if(!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h,c);
}
#else
void gotoxy(int x, int y)
{
char str[200];
sprintf(str,"\033[%d;%dH",x,y);
printf(str);
}
#endif
enum class MoveDirect{LEFT,RIGHT,TOP,DOWN};
class PointA{
public:
PointA():x_(0),y_(0){}
PointA(int x,int y):x_(x),y_(y){}
void moveA(const MoveDirect &direct,int step=1){
switch (direct){
case MoveDirect::LEFT :
x_ -= step;
break;
case MoveDirect::RIGHT :
x_ += step;
break;
case MoveDirect::TOP :
y_ -= step;
break;
case MoveDirect::DOWN :
y_ += step;
break;
}
}
MoveDirect getDirect(const int &direct_key){
switch (direct_key)
{
case 75:
return MoveDirect::LEFT;
case 77:
return MoveDirect::RIGHT;
case 72:
return MoveDirect::TOP;
case 80:
return MoveDirect::DOWN;
}
}
void moveA(const int &direct,int step=1){
MoveDirect m_direct = getDirect(direct);
moveA(m_direct);
}
int x_;
int y_;
};
int main()
{
PointA cur_point;
int ch;
do{
ch = _getch();
if (ch == 224)
ch = _getch();
cur_point.moveA(ch);
gotoxy(cur_point.x_,cur_point.y_);
//cout << ch << endl;
}while(ch != 27);
return 0;
}