想写一个双缓冲的贪吃蛇,但是不知为何最后两行仍闪烁

问题遇到的现象和发生背景

想写一个双缓冲的贪吃蛇,在网上学习代码之后如下,整体运行效果尚可,但是最后两行仍闪烁,想问一下代码哪里出问题了

问题相关代码,请勿粘贴截图
#include 
#include 
#include 
#include
using namespace std;

bool gameOver;
const int width = 30;
const int height = 30;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
char map[height + 2][width + 2];
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirecton dir;

HANDLE hOutput, hOutBuf;//控制台屏幕缓冲区句柄
COORD coord = { 0,0 };
//双缓冲处理显示
DWORD bytes = 0;


void Setup()
{
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
}
void Draw()
{

    for (int i = 0; i < height+2; i++)
    {
        for (int j = 0; j < width+2; j++)
        {
            if (i == y && j == x)
                map[i][j] = '0';
            else if (i == fruitY && j == fruitX)
                map[i][j] = '$';
            else
            {
                bool print = false;
                for (int k = 0; k < nTail; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        map[i][j] = 'o';
                        print = true;
                    }
                }
                if (!print)
                    map[i][j] = ' ';
            }


            if (j == width + 1)
                map[i][j] = '#';
        }
        //cout << endl;
    }

    //cout << endl;
   // cout << "Score:" << score << endl;

    for (int i = 0; i < width + 2; i++)
        map[0][i] = '#';
    //cout << endl;

    for (int i = 0; i < height + 2; i++)
        map[i][0] = '#';


    for (int i = 0; i < width + 2; i++)
        map[height + 1][i] = '#';


    for (int i = 0; i < height + 2; i++) 
    {
        coord.Y = i;
        WriteConsoleOutputCharacterA(hOutBuf, map[i], height+2, coord, &bytes);
    }
    //设置新的缓冲区为活动显示缓冲
    SetConsoleActiveScreenBuffer(hOutBuf);
    Sleep(30);


    for (int i = 0; i < width + 2; i++)
    {
        coord.Y = i;
        WriteConsoleOutputCharacterA(hOutput, map[i], width+2, coord, &bytes);
    }
    //设置新的缓冲区为活动显示缓冲
    SetConsoleActiveScreenBuffer(hOutput);
    Sleep(30);
}

void Input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'A':
        case 'a':
            dir = LEFT;
            break;
        case'D':
        case 'd':
            dir = RIGHT;
            break;
        case'W':
        case 'w':
            dir = UP;
            break;
        case'S':
        case 's':
            dir = DOWN;
            break;
        case 'x':
            gameOver = true;
            break;
        }
    }
}
void Logic()
{
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;
    for (int i = 1; i < nTail; i++)
    {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
    switch (dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }
    //if (x > width || x < 0 || y > height || y < 0)
    //  gameOver = true;
    if (x > width)
        x = 1; 
    else if (x <= 0) 
        x = width - 1;
    if (y > height) 
        y = 1; 
    else if (y <= 0) 
        y = height - 1;

    for (int i = 0; i < nTail; i++)
        if (tailX[i] == x && tailY[i] == y)
            gameOver = true;

    if (x == fruitX && y == fruitY)
    {
        score += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        nTail++;
    }
}
int main()
{

    Setup();


    //创建新的控制台缓冲区
    hOutBuf = CreateConsoleScreenBuffer(
        GENERIC_WRITE,//定义进程可以往缓冲区写数据
        FILE_SHARE_WRITE,//定义缓冲区可共享写权限
        NULL,
        CONSOLE_TEXTMODE_BUFFER,
        NULL
    );
    hOutput = CreateConsoleScreenBuffer(
        GENERIC_WRITE,//定义进程可以往缓冲区写数据
        FILE_SHARE_WRITE,//定义缓冲区可共享写权限
        NULL,
        CONSOLE_TEXTMODE_BUFFER,
        NULL
    );
    //隐藏两个缓冲区的光标
    CONSOLE_CURSOR_INFO cci;
    cci.bVisible = 0;
    cci.dwSize = 1;
    SetConsoleCursorInfo(hOutput, &cci);
    SetConsoleCursorInfo(hOutBuf, &cci);
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(30); //sleep(10);
    }

    return 0;
}

试试再加上清屏函数


void gotoxy(int x,int y)//⾃⼰定义⼀个gotoxy,将光标跳到x列y⾏
{
COORD c; //定义⼀个光标类
c.X = x; //设置这个光标的位置
c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); //调⽤WindowsAPI来设置光标位置
}
//最后在调用时这样写:

//gotoxy(0,0);