An important part of any Pacman game is the ability to portal from one side of the screen to the other side. In the original Pacman game, when you reach the edge of the screen on the middle path you're sent to the other side of the screen. In some of the mazes in Ms Pacman there were several locations on each side of the screen where one could portal to the other side of the screen. Most of the portals in Pacman are horizontal portals, but there's no reason why we can't portal vertically as well. It's actually pretty easy to do this with the node setup we currently have.
What we're going to do here is add a new neighbor type. Before we had UP, DOWN, LEFT, and RIGHT neighbors. Now we can have a neighbor that we can jump to, or portal to. So, we'll add that to the end of our neighbors dictionary and give it a value of None.
We'll need to define a value for the PORTAL constant though. It really doesn't matter what value we assign to it, as long as it isn't a value we're already using to represent the other guys. How about 3? That works. Another number that would work is 9283. Why not? We're not actually using the number, it just needs to be different than the others because that's how dictionaries work. Just add this to the constants.py file.
So portals come in pairs. I mean, it is possible to have 1-way portals, but in Pacman they're all 2-way portals. So we're going to create a new method in the NodeGroup class that takes 2 tuple values like (1, 5) and (6,9) for example, and checks to see if those are in the nodes lookup table or not. If not, then no harm we just move on. But if they are then we connect them together using the PORTAL key.
In the GameController class we need to call this new method after creating the nodes object. We then pass in these two tuples. Where did these values come from? These are just the (row, column) values of the two nodes I want to connect together. I'm hard-coding them in for now, but we'll need a better solution when we start to introduce more mazes and especially when you start creating your own mazes.
Now we need to tell Pacman to jump from one node to the next if he encounters a node with a portal. This isn't hard to do, when Pacman overshoots a node we set that node as his current node and then find the next target node. Before we try to find that next target node we check to see if this new node is a portal node or not. If not then we move on like normal. If it is, then we set that nodes portal node as the new node instead.