Thursday, 15 March 2012

c++ - Move ghost toward player in maze if player is nearby? -


I think this is a fairly easy thing to implement, but I want the ghost to be one at a time The player should start moving ahead, if the ghost is within 5 spots of ghosts. In my statement in Move_to_x_y where the change should be added

The function is checked to see whether the ghost is in five places:

  bool game :: Pass (Ghost and Ghost) Const (if (abdomen (abdomen) M-player-> get_x) - ghost. Gate_x ())  get_y () - ghost.get_y ()) <5) true truth; return false; }  

Then there is the trick function of the ghost, it usually only runs in a random direction, which is another case for when the player is not within five places:

  Zero ghost :: move (game & game) {floor * floor; Int r; Baal went away = false; Floor = game.get_curr_floor (); What to do (if (game.nearby (* this)) r = // add here what is ??? other {r = rand ()% 4; switch (r) {case 0: moved = floor-> gtc : Move_to_x_y (play, * this, 1, 0); breakage; Case 1: shifted = floor-> gtc: move_to_x_y (game, * this, 0, -1); breakage; case 2: shift = floor-> gtc: Move_to_x_y (play, * this, -1, 0); breakage; case 3: moved = floor-> gtc: move_to_x_y (game, * this, 0, 1); break;}}} while (! Moved);} < / Code> 

So depending on where the player is, either go ahead, down, left Right, on their behalf.

Thanks for the help!

returns your function That player or not near, but does not give any information on which direction the player is in. Change the return value from the bowl to an int, or to indicate some guidelines. For example:

  int game :: pass (ghosts and ghosts) const {if (ghost.get_x () - m_player-> get_x () <5 & amp; ghost Get_x () - m_player- & gt; ; Get_x () & gt; 0) returns 2; If (m_player-> get_x () - ghost.get_x () & lt; 5 & amp; amp; amp; mm_player-> get_x () - ghost.get_x ()> 0) back; If (ghost.get_y () - m_player- & gt; get_y () & lt; 5 & amp; amp; ghost.get_y () - m_player- & gt; get_y () & gt; return 3; If (m_player-> get_y () - ghost.get_y () <5 & amp; amp; amp; mm_player-> get_y () - ghost.get_y ()> 0) return1; Return -1; }  

This will return the number that already matches the direction you want it to be in your switch statement. So whatever you have to do in your conduction ceremony, it is set to be set on "near" return, and if it gives -1, then set it in a random direction in advance.

  r = game .nearby (* this); If (r == -1) r = rand ()% 4; Switch (r) .... etc  

No comments:

Post a Comment