This lesson introduces events, which are the triggers that tell the computer when to start a script.
Imagine if your TV turned on every time you clapped your hands, or your toaster started only when you did a dance. In coding, we can make these 'rules' come to life using something called Events!
In the world of computer science, an event is an action that the computer can sense. Think of it like a 'cause and effect' relationship. If you ring a doorbell (the cause), someone opens the door (the effect). In coding, the event is the cause, and the script is the effect. Without an event block, your code is like a car without a key—it has all the parts to move, but nothing to tell it to start! Most block-based coding languages use special hat-shaped blocks for events because they always sit at the very top of a script.
Quick Check
If a program is like a recipe, is the 'Event' the ingredients or the instruction to 'Start Cooking'?
Answer
The Event is the instruction to 'Start Cooking' because it triggers the process.
One of the most common ways to interact with a program is through the keyboard. We use the Key Pressed event to make things happen. For example, in a game, you might want a character to move right when the key is pressed. You can choose almost any key on your keyboard to be a trigger. When the computer detects that specific key going down, it immediately runs the blocks attached underneath that event block. This allows players to 'talk' to the computer while the program is running.
Let's make a sprite (a character) make a sound using a keyboard trigger: 1. Drag out a `when [space] key pressed` block from the Events category. 2. Snap a `play sound [Meow] until done` block underneath it. 3. Press your spacebar! Every time you press it, the count of sounds increases by .
Quick Check
Which block would you use if you wanted a character to jump only when the player hits the 'Up Arrow'?
Answer
The 'when [up arrow] key pressed' event block.
Not all events are the same! Some events happen automatically, while others require a human to do something. The `when green flag clicked` event is like the 'Master Start' button—it usually resets the game or starts the background music. The `when this sprite clicked` event is perfect for buttons or hidden items. By comparing these, you can decide how your user should interact with your project. If you have different events, you can have different scripts running at the same time!
Create a program with two different triggers to change a sprite's look: 1. Script A: Use `when [right arrow] key pressed` and attach a `change [color] effect by 25` block. 2. Script B: Use `when [left arrow] key pressed` and attach a `set [color] effect to 0` block. 3. Now, pressing Right Arrow adds color, and Left Arrow resets it to the original state.
Try to build a script where a sprite moves in a square using coordinates. 1. Use `when green flag clicked` to move the sprite to . 2. Use `when [S] key pressed` to run a script that moves the sprite steps. 3. Use `when this sprite clicked` to make the sprite say 'You found me!' for seconds.
What is the main job of an 'Event' block in coding?
If you have a script starting with `when [space] key pressed`, what happens if you press the 'Enter' key?
You can have more than one event block in a single coding project.
Review Tomorrow
Tomorrow, try to explain to a friend why a doorbell is a perfect example of an 'event' in real life.
Practice Activity
Open your coding tool and try to make a sprite grow larger when you press the 'Up' arrow and smaller when you press the 'Down' arrow.