Keeping score during the game
We have a simple system that increases Score of Player as it touches Good Orbs and takes Lives from Player as it crashes into Bad Orbs. When the Score reaches a set number, we win. When Player Lives decreases to zero, we lose.
When we lose, we have the option to retry the Scene. To do this, the system needs to remember what the Score and Player Lives were when first entering the Scene so they can be reset back to these values.
The following two screenshots show the code for initializing the score:
Initializing the scoring system
The following steps occur while the game is in the setup State of the game.
An analysis of the code shown in the preceding two screenshots is as follows:
In the GameData
file:
Step 1:
From Chapter 9, Start Building a Game and Get the Basic Structure Running, we already have the variables for keeping track of the Score and Player Lives during gameplay:
- The
playerLives
variable on line 16 - The
score
variable on line 18We need two more variables...