The Accidental Press Helper

Grade 3 Scratch 3.0 Simulation first The Power to Undo
Simulation lab
Tool: Scratch 3.0
Open Scratch ↗
Simulator preview

Follow instruction sequences to guide the character to the goal.

📁 Tools ▴
  • Web browser (Chrome, Edge, Firefox)
  • Internet connection
  • Scratch 3.0 online editor

🔧 Build Your Accidental Press Helper

1 Create two variables: 'current_score' and 'previous_score' initialized to 0.
2 When the Score Button (Space) is pressed, first store current_score into previous_score.
3 Add +5 points to current_score and display the updated score.
4 Program the Undo key (U) to copy previous_score back into current_score, reversing the mistake!
🧩 Block plan / Starter code ▴
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 / ActionExpected Result
Press Space twiceScore increments to 10 points
Press U (Undo)Score immediately returns to 5 points
Press U againScore remains at 5; cannot undo past last saved backup
⚑ Try a challenge

Add an 'Undo Available' light indicator that stays green when an undo step exists and turns grey after being used!

❓ Quick check
Why do good software designers add an 'Undo' option to important actions?

📖 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 / ActionExpected Result
Press Space twiceScore increments to 10 points
Press U (Undo)Score immediately returns to 5 points
Press U againScore remains at 5; cannot undo past last saved backup

⚑ Extension Challenges & Blueprints

⚑ Extension Challenges
  • 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!

❓ Check Your Understanding

Question 1: Why do good software designers add an 'Undo' option to important actions?

Question 2: In our project, what does the 3-second countdown represent?

Question 3: What variable tracks whether a choice is currently waiting to be confirmed?

Question 4: What should happen to the countdown if the user clicks the Undo button?

Feedback appears after you check your answer.