Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Pacman do the ghosts choose paths independently for finding pacman?

Tags:

pacman

So I have been playing a lot of pacman on my cell lately and am wondering how do the ghosts seem to work independent of each other. I was thinking about how it would have been programmed.

One option that I thought of was threads. All 4 ghosts are running in their own threads and somehow find pacman's position. But it seems a bit to much to have four threads working and syncing would be hard. Also, google wrote pacman in Javascript which doesn't support threads, so it can be done without threads and there has to be an easier way.

My second thought was event handlers. I just wire the 'directionChanged' event that pacman will fire to 4 event handlers, one for each ghost. Each ghost then decides what path to take to get to pacman. This I think is more likely what is happening. But it can get slow if the event handlers are synchronously executed, because paths will have to be calculated sequentially and the 4th ghost will take time to change direction and this might create a visible lag (probably). Also, the ghosts would fire an event themselves when they hit a wall and their event handlers would change the ghosts direction. But given the frequency with which pacman changes directions and the four ghosts respond, event handlers also seem a bit too much.

I am saying the above ideas would be a bit too much because remember the game was written 30 years ago when cpu time and memory were scarce, so I think there has to be a much easier way.

Also, it seems that the ghosts are following different paths even when pacman is still. Do all the ghosts use completely different or differently optimized path finding algorithms?

I am more interested in finding out how all the ghosts seem to work for themselves simultaneously than the path finding algorithms they use. Thoughts?

like image 673
Punit Vora Avatar asked Aug 21 '10 02:08

Punit Vora


People also ask

How do the ghosts work in Pac-Man?

The ghosts in Pac-Man always leave the ghost house to the left, however, due their programming, they can immediately change direction as soon as leaving depending on where you are. In some levels of the game there are “Safe Zones” on the maps where you can hide and, due to the ghost's programming, will never be caught.

Are the ghosts in Pac-Man actually ghosts?

These cutscenes have mostly been swept under the carpet; Namco themselves never reference them; but the fact remains, the “ghosts” aren't actually ghosts. More than $3.5 billion has been pumped into Pac-Man arcade machines. Obviously Pac-Man was a successful game, but it's hard to conceive just how big a deal it was.

Who is the smartest ghost in Pac-Man?

In Pac-Man Party and Ghostly Adventures, Inky has little to no resemblance to his original design; he is very skinny, has twirly hair, and teeth. He is also a darker shade of blue, and is portrayed as being the smartest of the four ghosts.

How do you do the ghost glitch in Pac-Man?

On the 20 Year Reunion arcade machines, put in a coin, then, on the Player 1 joystick, enter: Up, Up, Up, Down, Down, Down, Left, Right, Left, Right, Left. If the ghost on the Game Select screen turns pink and a sound plays then you did it right.


Video Answer


2 Answers

There's a lot of great information about how pacman works here including some detailed write ups about the ghosts behavior.

**Note I found this information from Pathfinding Algorithm For Pacman a while ago.

EDIT:

This blog post has even more information about the ghosts.

like image 130
GWW Avatar answered Sep 20 '22 21:09

GWW


You're drastically over-thinking this.

Threads and event-handlers are terribly slow by video game standards. Multi-threaded game engines are a relatively new invention, literally decades after Pacman was released. Pacman's meager logic will occur inside a single fairly tight loop, something like this very simplified pseudocode:

while (!pacman_dead) {
  foreach ghost {
    if (ghost has hit a wall) {
      if (pacman to left) turn left
      if (pacman to right) turn right
    } else {
      go straight
      if (ghost touched pacman) {
        pacman_dead = true
      }
    }
  }

  handle_input();
  move_pacman();
  draw_screen();      
}

This is a fairly common pattern in gaming, and it gives the appearance of concurrence. Typically a game will run in a single loop, which advances the game state by some tiny increment in the space between screen redraws. This is why performance is still very important in game development: Your game must iterate over every in-game object which needs to do something or make some decision (AI-controlled opponents, moving platforms, simple animations, etc) and update their state at least 30 times per second.

like image 37
meagar Avatar answered Sep 22 '22 21:09

meagar