派生 类 打印不正确
Derived Classes not printing properly
我正在制作这个主机游戏,我这样做的方式是从一个名为 map 的 3 维数组将字符打印到控制台。这是使用怪物,角色和背景的字符分配的。问题是我已经能够分配给地图数组并成功打印 char taken a monster class。但出于某种原因,派生字符 class 对象没有打印其字符。
我相信这不是访问问题,因为没有编译错误,并且当程序运行时,字符分配给的行(不是确切的索引,而是正确的行索引)上有空白。
这是分配它的代码:
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 是一个函数,它获取当前 saved/selected 字符和 returns 它所属的对象
位置
int character::getPos(bool getX) {
if (getX) {
return xPos;
} else {
return yPos;
}
}
returns 字符
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();
我测试了是否可以打印对象的位置,它对 *enemy[] 有效,派生的 classes 看起来不错。我真的不知道为什么打印不正确。也许我犯了一个愚蠢的错误,就像一些洞察力一样。
What happens when the map is printed, the bottom right empty area is meant to be full of chars
事实证明这只是我犯了一个非常愚蠢的错误。感谢 Simon Kraemer 发现问题。
我刚刚在 printScreen 方法中添加了另一个 else 来打印字符 if 语句。
顺便说一句,如果我有这样的获取位置功能有什么关系,它使用更少 space 并且使用起来更快。此外,这不是一个小组项目,所以混淆不是问题(如果我在一个小组中,也许 id 会更改它)。
我正在制作这个主机游戏,我这样做的方式是从一个名为 map 的 3 维数组将字符打印到控制台。这是使用怪物,角色和背景的字符分配的。问题是我已经能够分配给地图数组并成功打印 char taken a monster class。但出于某种原因,派生字符 class 对象没有打印其字符。 我相信这不是访问问题,因为没有编译错误,并且当程序运行时,字符分配给的行(不是确切的索引,而是正确的行索引)上有空白。
这是分配它的代码:
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 是一个函数,它获取当前 saved/selected 字符和 returns 它所属的对象
位置
int character::getPos(bool getX) {
if (getX) {
return xPos;
} else {
return yPos;
}
}
returns 字符
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();
我测试了是否可以打印对象的位置,它对 *enemy[] 有效,派生的 classes 看起来不错。我真的不知道为什么打印不正确。也许我犯了一个愚蠢的错误,就像一些洞察力一样。 What happens when the map is printed, the bottom right empty area is meant to be full of chars
事实证明这只是我犯了一个非常愚蠢的错误。感谢 Simon Kraemer 发现问题。
我刚刚在 printScreen 方法中添加了另一个 else 来打印字符 if 语句。
顺便说一句,如果我有这样的获取位置功能有什么关系,它使用更少 space 并且使用起来更快。此外,这不是一个小组项目,所以混淆不是问题(如果我在一个小组中,也许 id 会更改它)。