标签:include int C语言 printf MoveCursorTo 移动 col 光标 row
#include<windows.h>
//下标从1开始,x行y列。
char GetStr(int x, int y)//作用是提取出窗口中第x行y列的位置的字符是什么,如果没有东西会返回空格符号
{
COORD pos;
//ReadConsoleOutputCharacterA里的x和y指的是x列y行,且从0开始标号
pos.X = y - 1; pos.Y = x - 1;
LPSTR str;
DWORD read;
ReadConsoleOutputCharacterA(GetStdHandle(STD_OUTPUT_HANDLE), str, 1, pos, &read);
return str[0];
}
#include <windows.h>
static void SetPos(int x, int y)
{
COORD point = { x, y };//光标要设置的位置x,y
HANDLE HOutput = GetStdHandle(STD_OUTPUT_HANDLE);//使用GetStdHandle(STD_OUTPUT_HANDLE)来获取标准输出的句柄
SetConsoleCursorPosition(HOutput, point);//设置光标位置
}
这个比system(“cls”)效果好,闪屏不会特别厉害
如果需要清屏,这样调用即可 SetPos(0, 0)
#include<stdio.h>
#include<windows.h>
#include<conio.h>
void MoveCursorTo(int nRows, int nCols) {//在任意位置移动光标
COORD crdLocation;
crdLocation.X = nRows;
crdLocation.Y = nCols;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crdLocation);
}
int main(int argc, char* argv[])
{
char chInput = 0;
int row = 10;
int col = 10;
MoveCursorTo(row, col);
printf("*");
while (1) {
//#include<conio.h>中_kbhit可以检测用户是否按下,而_getch可以配合_kbhit拿到用户输入。
if (_kbhit() != 0) {//当_kbhit返回值不为0的时候,代表用户有按键按下。
chInput = _getch();
switch (chInput) {//只认wasd,其他输入字符不动作
case'a':
MoveCursorTo(row, col);//移动到原有的位置,
printf(" ");//打印空格,覆盖原来打印的字符,
//Printf()打印都是从左向右的,原来的函数都是先定位(row, col),再打印的"a",这就将"a"覆盖为空格了
row--;
MoveCursorTo(row, col);
printf("a");
break;
case'd':
MoveCursorTo(row, col);
printf(" ");
row++;
MoveCursorTo(row, col);
printf("d");
break;
case'w':
MoveCursorTo(row, col);
printf(" ");
col--;
MoveCursorTo(row, col);
printf("w");
break;
case's':
MoveCursorTo(row, col);
printf(" ");
col++;
MoveCursorTo(row, col);
printf("s");
break;
default://其他输入字符不动作
break;
}
}
}
return 0;
}
标签:include,int,C语言,printf,MoveCursorTo,移动,col,光标,row 来源: https://blog.csdn.net/qq_50832904/article/details/120390428
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。