Please update your browser
Your browser version is not supported by this site. CS First may not work properly.
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 0, you’re done!
Make the variable first.
Call it “lives.”
Drag out a “change variable” block, and type in negative 1.
While you’re in the “data” menu, 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 uses a value of 5.
Next, use an “event” block to determine when you lose a life.
Use “when I receive.”
When you click the dropdown you’ll see there’s already a message called “lose a life.”
You’ll broadcast a 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, from “data,” put the
“lives” variable in the first blank.
Type in 0 in the second blank.
If lives reaches 0, the game is over.
Next, drag out a “stop” block, and snap it into the “then” part.
Select “other scripts in sprite” in the dropdown.
This keeps the sprite from doing anything weird while the program tells the user it’s
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 at the
top of the motion loop, and set both of the speeds back to 0.
The hero loses a life when it falls through the cracks or hits a bug.
In the motion loop, place another “if” block.
Snap in a “touching” block.
In the dropdown, 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 1 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.