Enemy movement on a maze (like pacman)

Hi, I would know what’s the best way, for an enemy sprite, to search the path on a maze??
(Like pacMan)

I will appreciate code’s examples

Search for the A* (A star) algorithm or more generally “pathfinding”. It’s pretty easy if you just have a pacman-style maze. If you have a continuous world it’s somewhat more complicated. I think there’s something in the Shared code forum you might benefit from. Just a few pointers…

A* is good for pathfinding.

However, in pacman games, the ghosts probably aren’t using A*. What they do is the following:

if there's a possible turn {
   depending upon this ghost's type of ai, decide which way to turn
} else {
   keep going straight
}

That’s alot simpler than A*.

I did a pacman clone once. basically I had my ghosts run around randomly, and at each intersection, they looked all four ways. if they caught a glimpse of pacman, they’d head in that direction without question until 1) they hit a wall or 2) they see pacman again

not the best AI scheme, but it is easier than A*

I use a TileMap… so at any time I’ve to look if the tiles around the enemy are blocked or not? that is not expensive??

yeah I used a tilemap too (not an ‘official’ TileMap, just one I hacked up real fast), it isn’t very expensive at all to do that. you can optimize it in a variety of ways.

for example… don’t forget that technically your ghosts can “know” where pacman is at any time during gameplay, because the variables are all in the same scope. with this in mind, you do a quick check of where he is. so if my ghost hits an intersection, and pacman’s position is higher than the ghost, I wouldn’t bother looking DOWN for pacman.

my pacman clone was a 400x400 game, which used a 25x25 tilemap (each tile being 16x16). a grid of that size is cake when it comes to scanning around the tilemap for stuff - no performance issues at all for me :wink:

I have just implemented the ghost AI on my pac-man game and this is what I did .

test the position of the ghost with the position of the player.

if ghost is not moving . pick one of the 2 directions that will take it towards the player.

if ghost is moving but it has not moved the distance of one tile square, keep moving

if the ghost has moved the distance of 1 tile square, check the 2 possible directions the player may be, if not blocked, choose one, if blocked, continue the way it was already going.

I found that this worked very well, especially when i added a little check that if a ghost got stuck unable to move towards the player, it would keep picking a random direction until it got moving again.

hope that helps