Chapter 7, Exercise 3 – Level Completion Stars

Give players an incentive to repeat levels by awarding stars for a completion grade instead of the pass or fail condition that currently exists. You could award one star whenever the player clears the level, two stars if they clear with more than 25 percent of the level’s bubble allocation remaining, and three stars if they complete the level by firing only half the bubbles they were given. Add information to the level completion dialog to show the player how many stars they earned.

Firstly, we’ll need some way to display the stars in the HTML. These will go in the end game dialog box in index.html:

<div id="end_game" class="dialog">
<div id="end_game_message">
<h2>Game Over</h2>
<div id="final_score">
<span>Final Score:</span>
<span id="final_score_value"></span>
<div id="stars">
    <div id="new_high_score">New High Score!</div>
     <div id="level_failed" class="level_failed">Level Failed!</div>
      <div id="level_complete" class="level_complete">Level Complete!</div>
  <div class="but_start_game button">
     <span class="level_complete">Next Level</span>
     <span class="level_failed">New Game</span>
Now we need to calculate how many stars to award. Using the original version of the game, the player is given 70 bubbles minus five for each level they complete. We'll do the calculation in endGame in game.js:
var endGame = function(hasWon){
  var numStars = 0;
  var startingBubbles = MAX_BUBBLES - level * 5;
  var percentRemaining = numBubbles/startingBubbles * 100;
  if(percentRemaining > 50){
    numStars = 3;
  }else if(percentRemaining > 25){
    numStars = 2;
  }else if(hasWon){
    numStars = 1;
  if(score > highScore){
    highScore = score;
    score = 0;
    level = 0;
  $("#board .bubble").remove();
Note that we always give one star for completing a level, and that we're passing the number of stars to BubbleShoot.ui.endGame.
To display the stars, we'll edit endGame in ui.js:
endGame : function(hasWon,score,numStars){
  var stars = '';
  for(var i=0;i<numStars;i++){
    stars += '* ';

We’re only displaying asterisks for stars here, but if you clear a level you should see anything between one and three stars. The next step would be to create some graphics and add some style to the element so that obtaining stars feels like more of an achievement. With the logic in place, all of this can be done inside ui.js.

Leave a Reply

Your email address will not be published. Required fields are marked *