Here is my code:
#include "snake.h"
void SetPos(short x, short y)
{
//获得标准输出设备的句柄
HANDLE houtput = NULL;
houtput = GetStdHandle(STD_OUTPUT_HANDLE);
//定位光标的位置
COORD pos = { x, y };
SetConsoleCursorPosition(houtput, pos);
}
void CreateMap()
{
//上
int i = 0;
const int WIDTH = 29;
const int HEIGHT = 27;
SetPos(0, 0);
for (i = 0; i < WIDTH; i++)
{
wprintf(L"%lc", L'□');
}
//下
SetPos(0, HEIGHT-1);
for (i = 0; i < WIDTH; i++)
{
wprintf(L"%lc", L'□');
}
//左
for (i = 1; i < HEIGHT-1; i++)
{
SetPos(0, i);
wprintf(L"%lc", L'□');
}
//右
for (i = 1; i < HEIGHT-1; i++)
{
SetPos((WIDTH-1)*2, i);
wprintf(L"%lc", L'□');
}
getchar();
}
When I tried to print the map, I encountered some problem. I have already included the setlocale(LC_ALL, ""); in the main function.
The result is like this:
How can I print a rectangle successfully? My purpose is create a Snake game. And I want to snake move in the rectangle. But the rectangle is printed wrong. I also tried to debug, but there is no use, I couldn't find why it prints in this way.
wprintfshould suffice. We don't neet the whole snake game.mainfunction. Build with extra warnings that are treated as errors. Test. Once that work you add a very small piece of code, perhaps only one or at most two lines (three if you add an empty function). Build and test as before. Then when there's a problem it will be very easy to isolate and make a minimal reproducible example for just that part.