So we have Pacman jumping from node to node and that's a great first step. What we really want is to actually see him moving smoothly in between the nodes as well. In this part we'll have him move in between the nodes, but he'll always stop on each node even if a key is being pressed. This isn't the final movement for Pacman, but just the next step to fully understanding how he moves within a maze of nodes. Below is the progression of what we want to happen visually. Once we have a good visual understanding of what we want to accomplish it will be easier to program.
Here is our sample maze with Pacman in the lower right corner. There are only 2 possible directions he is able to move: UP and LEFT. We'll assume the player wants Pacman to move UP.
In this image we see that the player has pressed the UP key which makes Pacman move UP towards the nodes UP neighbor. Any other key presses won't have any effect on Pacman while he is moving in between two nodes.
Eventually Pacman will overshoot the node he is trying to reach. Once we detect this we move Pacman onto the node he was moving towards and make him STOP. You won't actually see Pacman overshoot nodes like this though. This is just to show you what it means to overshoot a node.
Here we see that Pacman is finally resting on his target node.
This new method checks to see if Pacman has overshot the target node he is moving towards. The image above shows visually how we know he has moved past his target node by just comparing the distances as described. If Pacman's distance is greater or equal to the distance between the two nodes, then we say that he has overshot the target node. Notice that we use the magnitudeSquared method because we're just comparing two distances so we can avoid taking a square root. This method will just return True or False.
In the __init__ method we'll add the target variable after defining the self.node variable. The target node is usually the node Pacman needs to move towards, but if Pacman is stationary on a node then the target node is simply None since he has no target. Also remove the keyDown variable.
In the moveByKey method we need to change the initial condition. Before, we checked if the node Pacman was on had a neighbor in a given direction. Now we just want to check if Pacman's main direction is the STOP direction. Basically we want to check to see if Pacman is resting on a node instead of moving between nodes. Once we've determined that Pacman is in fact on a node, then we can check to see if there's a valid node in the given direction. We can also remove the setPosition method because we don't want to make Pacman jump from node to node. Get rid of both references to the keyDown as that was only used in the last section. Also, instead of setting his new node to be the neighbor node in that direction like we did before, we set it as the target node. Basically, when the user presses a valid key in a valid direction and Pacman is STOPPED (which means he is resting on a node), then set the keyed direction as his new direction and set the node that is the neighbor in that direction as the target node.
In the update method we want to uncomment out the line where we update the position.
Then after we detect the key presses we'll check to see if we overshot the target node by calling the method we just wrote. If we did overshoot the target node, then we set our current node as the target node and make Pacman STOP since that's the point of this type of movement. From now on we want to have Pacman move in a direction all by himself without the player having to hold down the key.