This topic is now archived and is closed to further replies.


Help Please

Recommended Posts

Ok, Im on the second ghost for my pacman clone(which ive posted help for several times by now). I made the ghost have a tracking algorithm to track pacman, and i added collision detection. It works but theres only one thing. If it hits a wall, it doesnt find an alternative path to get to pacman. It just sits at the wall instead of going around it. Can anyone tell me how to resolve this(with code of course). Heres my code which is the tracking routine and the collision using a function that scans for a color(from one of Andre Lamothe''s books). // first x-axis if (pacman.x > ghost_2.x) { ghost_2.x+=4; ox=4; oy=0; } else if (pacman.x < ghost_2.x) { ghost_2.x-=4; ox=-4; oy=0; } // now y-axis else if (pacman.y > ghost_2.y) { ghost_2.y+=4; ox=0; oy=4; } else if (pacman.y < ghost_2.y) { ghost_2.x-=4; ox=0; oy=-4; } ...and the collision if (Color_Scan(ghost_2.x+2,ghost_2.y+2, ghost_2.x+ghost_2.width-2,ghost_2.y+ghost_2.height-2, 120,130,back_buffer,back_lpitch)) { ghost_2.x-=ox; ghost_2.y-=oy; } Don''t bother telling me how messy my code is, thats not my main priority right now. Thanks in advance.

Share this post

Link to post
Share on other sites
Way to linear for any sort of tracking ...
here''s a picture of your problem


Your ghost is trying to move up and left, but of course the wall is there. so it does move, but your collision code moves him back down.

A good start would be with a recursive function

something like this

bool TrackDownPacMan(int Move)
Move ghost in the passed direction

while (path not found)
Move = CheckAroundGhost() //This will check the up,down, left, right. This will return the direction to move with prioporty to move that will move us closer.
if (Move puts us on pacman)
return true; //we found a path.
//return will also pop us back up the recursive calls.

for( i = 0; i < 4; i++) //4 represents the 4 directions
if (TrackDownPacMan(i))
return true; //one of the subpaths found a way. with this direction

//since we are this point no move worked so we return false.
return false;

So basicly you do your first call checking the 4 possible moves and which ever one (could be more than one) returns true after checking the subpaths will be where you move to...

I wrote this quickly but this is good stuff


Share this post

Link to post
Share on other sites