Module 3 - Flow control, variables and events

Now we've got the basics of linear scripts done let's introduce some more concepts.
In this module we're going to introduce:
  • The IF control structure
  • Variables
  • Broadcasting events. 

Hit testing [Introduces concept IF...then...else]

Now we want the cat to not be allowed to hit the bat
Go to the Scripts tab of the bat sprite and change the script to be:

Adding 'lives' [introduce concept of variables)

Let's say the cat has 3 lives rather than the usual 9; we need somewhere to keep a count of how many lives the cat has left.
Programs use variables to store information they need, let's add a variable:
Variables tab
Make a variable
Called lives
Choose For All Sprites
Select the bat and the Scripts tab.
From Variables drag Set Lives to 0 to just below the When (flag) clicked hat and then change the 0 to 3.
Run game, note the lives variable changes to 3.
Now drag change lives by 1 inside the if touching <cat> block.  Change the 1 to -1
Run game.
OK, so now we've shown we can keep track of how many lives the cat has left; what we need to do is use that information; for that we're going to create our own event (just like the Scratch events such as When <left> pressed).  Our event is going to happen when the game is over.
Drag another if block below the change lives by -1 block.
Go to Number tab and choose the ( ) = ( ) block.
From Variables tab drag the lives block into the first white part of the if and edit the second part and set it to 0.
Drag broadcast inside this IF block and create a new message game over.
Drag Stop Script just below the broadcast block.
Once you're done it should look like this:    
Now we have created an event "game over" that will happen when all the lives are used up, we now need to respond to it.
All sprites get all events, so the first thing we should do is to tell the user the game is done:
Choose Stage and the Backgrounds tab then Paint a new background with 'Game Over' on it, rename it game over.
From Control drag When I receive game over onto Scripts for the stage.
Fron Looks drag Switch to background and choose game over.
From Contol drag Stop All Scripts below Switch to background.
Try it, uh oh! background starts off with the game over background. [Introduce concept of bugs]
Add new event handler to the stage scripts: When (flag) clicked with a Switch to background to the game background.
We will also need event on the cat to hide it when the game is over and also one to show it when the game starts.
Similarly the bat will need to be hidden and shown when appropriate. 
These are all good bugs to let the kids find and suggest fixes.