We need to be able to add text to our game so that we can display the scores, level, and any other information we want to convey to the player. The top portion of the screen is reserved for textual information in case you were wondering before. We can fit three lines of text where each character is 16 pixels high. Below is a list of all of the text that we need to display to the player while the game is being played.
This seems like next level stuff compared to what we've been doing so far.
Every text we create has a color, size, id, and x/y position. It can also have a lifespan if we only want to display it for a certain period of time. We can also make text invisible.
You can use any font you want, but the font I am using is called "PressStart2P". From there you can download it and you'll end up with a TTF file which stands for True Type Font. Again, you can use any font you want, but for an old-school game like Pacman this font is pretty much perfect.
We need to setup the font and create a label before we can use the font. The setText method allows us to change the text if we need to. In the update method we check to see if the object has a lifespan. If not, then there's no need to destroy it, it will stay on the screen forever. If it does though, then we keep it alive during that time and destroy it when that time is over. Well, we don't actually destroy it since a Python object can't destroy itself. We mark it so we know that we can destroy it and then destroy it somewhere else.
We'll create some constants for most of the text we'll need. These essentially will be keys in a dictionary we'll create in a bit. Again, like the others the values don't actually matter too much as long as they are all unique.
This probably requires a bit of explanation. So the previous class allows us to create any text on the screen. However, we'll have multiple text objects on the screen at once with some of them changing and/or disappearing and doing different things. Luckily, this is such a simple game that we already know most of the text that can and will appear in the game. So let's create a TextGroup class that will encapsulate all of that text in one place.
Most of the text can be predefined, so we have a method called setupText which will create all of the predefined text. The text that appears in the middle of the screen that displays the status such as "READY", "PAUSED", and "GAMEOVER" are all created, but hidden by setting their visible variable to false. So when we pause the game, for example, we'll simply show the "PAUSED" text. When we unpause the game we hide it again.
The text that we created the constants for is the text that we'll need to access at some point to either update it like the score text and level text, or show it like the gameover, paused, and ready text. There are 2 text messages that we'll never need access to and those are just the top 2 that simply say "SCORE" and "LEVEL". So we don't need to know what their IDs are so we just call the addText method on those two which increments the id counter and adds the text to the dictionary. Notice that I'm starting the nextid variable at 10. I could start it at 100 if I wanted, the values don't matter, just as long as they aren't overwriting the ones I need access to which I defined above as 0-4. However, the addText method returns the id that was used in storing this text message in case we do need to access it later.
The update method just updates any text that needs to be updated and removes text that needs to be removed.
We have the showText and hideText which are only for those 3 messages I mentioned above. We can only show one at a time so we simply pass in the key to the one we want to show and hide the rest.
The only text that needs to be updated on a regular basis is the score so it gets its own method here. The updateText method can update any text to a new value if needed.
Finally, like everything else we need to draw all of our text to the screen. It is assumed that everything in the alltext dictionary needs to be displayed to the screen.
So, obviously we need to import the TextGroup class and create an object out of it. We pass in the level value.
We also need to create a score variable to keep track of our total score.
We need to create a new method which just updates the score. All we have to do is pass in a value to add to the score, then update the score text on the screen to reflect that value. We'll need to call this method anywhere we update the score. So whenever we eat a pellet, a ghost, or a fruit our score increases.
In the update method we just call the textroup's update method.
In the checkEvents method when we detect the spacebar has been pressed as it should be when we're pausing and unpausing the game, we check to see if we are paused or unpaused. If we're paused then we show the pause text, otherwise we hide it.
We need to create a new method which just updates the score. All we have to do is pass in a value to add to the score, then update the score text on the screen to reflect that value. We'll need to call this method anywhere we update the score. So whenever we eat a pellet, a ghost, or a fruit our score increases.
We need to create a new method which just updates the score. All we have to do is pass in a value to add to the score, then update the score text on the screen to reflect that value. We'll need to call this method anywhere we update the score. So whenever we eat a pellet, a ghost, or a fruit our score increases.
We need to create a new method which just updates the score. All we have to do is pass in a value to add to the score, then update the score text on the screen to reflect that value. We'll need to call this method anywhere we update the score. So whenever we eat a pellet, a ghost, or a fruit our score increases.