Scratch lesson 19 of 20 · Advanced · About 45–60 minutes
Turn a points rule into a small test runner. Store known answers in lists, compare them with your code and use a deliberately failing boundary case to learn how to debug.
What you’ll make Build a score calculator and an automatic report that checks six cases in one click.
Get ready
Open the Scratch editor (opens in a new tab) and start a new project.
- Start a new project. Pick a sprite and rename it Test Runner. Choose the Code tab. For this project, gems and misses are both whole numbers starting at zero.
- Create variables named Actual, Case, Passes and Failures. Create four lists called Gems, Misses, Expected and Report. Use For all sprites so you can find each variable and list easily, and leave Report, Passes and Failures visible.
- The game rule is 10 points per gem, minus 4 per miss, with a minimum of zero points. Collecting at least five gems with no misses earns 15 extra points. Keep paper nearby to work out each expected score as you set up the cases.
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. Put the scoring rule in a custom block
In My Blocks, choose Make a Block. Add two number or text inputs, called gems and misses. Use label text to make the block read calculate award (gems) misses (misses). Drag the small input reporters from the definition hat into the green calculations and conditions. This custom block puts its result into Actual. Keeping the rule in one place lets a future game and this test runner call the same code. Start with the bonus check shown here. It contains the one small mistake you will find and fix.
define calculate award (gems) misses (misses)set [Actual] to (((gems) * (10)) - ((misses) * (4)))if <(Actual) < (0)> thenset [Actual] to (0)if <<(gems) > (5)> and <(misses) = (0)>> thenchange [Actual] by (15)2. Build a helper to register a case
Make a second custom block. Add the inputs gems, misses and score. Use labels between them so the block reads case (gems) misses (misses) expects (score). Each call appends one entry to each list. The first item of every list belongs to test 1, the second to test 2, and so on. This helper keeps the three lists the same length.
define case (gems) misses (misses) expects (score)add (gems) to [Gems]add (misses) to [Misses]add (score) to [Expected]3. Reset and load six test cases
Start the main script with the green flag. Clearing the lists lets you run the same suite again without appending copies of the old cases. The first three cases check zero, an ordinary score and the lower limit. Case 4 tests just below the bonus threshold. Case 5 tests exactly five gems and no misses. Case 6 checks that a miss cancels the bonus. Work out case 5 by hand: 50 points for the gems and 15 for the bonus.
when green flag clickeddelete all of [Gems]delete all of [Misses]delete all of [Expected]delete all of [Report]set [Passes] to (0)set [Failures] to (0)set [Case] to (1)case (0) misses (0) expects (0)case (3) misses (1) expects (26)case (1) misses (4) expects (0)case (4) misses (0) expects (40)case (5) misses (0) expects (65)case (5) misses (1) expects (46)4. Compare the actual and expected scores
Snap this stack under the sixth case in the flag script. The repeat count comes from the length of Expected. For each row, use item blocks from the Variables area to fetch the matching gems, misses and expected score. The calculator runs before the comparison, so Actual contains this row's result. When a row fails, the report shows the row number and the two values. Widen the Report monitor if you need more room to read it.
repeat (length of [Expected])calculate award (item (Case) of [Gems]) misses (item (Case) of [Misses])if <(Actual) = (item (Case) of [Expected])> thenchange [Passes] by (1)add (join [PASS case ] (Case)) to [Report]elsechange [Failures] by (1)add (join (join (join [FAIL case ] (Case)) (join [ expected ] (item (Case) of [Expected]))) (join [ got ] (Actual))) to [Report]change [Case] by (1)5. Fix the boundary and run again
Click the green flag. Case 5 should report expected 65, got 50. Read the faulty check: five is equal to five, so it fails the greater than 5 comparison. The game rule says at least five. For the whole-number inputs in this lesson, that means greater than four. Replace the faulty condition in your calculation block with the condition in this diagram, then click the flag again. Getting six passes shows both the fix and that the earlier cases still produce the same scores.
if <<(gems) > (4)> and <(misses) = (0)>> thenchange [Actual] by (15)Test your project
- Before fixing the condition, check that Report contains six rows, Passes shows 5, and Failures shows 1. The failed test should be case 5, with 65 expected and 50 actual.
- After the fix, check that Passes shows 6, Failures shows 0 and all report rows start with PASS.
- Click the flag twice. The report should still have six rows each time. Its entries and counters should match the first clean run.
- Temporarily change case 2's expected score to 99. Check that case 2 fails, then put the correct value 26 back and run all six tests again.
If something goes wrong
- Several cases give unexpected scores.
- Check the first set Actual block uses the input reporters from the definition hat. Each call must replace the previous value before applying the floor and bonus.
- The last report row is missing or cases start at the wrong value.
- Start Case at 1, which is the first Scratch list index. Put change Case by 1 inside the repeat, after the result is added.
- An expected score appears to belong to a different row.
- Each case helper call must add to Gems, then Misses, then Expected. Check each list has six entries and that you cleared all three at the start.
Make it your own
- Add a seventh test for six gems and no misses. Write its expected score yourself before running the test.
- Raise the bonus to 20 points. Change the game rule and the relevant expected result, then rerun the suite. Explain why the other cases should still pass.
- Build a similar test runner for one rule in your own game, such as lives lost when a shield is active. Cover a normal example, a boundary and a case that should be rejected.
What you’ve learned A small scoring rule becomes easier to change when one custom block holds the logic and repeatable cases check its normal and boundary behaviours.