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?
pacman
desigeek
source share