The Accidental Press Helper
Follow instruction sequences to guide the character to the goal.
- Web browser (Chrome, Edge, Firefox)
- Internet connection
- Scratch 3.0 online editor
🔧 Build Your Accidental Press Helper
when [green flag] clicked
set [current_score v] to [0]
set [previous_score v] to [0]
say [Game ready! Press Space for +5 pts. Press U to Undo.]
when [space v] key pressed
set [previous_score v] to (current_score) // Save backup state!
change [current_score v] by (5)
say (join [Score: ] (current_score))
when [u v] key pressed
set [current_score v] to (previous_score) // Restore backup state!
say (join [Undo! Restored score: ] (current_score))
Copying state into a shadow variable before modifying it enables instantaneous rollbacks.
⚗️ Test your model
| Test Condition / Action | Expected Result |
|---|---|
| Press Space twice | Score increments to 10 points |
| Press U (Undo) | Score immediately returns to 5 points |
| Press U again | Score remains at 5; cannot undo past last saved backup |
Add an 'Undo Available' light indicator that stays green when an undo step exists and turns grey after being used!
📖 Our Story & Concepts
Have you ever clicked a button by accident on a phone or computer and deleted something you spent hours working on? In well-designed software, engineers build a safety net called Undo.
Undo works by taking a digital snapshot of your work right before any big change happens. If you make a mistake, pressing Undo simply restores the snapshot! This teaches the core engineering concept of state backups and error recovery.
</> Full Annotated Code Walkthrough
// Full Undo Script with Visual Indicator & Action History
when [green flag] clicked
set [current_score v] to [0]
set [previous_score v] to [0]
set [can_undo v] to [0]
broadcast [update_ui v]
when [space v] key pressed
set [previous_score v] to (current_score)
change [current_score v] by (5)
set [can_undo v] to [1]
play sound [Coin v]
broadcast [update_ui v]
when [u v] key pressed
if <(can_undo) = [1]> then
set [current_score v] to (previous_score)
set [can_undo v] to [0]
play sound [Boing v]
broadcast [update_ui v]
say [Restored previous score!] for (1.5) seconds
end
Copying state into a shadow variable before modifying it enables instantaneous rollbacks.
▷ Expected Results & Troubleshooting
| Test Condition / Action | Expected Result |
|---|---|
| Press Space twice | Score increments to 10 points |
| Press U (Undo) | Score immediately returns to 5 points |
| Press U again | Score remains at 5; cannot undo past last saved backup |
⚑ Extension Challenges & Blueprints
- Level 1: Add a Deduct points button (D key for -5 pts) that also supports Undo.
- Level 2: Prevent Undo from running if no score change has happened yet.
- Level 3: Build a 2-step history buffer using two previous score variables!