If you're seeing this message, it means we're having trouble loading external resources for Khan Academy.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Okay, but what's a game if there's no winning or losing? It's LIFE! Haha, but no, really. Let's add a score and a win state to the game. We have many options for how we could score:

  • Count how many sticks the beaver grabs.
  • Count how many sticks the beaver misses.
  • Award more points for higher accuracy - like based on distance of beaver from stick center.

Sometimes games are purely about scores and raising your score, but other times, they have distinct win or lose states. What could we do in this game?

  • Declare them a winner if they grabbed some percentage of the sticks (up to 100%).
  • Immediately fail them if they miss some percentage of the sticks.
  • Fail them if they miss some number of sticks in a row.

For simplicity's sake, let's implement the first options: we'll score based on the number of stick grabs, and we'll declare them a winner if they get 90% of the sticks.

We want to always display the score, so we can just stick a text command in the draw function:

text("Score: " + beaver.sticks, 20, 20);

For the win state, we should come up with a condition that we can check each time, and then do something festive if it's true. Here's what we could do if we wanted to make sure you got 95% of the sticks:

if (beaver.sticks/sticks.length >= 0.95) {
    text("YOU WIN!!!!", width/2, height/2);
}

Give it a go! Can you win?