Question

I am creating a pacman in c and currently I am usisng a single thread for each ghost and each ghost represents a '#' but when I run it all the screen gets full of ghosts and not all the ghosts move just one or two.

Im using this logic

create a struct of 5 ghost, each ghost contains the x,y position. create an array of 5 threads and each thread implements one ghost each ghost moves randomly on the screen, for each space that it moves I print a space in the old position and then I print a '#' in the new position.

Could you provide me please an example of how to implement the movement of the ghost, or the implementation Im doing is the correct way?

Thank you

Was it helpful?

Solution

One thread per agent is not a very common approach to building games. It quickly becomes unworkable for large scenes. The conventional solution is to define a state machine representing a ghost, with some kind of "advance" method that gives it a chance to adjust its internal state to the next time quantum. Create multiple instances of this state machine, and call all their "advance" methods on each iteration of the game loop. All of this can happen in a single thread.

There's quite a bit more to it than this, but it'll get you started.

OTHER TIPS

Trying to update the screen simultaneously from several threads requires a mutex around the screen update code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top