2

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: enter image description here 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.

New contributor
tayir is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
23
  • 2
    Please post a minimal reproducible example, the smallest possible program that is needed to reproduce your problem. A program with a single wprintf should suffice. We don't neet the whole snake game. Commented yesterday
  • 2
    Break it down into smaller pieces, perhaps even start over. My recommendation is to not write too much all at once, as that will make it harder to test and debug. Instead start with an empty main function. 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. Commented yesterday
  • 2
    As for your problem, what font is used by the terminal? Is it a fixed-width, or a proportional font? Commented yesterday
  • 1
    In my experience, the Windows Console has many quirks and often does not work very reliably for things like games, even when not mixing Windows Console functions with standard I/O functions (which is generally a bad idea). I don't know whether the situation has improved in the latest versions of Windows. Therefore, I would rather recommend that you use a proper graphics API, for example SDL (cross-platform) or Direct2D (only works on Windows). There are tutorials available for both. Commented yesterday
  • 2
    Note thie code in its current state is not a minimal reproducible example because no one can compile it. Please post a complete program (with main) that can be copied to a text editor, built, and ran, with no additional steps. Commented yesterday

1 Answer 1

2

The terminal spacing between characters is not the same horizontally as it is vertically, meaning each character fits into a rectangle on the screen and not a square. Given your shape is a square it cannot occupy its whole rectangular space and leaves blank space on the top and bottom.

In this image you can visually see the height and width of each cell compared the square character.

To fix your snake game problem you could try add spaces between each of the horizontal squares or try different character combinations like []

I find the height of the cell is roughly twice its width so two characters usually make a square

(no spaces)
□
□□
□□□

(with spaces)
□
□ □
□ □ □

(other characters)

[]
[][]
[][][]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.