Falling Stars

Losing the Game

First things first! You need a way to make the game end when the player has run out of lives. At the moment that doesn’t happen.

You may have noticed that the lose My blocks block in the scripts for the Player Character sprite is empty. You’re going to fill this in and set up all the pieces needed for a nice ‘Game over’ screen.

First, find the lose block and complete it with the following code:

definelosestopother scripts in spritebroadcastgame overgotox:0y:0sayGame over!for2secondsstopall

What does this code do?

Whenever the lose block runs now, what it does is:

  1. Stop the physics and other game scripts acting on the Player Character
  2. Tell all the other sprites that the game is over by broadcasting a game over message they can respond to and change what they’re doing
  3. Move the Player Character to the centre of the Stage and have them tell the player that the game is over
  4. Stop all scripts in the game

Now you need to make sure all the sprites know what to do when the game is over, and how to reset themselves when the player starts a new game. Don’t forget that any new sprites you add also might need code for this!

Hiding the platforms and edges

Start with the easiest sprites. The Platforms and Edges sprites both need code for appearing when the game starts and disappearing when they receive the game over broadcast, so add these blocks to each of them:

whenIreceivegame over hide
whenclickedshow

Stopping the stars

Now, if you look at the code for the Collectable sprite, you’ll see it works by cloning itself. That is, it makes copies of itself that follow the special when I start as a clone instructions.

We’ll talk more about what makes clones special when we get to the step about making new and different collectables. For now, what you need to know is that clones can do almost everything a normal sprite can, including receiving broadcast messages.

Look at how the Collectable sprite works. See if you can understand some of its code:

whenclickedhidesetcollectable-valueto1setcollectable-speedto1setcollectable-frequencyto1setcreate-collectablestotruesetcollectable-typeto1repeatuntilnotcreate-collectables=truewaitcollectable-frequencysecondsgotox:pickrandom-240to240y:179createcloneofmyself
  1. First it makes the original Collectable sprite invisible by hiding it
  2. Then it sets up the control variables — we’ll come back to these later
  3. The create-collectables variable is the on/off switch for cloning: the loop creates clones if create-collectables is true, and does nothing if it’s not

Now set up a block for the Collectable sprite so that it reacts to the game over broadcast:

whenIreceivegame overhidesetcreate-collectablestofalse

This code is similar to the code controlling the Platforms and Edges sprites. The only difference is that you’re also setting the create-collectables variable to false so that no new clones get created when it’s ‘Game over’.

Note that you can use the create-collectables variable to pass messages from one part of your code to another!