Scratch 3 project guide

Morse Code Guess

Build a game where a lamp flashes and beeps 3 secret letters in Morse code, and you guess them. You get as many tries as you need!

What are we making?

Here's the finished game. Left: playing. Right: with the cheat sheet open.

The game stage: a lamp, the Scratch cat asking a question, a Guesses list on the right, Replay and Cheat sheet buttons
Layout sketch of the stage (480 × 360).
  1. The game picks 3 secret letters.
  2. The lamp flashes and beeps them in Morse code.
  3. You type your guess. Every guess goes in the Guesses list with ✅ (right letter, right place) or ❌ (wrong).
  4. Keep guessing until all three are ✅. Click Replay to hear them again, or Cheat sheet to peek at the codes.
  5. When you win, the cat tells you your score: how many tries and replays you used.
For the teacher Big ideas in this project: lists as a lookup table (two paired lists), going through a text one letter at a time with an index variable, custom blocks with inputs (functions), repeat until loops, and broadcast and wait to keep two sprites working together.

Morse code in 1 minute

Morse code turns every letter into short and long beeps.

  • Dot = a short beep (in our game: 0.1 second).
  • Dash = a long beep, 3 times as long as a dot (0.3 second).
  • Between dots and dashes inside a letter: a tiny silence (1 dot long).
  • Between letters: a longer silence. Real Morse uses 3 dots; we use 1 second so it's easier to hear.

Our 13 letters, click one to hear it 🔊

Your school exercise is to spell SD ISLAMIC CENTER SAMARINDA KALIMANTAN TIMUR. Those words use only these 13 letters, so the game uses only these too.

1Get the files

Open Scratch (online or the Scratch app) and start a new project. Then download these files:

Tip To upload a picture as a new sprite: hover over the cat button at the bottom right (“Choose a Sprite”) and click Upload Sprite. To add a second costume to a sprite, go to the Costumes tab and use the button at the bottom left, then Upload Costume.

2Make the sprites

We need 5 sprites and a backdrop. Names matter! Type them exactly like this, because the code uses them.

Sprite nameCostumesSoundsxyVisible?
Host
(rename “Sprite1”, the cat)
keep the cat—-175-55yes
Lampoff (lamp-off.svg)
on (lamp-on.svg)
1-dot
2-line
-17072yes
Replay Buttonreplay-button.svg—85-100yes
Cheat Buttoncheat-button.svg—183-100yes
Cheat Sheetcheat-sheet.svg—14032no (click the 👁 “hide” icon)
  1. Click the cat sprite and change its name from Sprite1 to Host.
  2. Upload lamp-off.svg as a new sprite and name it Lamp. In its Costumes tab, upload lamp-on.svg too. Rename the costumes to off and on.
  3. Still on the Lamp, open the Sounds tab and upload 1-dot.mp3 and 2-line.mp3. Delete the default sound if there is one.
  4. Upload the two buttons and the cheat sheet, and name them as in the table.
  5. Type the x and y numbers into the boxes under the stage for each sprite.
  6. Click the Stage (bottom right), open Backdrops, upload backdrop.svg, and delete the white “backdrop1”.

3Variables & lists

Go to the orange Variables category. Use Make a Variable and Make a List.

For all sprites

Any sprite can use these, so pick “For all sprites” when you make them.

NameKindWhat it's for
SecretvariableThe 3 secret letters, like SAM
TriesvariableHow many guesses you made
ReplaysvariableHow many times you clicked Replay
Solvedvariable1 when you guessed right, else 0
Playingvariable1 while the lamp is beeping
LetterslistThe 13 letters
CodeslistThe Morse code of each letter
GuesseslistYour guesses with ✅ / ❌

For this sprite only

First click the sprite, then make the variable with “For this sprite only”.

SpriteVariables
HostGuess Feedback Question i
Lampletter # symbol # letter gap
Cheat Sheetshowing

Fill the two lists

  1. Make sure the checkbox next to Letters is ticked, so the list shows on the stage.
  2. Right-click the list on the stage → import → choose letters.txt.
  3. Do the same for Codes with codes.txt.
  4. Check: item 1 of Letters is A and item 1 of Codes is .-. Item 11 is S and ...
Why two lists? They work like a dictionary. The two lists are in the same order, so the letter in place 11 of Letters (S) matches the code in place 11 of Codes (...). To find a code, we ask “where is S in Letters?”, then take the item in that same place from Codes.
#12345678910111213
LettersACDEIKLMNRSTU
Codes.--.-.-.....-.-.-..---..-....-..-

What shows on the stage

Keep only Tries, Replays and Guesses ticked. Drag Tries and Replays to the top-right corner, and drag and resize the Guesses list to fill the right side (above the buttons). Untick Secret, or the answer shows on screen! 😄

4Lamp: the “play code” block

Our own block that plays one Morse code, like -.-.

  1. Click the Lamp sprite. Go to My Blocks → Make a Block.
  2. Type play code, then click Add an input (number or text) and name it code.
  3. Leave “Run without screen refresh” unticked and click OK.
  4. Build this under the pink define block. Drag the code input from the define block when you need it.
Lamp sprite
define play code (code)
set [symbol # v] to (1)
repeat (length of (code))
  switch costume to (on v)
  if <(letter (symbol #) of (code)) = [.]> then
    play sound (1-dot v) until done
  else
    play sound (2-line v) until done
  end
  switch costume to (off v)
  wait (0.1) seconds
  change [symbol # v] by (1)
end

How does it work?

symbol # is our pointer. It starts at 1 and moves one step each time round the loop. Here's what happens for the letter C (-.-.):

Roundsymbol #letter (symbol #) of codeWhat we hear
11-long beep (lamp on 0.3 s)
22.short beep (lamp on 0.1 s)
33-long beep
44.short beep

5Lamp: play all 3 letters

Still on the Lamp. First the start-up script:

Lamp sprite
when green flag clicked
switch costume to (off v)
set [Playing v] to (0)
set [letter gap v] to (1)

Now the script that plays the secret. In the when I receive dropdown, choose New message and call it play letters.

Lamp sprite
when I receive [play letters v]
set [Playing v] to (1)
set [letter # v] to (1)
repeat (length of (Secret))
  play code (item (item # of (letter (letter #) of (Secret)) in [Letters v]) of [Codes v]) :: custom
  wait (letter gap) seconds
  change [letter # v] by (1)
end
set [Playing v] to (0)
Read the big block from the inside out If Secret is SAM and letter # is 1:
letter (1) of SAM → S → item # of S in Letters → 11 → item 11 of Codes → ... → play code ...

🧪 Test the lamp now!

Add this test script to the Lamp, then press space. You should hear and see ... .- -- (S A M).

Lamp sprite (test only)
when [space v] key pressed
set [Secret v] to [SAM]
broadcast (play letters v)

Works? Great, now delete the test script.

6Cat: the “make feedback” block

Click the Host (cat). Make a new block called make feedback with no inputs.

Host sprite
define make feedback
set [Feedback v] to []
set [i v] to (1)
repeat (3)
  if <(letter (i) of (Guess)) = (letter (i) of (Secret))> then
    set [Feedback v] to (join (Feedback) [✅])
  else
    set [Feedback v] to (join (Feedback) [❌])
  end
  change [i v] by (1)
end

It compares the guess and the secret letter by letter. Secret SAM, guess SAT → ✅✅❌.

How do I type ✅ and ❌? On Windows press ⊞ Win + . to open the emoji panel and search “check” or “cross”. On a Mac press Ctrl + ⌘ + Space. You can also just use Y and N.

7Cat: the game loop

The biggest script! It goes on the Host too. Take your time.

Host sprite
when green flag clicked
show list [Guesses v]
forever
  set [Secret v] to []
  repeat (3)
    set [Secret v] to (join (Secret) (item (pick random (1) to (length of [Letters v])) of [Letters v]))
  end
  set [Tries v] to (0)
  set [Replays v] to (0)
  set [Solved v] to (0)
  delete all of [Guesses v]
  say [Listen carefully...]
  wait (0.5) seconds
  broadcast (play letters v) and wait
  say []
  set [Question v] to [Which 3 letters did you hear?]
  repeat until <(Solved) = (1)>
    ask (Question) and wait
    set [Guess v] to (answer)
    if <(length of (Guess)) = (3)> then
      change [Tries v] by (1)
      make feedback :: custom
      add (join (Guess) (join [   ] (Feedback))) to [Guesses v]
      if <(Guess) = (Secret)> then
        set [Solved v] to (1)
      else
        set [Question v] to [Not yet! Check your guesses and try again.]
      end
    else
      set [Question v] to [Please type exactly 3 letters.]
    end
  end
  say (join [Correct! It was ] (join (Secret) (join [. Tries: ] (join (Tries) (join [, replays: ] (Replays)))))) for (4) seconds
  ask [Press Enter for a new round.] and wait
end

In the add … to Guesses block, the empty-looking box in the middle has 3 spaces typed in it, so the guess and the ✅❌ don't stick together.

The 3 parts of the loop

  1. New round. Start with an empty Secret. Three times, add a random letter from Letters. Reset the score, clear the Guesses list, and tell the Lamp to play (broadcast and wait waits until the lamp is finished).
  2. Guessing. Ask again and again until Solved = 1. Only 3-letter answers count as a try. Each try is added to the list with its feedback.
  3. You win! Show the score, then wait for Enter. The forever loop goes back to the top for a new round.
Good to know In Scratch, = doesn't care about big or small letters, so sam = SAM is true. Players can type in lowercase!

8The buttons

Replay Button

Replay Button sprite
when this sprite clicked
if <(Playing) = (0)> then
  if <(Solved) = (0)> then
    change [Replays v] by (1)
  end
  broadcast (play letters v)
end

The Playing check stops the letters from playing twice at the same time if you click very fast.

Cheat Button

Make a new message called toggle cheat sheet.

Cheat Button sprite
when this sprite clicked
broadcast (toggle cheat sheet v)

Cheat Sheet

Cheat Sheet sprite
when green flag clicked
hide
set [showing v] to (0)

when I receive [toggle cheat sheet v]
if <(showing) = (0)> then
  show
  go to [front v] layer
  hide list [Guesses v]
  set [showing v] to (1)
else
  hide
  show list [Guesses v]
  set [showing v] to (0)
end

The cheat sheet sits in the same place as the Guesses list, so we hide the list while the sheet is showing.

9Play-test checklist

Click the green flag and try each of these. Tick them off as you go.

Something wrong?

The lamp doesn't light up

Check the Lamp's costume names are exactly off and on (small letters).

No sound

Check the Lamp's sound names are 1-dot and 2-line, and the blocks in play code pick the right one: dot for ., dash for anything else.

The beeps are the wrong letter

The two lists must be in the same order. Compare them with the table in Step 3.

Nothing plays at all

Check the message name is exactly play letters in both the broadcast and the when I receive blocks.

My right guess is never “Correct”

Make sure Guess is set to answer (light blue Sensing block), and that Secret is a “For all sprites” variable.

Challenges 🚀

Finished? Try making your game even better.

  1. Get faster: change letter gap from 1 to 0.5, then 0.3 (real Morse speed!).
  2. All 26 letters: add the missing letters to Letters and their codes to Codes. Keep the same order in both! Don't forget to update the cheat sheet.
  3. Victory sound: play a happy sound when you guess right.
  4. Total score: add a Rounds won variable that counts how many rounds you've finished.
  5. 4 letters: change all the 3s to 4. Which blocks need to change? (Hint: there are three of them.)
  6. Your name: make the secret your own name instead of random letters.