更改了网上的代码,实现有尾迹的移动,
用键盘上的上下左右来控制
这是借鉴地址:C语言之实现控制台光标随意移动
#include <stdio.h>
#include <windows.h>
#include <conio.h>
HANDLE hout;
//获得输入
char getInput()
{
int ch; //输入字符串
COORD coord; //屏幕上的坐标
CONSOLE_SCREEN_BUFFER_INFO csbi; //控制台屏幕缓冲区信息
coord.X=11;
coord.Y=10;
ch=getch();
csbi.dwCursorPosition.X=11;
csbi.dwCursorPosition.Y=10;
//0x0d表示回车,0XE0表示上下左右等键的键码
while(ch==0xE0||ch==0x0d)
{
GetConsoleScreenBufferInfo(hout,&csbi);//读取控制台屏幕缓冲信息
coord.X=csbi.dwCursorPosition.X; //得到坐标X的值
coord.Y=csbi.dwCursorPosition.Y; //得到坐标Y的值
ch=getch();
//printf(" ");
//上
if(ch==0x48)
{
if(coord.Y!=0)
{
coord.Y--;
//printf("%c",'A');
}
}
//下
else if(ch==0x50)
{
coord.Y++;
//printf("%c",'A');
}
//左
else if(ch==0x4b)
{
if(coord.X!=0){coord.X--;}//printf("%c",'A');}
}
//右
else if(ch==0x4d)
{
if(coord.X!=79)
{
coord.X++;
//printf("%c",'A');
}
}
printf("%c",'.');
SetConsoleCursorPosition(hout,coord);
printf("A");
SetConsoleCursorPosition(hout,coord);
ch=getch();
}
}
int main()
{
char ch;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
//从键盘获取输入,如果是方向键则执行方向功能,如果是回车键则换行,如果是字符则输出
getInput();
return 0;
}
本来是为了做个控制台下的迷宫,但是当时实在无力,现在也一样。