简体   繁体   中英

Add dynamically objects

I'm implementing a pacman game. One condition is that you have to add or remove enemies in runtime (while playing). My idea is to add a ghost when you press '+' and remove a ghost every time you press '-'.

Im using opengl to draw the maze and characters. I don't know how to make a new ghost(); or delete ghost(); every time you press the keys mentioned above.

Perhaps another solution would be a vector or map of ghosts. But user would not give any name or anything...

My pseudo code:

#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_ */

Ghost would be an inherited class of a particle.

I control the keyboard input in the window of the game with following code (i put some code to get the idea):

glutDisplayFunc(display);

     //TO-DO : COMMENT
     glutKeyboardFunc(keyboard);


     glutSpecialFunc(specialkeyboard);

Finally here when gamer presses the key '+' to add a ghost to the game...

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();
}

Im programming with Eclipse and C++.

Perhaps another solution would be a vector or map of ghosts

Exactly.

But user would not give any name or anything...

Since when do you need a name to push_back to vector ?

Of course, I wouldn't use pointers in this case; just push or pop a value. In case you really wanted to use pointers, don't use raw ones and new / delete ; a vector or set of std::unique_ptr<your_type> will be a much better choice.

Do not new your ghosts. Have a vector of ghost objects, and push_back an entry to the vector every time you want to make a player's life harder. When '-' is pressed, you will simply pop_back from the vector (just make sure you have at least one element there before popping).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM