|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <Windows.h>
- void map(char(*square)[20])
- {
- int i, j;
- for (i = 0; i < 10; i++)
- {
- for (j = 0; j < 20; j++)
- {
- printf("%c", *(*(square + i) + j));
- }
- printf("\n");
- }
- }
- static void GotoXY(int x, int y)
- {
- COORD position = {2*x, y };
- 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()
- {
- HideCursor();
- printf("请输入大写的WASD进行操作,了解操作请按回车\n");
- char square[10][20] = { "--------------------",\
- "| |",\
- "| |",\
- "| |", \
- "| |", \
- "| |", \
- "| |",\
- "| |",\
- "| |",\
- "--------------------" };
- map(square);
- COORD pos = { 1, 1 };
- getchar();
- system("cls");
- map(square);
- GotoXY(1, 1);
- puts("■");
- while (1)
- {
-
- char z = getch();
- system("cls");
- map(square);
- if (z == 'W' || 'A' || 'S' || 'D')
- {
- if (z == 'W')
- pos.Y--;
- if (z == 'A')
- pos.X--;
- if (z == 'S')
- pos.Y++;
- if (z == 'D')
- pos.X++;
- if (pos.X < 1)
- pos.X = 1;
- if (pos.Y < 1)
- pos.Y = 1;
- if (pos.X > 9)
- pos.X = 9;
- if (pos.Y > 8)
- pos.Y = 8;
- GotoXY(pos.X, pos.Y);
- puts("■");
- }
- }
- getchar();
- return 0;
- }
复制代码
这个可以做到在一个框里■四处走动,但是为了保证y轴只有一个■而用了system("cls"),但这样也导致了我做的框每次都会被删除然后重新显示,所以我想问问有什么办法可以让框不消失,而■在y轴也只显示一个?
- #include <stdio.h>
- #include <string.h>
- #include <Windows.h>
- void map(char *square[])
- {
- for(int i = 0; square[i]; ++i)
- puts(square[i]);
- }
- static void GotoXY(int x, int y)
- {
- COORD position = {x, y};
- 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进行操作,了解操作请按回车\n");
- getchar();
- system("cls");
- char *square[] = {
- "--------------------",
- "| |",
- "| |",
- "| |",
- "| |",
- "| |",
- "| |",
- "| |",
- "| |",
- "--------------------",
- NULL
- };
- const int MAP_WIDTH = 20 - 3;
- const int MAP_HEIGHT = 10 - 2;
- map(square);
- COORD pos = {1, 1};
- COORD old_pos = pos;
- while(1)
- {
- GotoXY(old_pos.X, old_pos.Y);
- puts(" ");
- GotoXY(pos.X, pos.Y);
- puts("■");
- int ch = getch();
- old_pos = pos;
- 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 < 1)
- pos.X = 1;
- if(pos.Y < 1)
- pos.Y = 1;
- if(pos.X > MAP_WIDTH)
- pos.X = MAP_WIDTH;
- if(pos.Y > MAP_HEIGHT)
- pos.Y = MAP_HEIGHT;
- }
- return 0;
- }
复制代码
|
|