State machines
The player ship can be in a number of different states during gameplay. For example, when alive, the ship is visible and can be controlled by the player, but is vulnerable to being hit by rocks. On the other hand, when invulnerable, the ship should appear semi-transparent and immune to damage.
One way that programmers often handle situations like this is to add Boolean flag variables to the code. For example, the invulnerable flag is set to true when the player spawns, or when the alive flag is set to false when the player is dead. However, this can lead to errors and strange situations where both the alive and invulnerable flags are set to true at the same time. What happens when a rock hits the player in this situation? The ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access