Computer Science
Grade 5
20 min
Simple Events
Simple Events
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a 'Simple Event' is in programming.
Identify at least three different types of user-input events (e.g., mouse click, key press, sprite touch).
Create a program that uses a simple event to trigger a sequence of actions.
Explain the relationship between an event and an event handler.
Use a simple event to change the value of a variable.
Debug a program where an event is not triggering the correct action.
Have you ever wondered how a video game knows exactly when you've pressed the jump button? 🤔 Let's find out!
In this lesson, we will explore 'Simple Events,' which are like secret triggers that make our programs interactive. You'll learn how to make characters and objects in your programs respond to things li...
2
Key Concepts & Vocabulary
TermDefinitionExample
EventAn action or occurrence that a program can detect, like a mouse click or a key press.When you press the spacebar on your keyboard, that is a 'key press' event.
Event HandlerA block of code that is written to run when a specific event happens. It 'handles' the event.A block of code that says 'change score by 10' is an event handler that runs when the 'player clicks on a coin' event occurs.
TriggerThe specific action that causes an event to happen. It's the 'cause' in a cause-and-effect relationship.The user physically clicking the mouse button is the trigger for a 'mouse click' event.
User InputAny information or action that a user provides to a computer program.Typing your name, clicking on an icon...
3
Core Syntax & Patterns
The 'WHEN... DO...' Pattern
WHEN [a specific event happens] DO [a specific block of code]
This is the most common pattern for simple events. You choose a trigger event (like 'when space key pressed') and attach a script (the 'DO' part) that you want to run every time that event occurs.
Event Scope
Events are often tied to specific objects or sprites.
When you create an event like 'when this sprite clicked', the event handler code will only run if you click on that *specific* sprite, not just anywhere on the screen. This allows different objects to react to the same type of event in different ways.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A button should add 10 points when clicked, but only if the score is less than 100. If the score is 100 or more, it should do nothing. Which set of blocks inside `WHEN this sprite clicked` would achieve this?
A.'change score by 10' block.
B.An 'if score > 100' block with 'change score by 10' inside.
C.An 'if score < 100' block with 'change score by 10' inside.
D.'set score to 100' block.
Challenging
To create a complete, working 'cookie clicker' game as described in the tutorial, what are the two essential event-based scripts you need?
A.One to set the score to 0 when the green flag is clicked, and one to change the score by 1 when the cookie sprite is clicked.
B.One to make the cookie spin when a key is pressed, and one to hide the cookie when it's clicked.
C.One to set the score to 100 when the game starts, and one to set the score to 0 when the cookie is clicked.
D.Two different scripts that both change the score by 1 when the cookie sprite is clicked.
Challenging
A student's code is `WHEN green flag clicked DO change score by 10`. They run the program and click a button sprite on the screen, but the score variable does not change. Why?
A.The code is listening for the green flag to be clicked, not for the button sprite to be clicked.
B.The 'change score' block is broken.
C.Variables cannot be changed by the green flag event.
D.The score can only be changed by pressing a key.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free