If you play the game now you'll notice that Pacman and all of the ghosts start the game in the upper left corner. The ghosts will start by going to their respective corners of the maze and then begin the whole SCATTER/CHASE phases. If Pacman eats a power pellet and then touches one of the ghosts, that ghost will quickly return to the ghost home.
What we want to do in this section is to define the starting positions of the ghosts and Pacman. Refer to the image on the right.
So the reason that Pacman and the ghosts all start on the upper left corner node is because that's the node we're passing in when we create their objects. All we really have to do is pass in the correct nodes. We just have to know where they are. It's helpful to refer to the maze1.txt file and the homedata array in the NodeGroup class.
We already have a method in the NodeGroup class that will return the correct node if we pass in its coordinates. So, it's just a matter of knowing the coordinates for a particular maze. We'll explore better ways in a bit, but for now these are the coordinates for all of the starting positions. (Remember that all nodes are in (x, y) or (column, row) format:
Add these to the startGame method as shown below. For now we'll just start Pacman directly on the node at (15, 26) to make things easier. For the ghosts we need to create a new method where we can pass in the node data later after creating the ghosts.
We'll add this method to the Entity class. Whenever we define a starting node, we'll need to set it to node and target and then call the setPosition method.
You can run the game now and you'll notice that everyone starts on the nodes we want them to start on . However, Pacman just sits there on his node until the player moves him. Normally in a Pacman game, Pacman has some initial direction and moves in that direction on his own. In the original game, that direction is LEFT. So we'll simply add this line to the Pacman class to say that his initial direction is LEFT. Now when you run the game Pacman will start moving left.