This course will become read-only in the near future. Tell us at if that is a problem.

Main Project Step 4 - Winning and Losing

A game isn't really a game until it has some sort of goal, whether the goal is "reach the end of the last level", "defeat the other player", "get the highest score", "get the quickest time", or just "survive as long as possible".  Even open-ended games like The Sims or Minecraft, that rely on the player setting his or her own goals, have better or worse outcomes (happy Sims vs. sad Sims; getting diamonds vs. getting blown up by creepers).

For step 4 of your main project, add a victory and/or loss condition to your game.  This means:

1. Figure out what the victory or loss condition is.  Add in any needed variables such as score, time, or health that are related to winning or losing, and add in any objects that will increase or decrease these variables.

2. Make sure that you are either checking regularly for the victory/loss condition, or else make sure that triggering it will cause a callback function to be executed somehow.

3. When a victory or loss condition has been reached, end the game. (You may need some kind of global variable or data structure to track the state of the game - is a game currently in progress or not?)

4. When the game ends, display a screen which tells the user that they won or that they lost, perhaps with a final score or final elapsed time if appropriate.  Provide some UI on this screen that the player can use to start a new game if they wish.

For example, in an action game, you will want to use collision detection (see the examples in Lesson 4) to detect whether the player has touched a monster or other deadly obstacle.  If they have, you can end the game immediately (or decrease some kind of hit-points or lives counter, and end the game when they reach 0).  You might also add some sort of flag or goal object, and have the game end with a victory screen when the player contacts it.

In a puzzle game or a strategy game, you might add the code that checks whether the puzzle has been completed or not, or the victory condition been achieved or not.

Task Discussion