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.
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.