Ending the game

If the game is not ended, the game is playing, and if the player has just died or won, the game is ended. We need to know when the game is ended and when it is playing. Let's make a new member variable gameEnded and declare it after the TDView class declaration:

private boolean gameEnded;

Now, we can initialize gameEnded in the startGame method. Enter this code as the very last line in the method.

gameEnded = false;

Now, we can finish the last few lines of our game rules logic, but wrap them in a test to see if the game has ended or not. Add the following code to conditionally update our game rules logic, right at the end of the TDView class's update method:

if(!gameEnded) { //subtract distance to home planet based on current speed ...

Get Android Game Programming: A Developer’s Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.