You have already used events in Scratch. In fact, every script in Scratch begins when some event or other occurs. Two events that you have met are shown below,
The left hand side of the image shows the When Green Flag Clicked that starts your scripts when the main program starts. Most of the programs you have made have one main script that starts when the green flag is clicked.
The right hand side shows When Sprite Clicked block. It says tiddles here because that's what the sprite in my program was called. You used this event in the simple calculator program. When you clicked on one of the sprites, it performed a calculation.
There are 2 more types of event in Scratch, both described on this page.
When Key Pressed Event
Earlier you met a program with a car and a racetrack that looked like this,
The main program has been changed slightly to add a speed variable. Events have been added to change the speed when the Up or Down arrows on the keyboard are pressed.
Challenge: Download the program as it was before, example 6.sb. Add the variable and change the script to match the screenshot from above. Test the program - there are a few bugs to iron out. Firstly, you need to restrict the speed to no more than 10. You can do this by adding an If block to the Up arrow event. Next you need to make sure that the speed can't be lower than -10. Then make sure that the car turns properly when moving in reverse. Finally, see what else you can make happen in the program on the press of a key.
A Note About Keyboard Events
Look carefully at the two scripts in the screenshot,
There is a very subtle difference between these two scripts, both of which would move a sprite 5 steps to the left if the left arrow key were pressed. The first script uses a forever if block to see if the key is down on the keyboard - this script is always running. The second script runs once every time the key is pressed and then stops.
Both approaches acheive a similar effect. As a general rule, the forever if is best when you want your program to respond to a key being pressed for a long time - like when a player controls sprite movement in a game. The When Key Clicked approach is best when you want your program to do something once when a key is pressed - eg start and stop.
When I Receive Broadcast
A broadcast is a message that is sent through the entire Scratch program and can activate receiving scripts in stage or sprite scripts sections.
Here is a very basic space invader game. The large white rectangle is the player spaceship which moves left and right. The circle is the space alien that needs to be shot. The line is the missile that the spaceship can shoot.
The spaceship moves in a way you met in the iteration section,
This next script makes the bullet fire.
Finally, the enemy movement and the collision with the bullet.
Download and test this program - events2.sb. You will see that you can move left and right. Press the s key to shoot. When you hit the enemy, it disappears but the missile just keeps on going.
To fix this we could add a block like this to the missile script (Don't add this to your program),
This doesn't make the missile disappear as you'd expect. The reason for this is that the first script in the program detects the collision with the enemy and hides the enemy. This block doesn't pick up on that. The other problem you have is that it means creating a script like this for every enemy in the game. The alternative is to add this to the collision in the enemy script.
And then you add the following event to the missile sprite,
Add these scripts and then test the program. You should now be able to duplicate the enemy sprite and place enemies all over the place. The missile should always disappear when it hits one.
Challenge 1: Add another sprite. Place it at the top of the screen. Make a bomb for it to drop. If it is directly above the gun (has the same xposition) then it should drop a bomb (if it is not already doing so). The bomb should disappear if it hits the gun or is shot by the missile.
Challenge 2: Adapt the program so that the aliens move as a squadron - when one of them hits the edge of the screen, they all start moving in the opposite direction.
Download this race program - events challenge.sb. Use broadcasts to make the cars start and stop at the same time.