Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;程序,文本在cmd中不断抖动_C++_Cmd - Fatal编程技术网

C++ c++;程序,文本在cmd中不断抖动

C++ c++;程序,文本在cmd中不断抖动,c++,cmd,C++,Cmd,这个程序应该有一个由点组成的地图和一个你可以移动的播放器 但是有一天,当我打开这个程序时,文本在忽隐忽现,上下移动 它使用Windows.h在控制台上重新绘制而不闪烁,并隐藏光标 它是否与cmd属性、代码或Windows.h头文件有关 #include <iostream> #include <conio.h> #include <Windows.h> char map[1000][1000]; int width = 100; int height =

这个程序应该有一个由点组成的地图和一个你可以移动的播放器

但是有一天,当我打开这个程序时,文本在忽隐忽现,上下移动

它使用Windows.h在控制台上重新绘制而不闪烁,并隐藏光标

它是否与cmd属性、代码或Windows.h头文件有关


#include <iostream>
#include <conio.h>
#include <Windows.h>

char map[1000][1000];
int width = 100;
int height = 10;
char Tmt = '.';

void cls()
{
    COORD cursorPosition;
    cursorPosition.X = 0;
    cursorPosition.Y = 0;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPosition);
}

void hidcur()
{
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(consoleHandle, &info);
}

class Player {
public:

    int x;
    int y;
    char sym;

    void Draw()
    {
        map[x][y] = sym;
    }

    void Logic()
    {
        if (x < 1) { x++; }
        if (y < 1) { y++; }
        if (x > width) { x--; }
        if (y > height) { y--; }
    }

    void Input()
    {
        if (_kbhit())
        {
            switch (_getch())
            {
            case 'w':
                map[x][y] = Tmt;
                y--;
                break;
            case 'a':
                map[x][y] = Tmt;
                x--;
                break;
            case 's':
                map[x][y] = Tmt;
                y++;
                break;
            case 'd':
                map[x][y] = Tmt;
                x++;
                break;
            case 'p':
                system("cls");
                break;
            }
        }
    }

    void PrintPos()
    {
        printf("Pos:\n");
        if (x >= 10) { printf("    %d\n", x); }
        else { printf("    0%d\n", x); }
        if (y >= 10) { printf("    %d\n", y); }
        else { printf("    0%d\n", y); }
    }
};

Player player1;

void Setup()
{
    player1.x = 2;
    player1.y = 2;
    player1.sym = '@';
    player1.Draw();
    player1.Logic();

    for (int mapsui = 1; mapsui <= width; mapsui++)
    {
        for (int mapsuj = 1; mapsuj <= height; mapsuj++)
        {
            map[mapsui][mapsuj] = Tmt;
        }
    }
}

void DrawMap()
{
    player1.PrintPos();
    printf("\n\n\n");
    for (int drawi = 1; drawi <= width; drawi++)
    {
        for (int drawj = 1; drawj <= height; drawj++)
        {
            printf("%c", map[drawj][drawi]);
        }
        printf("\n");
    }
}

int main()
{
    Setup();

    while (1)
    {
        DrawMap();
        player1.Input();
        player1.Logic();
        player1.Draw();

        cls();
        hidcur();
    }

    return 0;
}


#包括
#包括
#包括
字符映射[1000][1000];
整数宽度=100;
整数高度=10;
字符Tmt=';
void cls()
{
坐标光标位置;
光标位置X=0;
光标位置Y=0;
设置控制台位置(GetStdHandle(标准输出句柄),光标位置);
}
void hidcur()
{
句柄控制台HANDLE=GetStdHandle(标准输出句柄);
控制台\光标\信息;
info.dwSize=100;
info.bVisible=FALSE;
SetConsoleCursorInfo(控制台句柄和信息);
}
职业选手{
公众:
int x;
int-y;
字符符号;
作废提款()
{
map[x][y]=sym;
}
void逻辑()
{
如果(x<1){x++;}
如果(y<1){y++;}
如果(x>width){x--;}
如果(y>高度){y--;}
}
无效输入()
{
如果(_kbhit())
{
开关(_getch())
{
案例“w”:
map[x][y]=Tmt;
y--;
打破
案例“a”:
map[x][y]=Tmt;
x--;
打破
案例s:
map[x][y]=Tmt;
y++;
打破
案例“d”:
map[x][y]=Tmt;
x++;
打破
案例“p”:
系统(“cls”);
打破
}
}
}
void PrintPos()
{
printf(“Pos:\n”);
如果(x>=10){printf(“%d\n”,x);}
else{printf(“0%d\n”,x);}
如果(y>=10){printf(“%d\n”,y);}
else{printf(“0%d\n,y);}
}
};
玩家1;
无效设置()
{
player1.x=2;
player1.y=2;
player1.sym='@';
player1.Draw();
player1.Logic();

for(int-mapsui=1;mapsui在DrawMap方法中,当有两个for循环时,检查外循环中的宽度和内循环中的高度,反之亦然。请注意,外循环控制行数,并将其限制为宽度(100)您正在显示100行,其中大部分是空的,因为安装程序只初始化了10行。由于命令窗口显示的行数较少(我认为默认情况下大约为30行或类似的行数),因此贴图会向上移动,从而导致您看到的闪烁。应该是:

for (int drawi = 1; drawi <= height; drawi++)
{
    for (int drawj = 1; drawj <= width; drawj++)
    {
        printf("%c", map[drawj][drawi]);
    }
    printf("\n");
}
for(int-drawi=1;drawi