我製作這個控制檯遊戲和我這樣做的方式是從稱爲地圖的三維數組中打印字符到控制檯。哪些是使用怪物,人物和背景的字符分配的。問題是我已經能夠分配給地圖數組並且成功地打印了一個怪物類的字符。但由於某些原因,派生的字符類對象沒有打印字符。 我相信這不是一個訪問問題,因爲沒有編譯錯誤,當程序運行時,這些字符被分配到的行(沒有確切的索引,但是正確的行索引)上有空白空白。派生類沒有正確打印
下面是分配給它的代碼:
void game::assignScreen() {
for (int row = 0; row < 20; row++) {
for (int col = 0; col < 40; col++) {
if (col == 0 || col == 39) {
map[row][col] = (char) 124;
} else if (row == 0 || row == 19) {
map[row][col] = (char) 95;
} else {
map[row][col] = (char) 46;
}
}
}
switch (difficulty) {
case 1:
for (int i=0; i <2; i++){
map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
}
case 2:
if (difficulty == 2) {
for (int i = 0; i <4; i++) {
map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
}
}
map[elfPlayer.getPos(true)][elfPlayer.getPos(false)] = elfPlayer.getSymbol();
map[guardPlayer.getPos(true)][guardPlayer.getPos(false)] = guardPlayer.getSymbol();
map[knightPlayer.getPos(true)][knightPlayer.getPos(false)] = knightPlayer.getSymbol();
map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
break;
case 3:
for (int i = 0; i <6; i++) {
map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
}
case 4:
if (difficulty == 4) {
for (int i = 0; i <9; i++) {
map[enemy[i]->getPos(true)][enemy[i]->getPos(false)] = enemy[i]->getSymbol();
}
}
map[elfPlayer.getPos(true)][elfPlayer.getPos(false)] = elfPlayer.getSymbol();
map[guardPlayer.getPos(true)][guardPlayer.getPos(false)] = guardPlayer.getSymbol();
map[knightPlayer.getPos(true)][knightPlayer.getPos(false)] = knightPlayer.getSymbol();
map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
map[roguePlayer.getPos(true)][roguePlayer.getPos(false)] = roguePlayer.getSymbol();
break;
}
}
而這種打印它
void game::printScreen(string msg) {
clearScreen();
cout << msg;
for (int row = 0; row < 20; row++) {
for (int col = 0; col < 40; col++) {
if (map[row][col] == 'E' || map[row][col] == 'G' || map[row][col] == 'K' ||
map[row][col] == 'R' || map[row][col] == 'W') {
if (getPlayer(map[row][col]).getStatus() == 1) {
changeColor(14, false);
cout << map[row][col];
changeColor(15, false);
} else if (getPlayer(map[row][col]).getStatus() == 2 || getPlayer(map[row][col]).getStatus() == 3) {
changeColor(4, false);
cout << map[row][col];
changeColor(15, false);
}
} else {
cout << map[row][col];
}
}
cout << endl;
}
}
getPlayer是一個函數,它當前保存的/選擇的字符,並返回它屬於
對象位置
int character::getPos(bool getX) {
if (getX) {
return xPos;
} else {
return yPos;
}
}
個
回報燒焦
char character::getSymbol() {
return symbol;
}
頭文件
class character
{
public:
character();
~character();
string getName();
char getSymbol();
int getStatus();
int getPos(bool);
void setPos(int, int);
void setStatus(bool, bool);
protected:
string className;
char symbol;
private:
int xPos;
int yPos;
};
從
monster *enemy[21];
elf elfPlayer;
guard guardPlayer;
knight knightPlayer;
rogue roguePlayer;
wizard wizardPlayer;
printScreen(elfPlayer.getName());
cout << elfPlayer.getSymbol();
我測試是否爲對象的志願服務崗位可打印遊戲的頭文件,它的工作對*敵人[ ],派生類看起來很好。我真的不知道爲什麼它不能正確打印。也許我就像一些洞察力一樣犯了一個愚蠢的錯誤。 What happens when the map is printed, the bottom right empty area is meant to be full of chars
你應該完全認爲使用引用1將使你的代碼更具可讀性並且減少在你的地圖上調用operator []'的次數...... –
也請使用x和y的2個函數/ getter位置。 'getPos(bool getX)'看起來不正確......或者讓函數返回一個點對象。 –
您發佈了很多代碼,但其中大部分對解決問題沒有用處。請發佈[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –