我的代码出了问题。这不是编译器错误。
我有一个函数,根据值,它会在打印2D数组的单元格时改变颜色。在我的主要部分中,我有一个for循环,我在同一个2D数组上打印,并且每个循环后上面提到的值都不会改变。然而,在第13次循环之后,背景变得混乱。我已经在循环12和14之间上传了执行窗口的图像。以下url用于图像。
代码如下
void Planet::draw(){
int p=getHQ()->at(getHQ()->size()-1)->getPosHQX();
int q=getHQ()->at(getHQ()->size()-1)->getPosHQY();
HANDLE hConsole;
hConsole= GetStdHandle (STD_OUTPUT_HANDLE);
for (int i=0;i<BOO::getGlobalMapSize();i++){
for (int j=0;j<BOO::getGlobalMapSize();j++){
if (((i==p) && (j==q)) || ((i==p+1) && (j==q)) || ((i==p+2) && (j==q)) || ((i==p) && (j==q+1)) || ((i==p) && (j==q+2)) || ((i==p+1) && (j==q+2)) || ((i==p+2) && (j==q+1)) || ((i==p+2) && (j==q+2)) || ((i==p+1) && (j==q+1))){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
}
else{
if ((getZones()->at(i*BOO::getGlobalMapSize()+j)->getAccessDanger()>=0) && (getZones()->at(i*BOO::getGlobalMapSize()+j)->getAccessDanger()<0.3)){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_GREEN);
}
else if ((getZones()->at(i*BOO::getGlobalMapSize()+j)->getAccessDanger()>=0.3) && (getZones()->at(i*BOO::getGlobalMapSize()+j)->getAccessDanger()<0.6)){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_RED);
}
else if ((getZones()->at(i*BOO::getGlobalMapSize()+j)->getAccessDanger()>=0.6) && (getZones()->at(i*BOO::getGlobalMapSize()+j)->getAccessDanger()<=1)){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_RED);
}
else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
}
}
cout<<getPlanet()->at(getPlanet()->size()-1)->Map[i][j];
cout<<'|';
}
cout<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
}
cout<<endl<<endl;
Sleep(500);
}
int main(){
srand(time(NULL));
Planet Zeus;
for (int ii=0;ii<14;ii++){
Zeus.draw();
}
}