Ghost movement on pacman game

Hi, I’m doing a maze game like pacman and I’m writing the ghost class.

My question is: if I want that ghosts moves to the player direction what I’ve to do?
My solution is

a - compare the x and y values of ghosts and player
if(ghost.x > player.x) OVEST movement else EST
if(ghost.y > player.y) NORD movemente else SUD
and add this directions into preferredDir list

b - check all the possible directions of movement EST(x+1,y), OVEST(x-1,y), SUD(x,y+1), NORD(x,y-1)
and add this directions into possibleDir list

c - I compare the preferredDir list with possibleDir and
no match - i’ll choose a casual direction from possibleDir
1 match - i’ll choose this direction
2 match - i’ll choose a casual direction from preferredDir

It’s correct??

Wouldn’t hurt to try.

In an open maze with many connections, moving towards player will do. In a closed maze with dead ends etc, the optimal path might at first lead away from the player. A full scale A* Pathfinding is required for a perfect path.

If it were me, I’d store which direction the ghost is currently moving. If it’s moving vertically, I would make it keep moving the same direction unless it came to a place where it could move horizontally. And if it’s moving horizontally, it would be the same except switched.

That way, the ghosts only turn at spots where there’s a turn. With your way, I think you could have a situation where there’s a wall between the ghost and the player and the ghost just kind of stalls. You could have an additional check for when the player is right next to the ghost that lets it turn around and hit the player.

There might be additional problems if you’re using a sprite-based approach instead of a tile-based approach.

In Pacman, the different ghosts usually have different behavior.

I haven’t played a lot of pacman, but don’t the ghosts only chase the player if they have line-of-sight, and otherwise move randomly?

The 4 ghosts have different rules. I cant recall them all. One just wanders randomly, one chases the player, one stays around the center and one does something else. It was easy but worked. The center ghost is always a problem when the player wants to get from one part of the maze to the other. The chaser would always put pressure on the player. The random walker usually stayed in the same area and made it hard to collect the dots there.

I am not sure about the chaser, I think he did not exactly pathfind to the player. On each crossroad he would just choose the direction which brings him closer towards the player position. This way the player could trick him to take a certain way that would actiually lead him to a bad position.

-JAW