好吧,我有那个将数组映射保存到txt文件中的代码:
std::ofstream output("mapa.txt");
for(int y=0;y<worldHeight;y++) {
for(int x=0;x<worldWidth;x++) {
output<<scene[x][y];
if(x<(worldWidth-1)){output<<",";}
}
if(y<(worldHeight-1)){output<<std::endl;}
}
我想将其集成到while循环中:
std::ofstream output("mapa.txt");
while (true) {
if (yPos == topOfTheWorld) {
scene[xPos][yPos] = 2;
} else if (yPos >= topOfTheWorld) {
scene[xPos][yPos] = 1;
} else if(yPos < topOfTheWorld) {
scene[xPos][yPos] = 0;
} else {
scene[xPos][yPos] = 0;
}
output<<scene[xPos][yPos] << ",";
//if(xPos<(worldWidth-1)){output<<",";}
//if(yPos<(worldHeight-1)){output<<std::endl;}
yPos++;
if(yPos>worldHeight) {
output<<std::endl;
slope = random(5)-2;
if(topOfTheWorld<(worldHeight/6)) {
slope = 1;
} else if(topOfTheWorld>(worldHeight-10)) {
slope = -1;
}
topOfTheWorld += slope;
yPos = 0;
xPos++;
}
if (xPos>=worldWidth) {
break;
}
}
但是由于某些原因,“ while”循环变为无限...:/
我能做些什么?
代码看起来不错。但是我不确定scene
是否分配正确。您拥有yPos>worldHeight
和在下面xPos>=worldWidth
,这表明您不确定如何比较这些。我怀疑您至少其中之一错了。这可以轻松地在场景变量之外编写并覆盖yPos或xPos。
尝试更改为ypos>=worldHeight
,看看是否可行。如果没有检查您分配了多少空间,scene
并且看起来还可以,请尝试使其变大一些。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句