Sensing, Input and Operators
💻 ScratchRead the keyboard, mouse and user answers, then use operators to process them.
Sensing blocks let a project react to the player. Operators let it calculate, compare and join values.
- Detect key presses and mouse clicks.
- Ask the player for input.
- Use arithmetic and join blocks.
Keyboard and Mouse
Use key pressed? inside a forever loop for smooth movement. Use mouse down? for clicking and mouse x or mouse y to follow the pointer.
when green flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (5)
end
if <key [left arrow v] pressed?> then
change x by (-5)
end
endAsk and Answer
The ask ... and wait block shows an input box. The player's answer is stored in the answer reporter.
when green flag clicked
ask [What is your name?] and wait
say (join [Hello, ] (answer))Operators
Use +, -, * and / for maths. Use pick random for chance, round for whole numbers and join to combine text.
Practice
- Ask the player for two numbers and say their sum.
- Move the sprite with the arrow keys.
- Use pick random to place the sprite at a random x position.
Hands-on exercise
Write a script that:
1. Asks the player for a number.
2. Stores the answer in a variable.
3. Says the number doubled.
Recap
Sensing blocks read the player and the environment. Operators process the values into answers the project can use.
Example
when green flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (5)
end
if <key [left arrow v] pressed?> then
change x by (-5)
end
end
Exercises
1. Asks the player for a number.
2. Stores the answer in a variable.
3. Says the number doubled.
Expected Output
Entering 7 produces the message: Double is 14
Lesson Nav
Scratch Course
0/9 lessons completed
- 1 Your First Scratch Project Concept
- 2 Motion and Looks Blocks Tutorial
- 3 Events and Repeat Loops Tutorial
- 4 Your First Scratch Game Project
- 5 Variables and Keeping Score Tutorial
- 6 Making Decisions with Conditions Tutorial
- 7 Sensing, Input and Operators Tutorial
- 8 Clones, Timers and Levels Tutorial
- 9 Project: Build a Story Game Project