In this add-on,
add a losing condition to your game
to make it more challenging and fun.
You'll use a variable to keep track of the number of lives.
If it reaches zero, you're done.
Make the variable first.
Call it lives.
Drag out a change variable block and type in negative one.
Think about how many lives a player should
start with in your game
then get a set variable block, put it above the motion loop
and type that number into the block.
That will set the number of lives at the start of the game.
This example will use a value of five.
Next, use an event block to determine when you lose a life.
Use when I receive.
When you click the drop-down,
you'll see that there's already a message called
lose a life.
You'll broadcast the message that will trigger this event.
Snap the change variable block below it.
Click the stack to test
and the value in the variable display decreases.
But how does the program know you've lost?
You need a conditional.
Put an if-else block at the bottom of the new stack.
From the operators menu get an equal-to block.
Then put the lives variable in the first blank.
Type in zero in the second blank.
If lives reaches zero, the game is over.
Next, drag out a stop block
and snap it into the then part.
Select other scripts and sprites in the drop-down.
This keeps the sprite from doing anything weird
while the program tells the user, it's game over.
Below it, snap another one.
Keep all selected.
Between these two blocks, create some game over code.
Go ahead and get creative here about how the program
should tell the player they've lost
and the game has ended.
Change the backdrop, play a sound,
or do some other fun action.
If it's not game over and the sprite just lost one life,
reset the sprite.
In the else portion of the if-else block use a go-to block
like the one on top of the motion loop
and set both of the speeds back to zero.
The hero loses a life when it falls through the cracks
or hits a bug.
In a motion loop, place another if block.
Snap in a touching block.
In the drop-down menu, pick edge.
That's for when the hero falls in a hole
and hits the edge of the stage.
If you click on the bug sprite
you'll see this third stack already broadcasts
the lose a life message.
So you don't have to worry about writing that code
for the bug.
Then broadcast the new lose a life message
inside this if block.
Test it out.
Lose some lives
and see if the game works the way you expect.
If so, congratulations.
Now, it's your turn.
Make a lives variable.
Set the variable to the number of lives
at the start of the game.
Decrease it by one every time the hero hits a bug
or the edge of the stage.
If the lives variable reaches zero,
tell the user it's game over.