One thing that we all know about Pacman is that he eats a lot of pellets. Pellets are placed throughout the maze and it's Pacman's primary purpose in life to eat all of the pellets. In fact, that's the only way we know that the player completed a level. Once all of the pellets are gone, then the level is over and we can move on to the next level. There are also 4 big power pellets at each of the four corners of the maze. If Pacman eats one of these power pellets then the ghosts become vulnerable and Pacman can eat the ghosts. Right now let's just worry about how to get the pellets in the maze to begin with.
If you were to count up all of the power pellets in the original Pacman game, then there would be a total of 244 pellets. 240 of those pellets are regular pellets and are worth 10 points each. The other 4 pellets are the power pellets and are worth 50 points each. So just by finishing the level you'll get 2600 points. That's the minimum number of points you'll receive for just finishing a level.
If you were to also look at the original Pacman game, you'll notice that all of the paths don't have pellets. Some paths, like the paths surrounding the ghost home, don't have any power pellets. So we can't tell the game to fill any empty spot with pellets. I mean you could, an easy way to fill in the pellets would be to say wherever there's a '+' or a '.' symbol place a pellet there as well. But we need to be able to say where the power pellets go as well. And like I said before, maybe we don't want to place pellets everywhere. Here's what we'll do:
The difference between 'p' and 'P' is that 'p' is placed on a path and 'P' is placed on a node. Seems simple enough right? But now we'll need other symbols that also represent nodes and paths other than '+' and '.' since we're not placing pellets everywhere. Here's what we'll do:
So let's create modify our maze1.txt file and place the pellets into this file.
We are going to create three new classes that deal with the pellets: Pellet, PowerPellet, and PelletGroup. Create a new file called pellets.py.
The Pellet class really doesn't do a whole lot. A pellet is just a white circle that has a radius of 4 pixels. We also specify how many points a pellet is worth: 10 points. The visible variable just allows us to hide a pellet if we want.
The render method is the same as Pacman's render method, but we only draw it if the pellet is visible. When will we ever make the pellet not visible? Let's talk about the Power Pellet next.
The PowerPellet class defines the larger pellet that gives Pacman the special power to eat the ghosts. For now the only difference is that it is larger and is worth more points than the regular pellet.
We also have a timer for this class. The reason is because we want the power pellets to flash on and off. It's just a visual effect that makes it easier for the player to notice the power pellets more. So with the timer and a few other variables we will turn the power pellets on and off every 0.2 seconds.
We're going to have a lot of pellets on the screen, so it's a good idea to manage them and keep them organized. We'll never have to call the previous two classes directly, they'll get called through this class. This is sort of similar to the NodeGroup class in that it groups all of the pellets together in a list. Creating the pellets is easy. We just read the file line by line and create a pellet at that position based on the symbol we defined earlier. When we want to draw the pellets we just call this render method and it will take care of drawing the pellets for us.
The pelletList just stores all of the pellets including the power pellets, and the powerpellets list just stores the Power Pellets. We do this to make it easier to flash the power pellets essentially. That way we don't have to loop through all of the pellets just so we can make the powerpellets flash. Might as well just loop through the powerpellets since they're the only ones that need updating.
Also notice that we have an isEmpty method that checks to see when the pelletList is empty. We're not using this yet, but we will when we want to determine when Pacman has cleared a level.
We just need to add a few more node and path symbols to reflect the new maze text file.
First off we need to import the PelletGroup class that we just created.
In the startGame method we'll create a PelletGroup object and pass in the pellets1 text file so that it will know where to create the pellets.
In the render method we'll draw the pellets. Draw them before drawing Pacman so that the pellets appear below Pacman.