|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <Windows.h>
- static void gotoxy(int x, int y)
- {
- COORD position = { x, y };
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
- }
- int main()
- {
- int x = 0, y = 0;
- char z=0;
- printf("请输入大写的WASD进行操作,了解操作请按回车");
- getchar();
- system("cls");
- puts("■");
- while (1)
- {
- z=getch();
- if (z == 'W' || 'A' || 'S' || 'D')
- {
- if (z == 'W')
- y = y - 1;
- if (z == 'A')
- x = x - 1;
- if (z == 'S')
- y = y + 1;
- if (z == 'D')
- x = x + 1;
- gotoxy(x, y);
- puts("■");
- }
-
- }
-
- }
复制代码
这个可以让‘■’东西运动起来,x轴没有问题,y轴每移动一次前一个■会保留在屏幕上,因为知识面狭窄自己无能为力,所以想问问大佬咋才能解决这个问题呢?
- #include <stdio.h>
- #include <Windows.h>
- static void GotoXY(int x, int y)
- {
- COORD position = {x * 2, y}; // unicode
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
- }
- static void HideCursor()
- {
- CONSOLE_CURSOR_INFO cci;
- cci.bVisible = FALSE;
- cci.dwSize = sizeof(cci);
- HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorInfo(handle, &cci);
- }
- int main(void)
- {
- HideCursor();
- printf("请输入大写的WASD进行操作,了解操作请按回车");
- getchar();
- system("cls");
-
- COORD pos = {0, 0};
- while(1)
- {
- int ch = getch();
- system("cls");
- switch(ch)
- {
- case 'W':
- case 'w':
- --pos.Y;
- break;
- case 'S':
- case 's':
- ++pos.Y;
- break;
- case 'A':
- case 'a':
- --pos.X;
- break;
- case 'D':
- case 'd':
- ++pos.X;
- break;
- }
- if(pos.X < 0)
- pos.X = 0;
- if(pos.Y < 0)
- pos.Y = 0;
-
- GotoXY(pos.X, pos.Y);
- puts("■");
- }
- return 0;
- }
复制代码
|
|