In Pacman, ghosts choose their own paths to search for pacman?

So, Iโ€™ve been playing a lot in my cell lately, and Iโ€™m wondering how ghosts work independently. I was thinking about how this would be programmed.

One of the options I was thinking about is threads. All 4 ghosts work in their threads and somehow find the pacman position. But, it seems, a little to work with four streams, and synchronization will be difficult. In addition, google wrote pacman in Javascript, which does not support threads, so it can be run without threads, and there should be an easier way.

My second thought was an event handler. I just hook up the "directionChanged" event, which pacman will fire for 4 event handlers, one for each ghost. Then each ghost decides which path to take to get to pacman. This, I think, is more likely to happen. But it can slow down if event handlers are executed synchronously, because the paths must be calculated sequentially, and the fourth ghost will take time to change direction, and this can create a visible lag (possibly). In addition, ghosts will trigger the event themselves when they hit the wall, and their event handlers will change the direction of the ghosts. But given the frequency with which pacman changes direction and responds to four ghosts, event handlers also seem a bit overkill.

I say that the above ideas would be too much, because remember that the game was written 30 years ago when the CPU time and memory were scarce, so I think it should be much simpler.

In addition, ghosts seem to follow different paths, even when the pacman is still. Do all ghosts use completely different or differently optimized path finding algorithms?

I'm more interested in learning how all the ghosts seem to work for themselves at the same time than the path finding algorithms they use. Thoughts?

+7
pacman
source share
3 answers

You decidedly changed your mind about it.

Streams and event handlers are terribly slow by video game standards. Multithreaded game engines are a relatively new invention, literally decades after the release of Pacman. Smaller Pacman logic will happen inside one pretty tough 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 picture in games, and it gives the appearance of coincidence. Usually the game starts in one cycle, which โ€œstrikesโ€ the game state forward with a small increase in the interval between redrawing the screen. That's why performance is still very important in game development: your game must go through every game object that needs to make something or make some decision (AI-controlled opponents, moving platforms, simple animations, etc.) and update their status at least 30 times per second.

+6
source share

There's a lot of great information on how pacman works here , including some detailed notes on ghost behavior.

** Note. I found this information from Pacman Path Finder some time ago.

EDIT:

This post blog has even more ghost info.

+13
source share

Before rendering each frame, I will iterate over the ghosts. No events, threads or asynchronous problems. Each ghost probably has a very rough heuristic for clogging its moves and making decisions (turning left or right, no action, no change).

You can check here. :)

+1
source share

All Articles