Scratch lesson 13 of 20 · Advanced · About 50–60 minutes
Turn a room number and a star count into a three-character code. Write a validator that loads a good code and leaves current progress intact when someone mistypes it.
What you’ll make A mini game menu that encodes progress as a short code such as Q25 and restores the room and stars from a valid code.
Get ready
Open the Scratch editor (opens in a new tab) and start a new project.
- Start a new project with a sprite named Game Guide. Give it a clear backdrop so messages are easy to see.
- Make two variables called Room and Stars. Leave both monitors visible. The game has rooms 1 to 3 and a star count from 0 to 5.
- Make five more variables called Menu choice, Save code, Code OK, Trial room and Trial stars. Keep those monitors hidden, except for Save code if you would like to copy the result from the Stage.
- The format always puts Q first, the room second and the stars third. Q25 means room 2 with 5 stars. The code holds only game progress and can be written on a piece of paper.
To keep your project, choose File → Save to your computer in Scratch.

Build it in Scratch
Choose the sprite named above each stack. The colours help you find the block categories. Indented rows sit inside the block above. Follow the end marker to see where each loop or condition closes.
1. Set the starting progress
Place this short green-flag stack on Game Guide. Every new game begins in Room 1 with zero stars and an empty Save code. Click the flag to check the Room and Stars monitors. You will attach the menu in step 4. Having a known reset makes the later save-and-load tests easy to repeat.
when green flag clickedset [Room] to (1)set [Stars] to (0)set [Save code] to []2. Join the pieces into one code
Make a custom block called make save code without any inputs. The inner green Join reporter combines Q and Room. Drag that whole reporter into the left slot of another Join and put Stars in the right slot. Join sticks values together as text. The custom block puts the result into Save code, then displays it long enough to copy. The code records the values at the moment you ask for it. If the values change later, make a fresh code to save the new position.
define make save codeset [Save code] to (join (join [Q] (Room)) (Stars))say (join [Your code is ] (Save code)) for (5) seconds3. Check each character before loading it
Make a block named load code with a number or text input called entry. Begin by setting Code OK to 0. Use the green text Length reporter to check for exactly three characters. Inside that check, use Letter 1 to look for Q. Scratch’s text comparison also accepts a lowercase q. Then use Letter 2 and Letter 3 to store the proposed values in Trial room and Trial stars. The two green text Contains reporters check the allowed digits, one against 123 and one against 012345. Put both tests in the two sides of an And block. Only set Code OK to 1 when both tests pass. The final if/else sits underneath the whole check. It updates the real progress only after the code has passed.
define load code (entry)set [Code OK] to (0)if <(length of (entry)) = (3)> thenif <(letter (1) of (entry)) = [Q]> thenset [Trial room] to (letter (2) of (entry))set [Trial stars] to (letter (3) of (entry))if <<[123] contains (Trial room)?> and <[012345] contains (Trial stars)?>> thenset [Code OK] to (1)if <(Code OK) = (1)> thenset [Room] to (Trial room)set [Stars] to (Trial stars)say [Progress loaded.] for (2) secondselsesay [Check your three characters and try again.] for (2) seconds4. Build a menu with safe limits
Put this loop at the end of the green-flag stack from step 1. The first two branches change the game state, with limits that keep each field within its allowed range. If you ask to load, the second Ask block waits for a code. Drag its Answer reporter into the input of load code. The two Ask blocks belong to the same running stack. Scratch finishes the chosen branch before it asks for the next menu number.
foreverask [1 Next room | 2 Gain a star | 3 Make code | 4 Load code] and waitset [Menu choice] to (answer)if <(Menu choice) = [1]> thenif <(Room) < (3)> thenchange [Room] by (1)elseif <(Menu choice) = [2]> thenif <(Stars) < (5)> thenchange [Stars] by (1)elseif <(Menu choice) = [3]> thenmake save codeelseif <(Menu choice) = [4]> thenask [Enter your three-character game code.] and waitload code (answer)elsesay [Choose 1, 2, 3 or 4.] for (1) secondsTest your project
- Choose 1 once and 2 three times. Room should be 2 and Stars should be 3. Choose 3 and copy Q23.
- Click the flag to reset. Choose 4 and enter Q23. Check both monitors return to room 2 and 3 stars.
- Load Q35, then Q10. These check the highest valid digits and a zero in the star slot.
- While room 1 and zero stars are on screen, try Q49, Q2X, X23 and Q23Q. Each should show the error message. Both monitors should stay the same.
- Keep choosing 1 and 2 to reach the maximums. Room should stop at 3 and Stars at 5. Your next code should still be three characters long.
If something goes wrong
- A mistyped code changes one of the monitors.
- Store each proposed digit in a Trial variable first. Put both real progress updates inside the final Code OK branch.
- The saved value looks like a sum.
- Check that both green blocks shaping the code are Join blocks. Arithmetic changes numbers, while Join places text pieces side by side.
- The loader accepts a code with an extra letter.
- Make sure the outer test checks the length of entry before reading its first three characters and that Code OK resets to zero on each call.
Make it your own
- Add a fourth character to save the player’s badge count from 0 to 3. Update the constructor, length check, validator and test cases together.
- Write three valid codes and three mistaken codes for a friend to try. Ask them to explain which test each mistaken code fails.
What you’ve learned Encoding puts progress into a known text format. Decoding reads that format back. Validating every field before changing the game protects the current state.