我正在实现吃 bean 游戏。一个条件是您必须在运行时(玩游戏时)添加或移除敌人。我的想法是在您按“+”时添加一个重影,并在您每次按“-”时删除一个重影。
我使用 opengl 绘制迷宫和角色。我不知道如何制作一个新的幽灵();或删除 ghost();每次按下上述键时。
也许另一种解决方案是鬼魂的 vector 或 map 。但是用户不会给出任何名字或任何东西......
我的伪代码:
#define MOVE 1
#define QUIET 2
class particle {
protected: //Antes era publica
float x,y; //-- Current position
float vx,vy; //-- Velocity vector
int state;
long time_remaining;
public:
particle();
void set_position(int x,int y);
void init_movement(int destination_x,int destination_y,int duration);
void integrate(long t);
void draw();
virtual ~particle();
};
#endif /* PARTICLE_H_ */
幽灵将是粒子的继承类。
我用下面的代码控制游戏窗口中的键盘输入(我放了一些代码来理解这个想法):
glutDisplayFunc(display);
//TO-DO : COMMENT
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkeyboard);
终于到了,当玩家按下“+”键向游戏中添加幽灵时...
void specialkeyboard(int key,int x,int y)
{
switch(key)
{
case GLUT_KEY_UP:
//Pacman moves up
goNorth();
//square.set_position(300,300);
//square.init_movement(300,500,1000);
break;
case GLUT_KEY_DOWN:
//Pacman moves down
// square.set_position(300,300);
// square.init_movement(300,100,1000);
goSouth();
break;
case GLUT_KEY_LEFT:
//Pacman moves to the left
goEast();
break;
case GLUT_KEY_RIGHT:
goWest();
//square.set_position(300,300);
//square.init_movement(500,300,1000);
break;
case '+':
//generate new ghost some kind of ( add ghost to the game)
new ghost();
break;
case '-':
//kill or remove ghost from the game
delete ghost;
break;
}
glutPostRedisplay();
}
我正在使用 Eclipse 和 C++ 进行编程。
最佳答案
Perhaps another solution would be a vector or map of ghosts
没错。
But user would not give any name or anything...
你什么时候需要一个名字来push_back
至 vector
?
当然,在这种情况下我不会使用指针;只需推送或弹出一个值。如果您真的想使用指针,请不要使用原始指针和 new
/delete
;一个vector
或 set
的 std::unique_ptr<your_type>
将是一个更好的选择。
关于c++ - 动态添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33218487/