Reversing Pacman's direction when he's moving between nodes is actually pretty simple. We'll just modify the updateDirection method by adding an else statement below the if statement where we check to see if Pacman is stopped or not. If he is moving, then that means he must be between nodes and we jump to the else block. Here we'll check to see if the player is trying to move in the opposite direction. Remember that self.direction is Pacman's current moving direction and direction is the direction the player is trying to move in. If we detect that the player is trying to reverse direction, then we find the node he's trying to move towards by calling the current node's getNeighborByDirection method. Then finally set Pacman's direction to the reversed direction.
pacman.py
def updateDirection(self, direction):
if self.checkValidDirection(direction):
if self.direction == pygame.Vector2():
self.direction = direction
self.node = self.node.getNeighborByDirection(direction)
self.updateValidDirections()
else: #reversing direction
if direction == -self.direction:
self.node = self.node.getNeighborByDirection(direction)
self.direction = direction
