c++中如何在win32控制台用键盘上下左右键控制一个字符移动,求一可在vc++6.0上执行的完整简洁可编译代码。

不要颜色声音等,主要是那个控制键盘响应的函数
2025-02-24 10:37:25
推荐回答(2个)
回答1:

while(1)
{
ch=getch(); //输入
else if(ch==72) {ty=y-1; tx=x;} //上
else if(ch==80) { ty=y+1; tx=x;} //下
else if(ch==75) {tx=x-1;ty=y;} //左
else if(ch==77) { tx=x+1; ty=y;} //右
else if(ch==27) break ; //按Esc退出循环
if(。。。)//判断是否出界
{
gotoxy(x,y); printf(" ");
x=tx; y=ty;
gotoxy(x,y); printf(" 。");
}
}
void gotoxy(int x,int y) //定位光标
{
COORD position;
position.X=x; position.Y=y;
SetConsoleCursorPosition(out, position);
}

回答2:

用getch() 接受键盘响应。然后你搜一下gotoxy()函数这个是TC 里面的函数VC里没有。不过网上能找到代替品。以前做过,挺简单的。