这是一个 C++ 控制台游戏,里面有坦克可以发射炮弹。问题是关于坦克壳部分。我想创建一个 PlayerTankShell 类的对象,并在每次按下空格按钮时将其添加到链表中。我怎样才能做到这一点 ?
这是我的代码:
#include <iostream>
#include <conio.h>
#include <list>
using namespace std;
#define ATTACK 32
class PlayerTankShell
{
int x;
int y;
int speed;
bool isExist;
public:
PlayerTankShell(bool exists)
{
isExist = exists;
}
bool getExistense()
{
return isExist;
}
};
int main()
{
char input;
input = getch();
if (input == ATTACK)
{
// Here create an object and add it to the linked list
}
// My test so far:
PlayerTankShell *s1 = new PlayerTankShell(1);
PlayerTankShell *s2 = new PlayerTankShell(1);
PlayerTankShell *s3 = new PlayerTankShell(1);
list<PlayerTankShell> listShells;
listShells.push_back(*s1);
listShells.push_back(*s2);
listShells.push_back(*s3);
list<PlayerTankShell>::iterator i;
for (i = listShells.begin(); i != listShells.end(); i++)
{
cout << "exists=" << i->getExistense() << endl;
}
return 0;
}