Making a Piano in Scratch

A quick tutorial by Nick Wright

Note: If an image is hard to see, right click and press "View image in new tab."



All you need is a computer capable of running Flash, and a Scratch account.
The first step, if not already done, is to create a Scratch account.
Once you have your account created, create a new project. The blank canvas should look like this.
Since he's not needed, you can delete Sprite1.
Now, create two variables: "note" and "instrument."
From there, drag "When green flag clicked" into the script area.
Now, add the following blocks:
  • Set (note) to 60
  • Set (instrument) to 1
  • Set instrument to 1 (in Sound)
  • Hide variable (note)
  • Hide variable (instrument)
And attach them to the green flag block. Your backdrop should be completely blank again.
Return to the events pane. Drag 10 "when space key pressed" blocks to the canvas - change each of these to Q, W, E, R, T, Y, U, I, O, and P.
Right click and duplicate one of these new blocks 4 times - change these blocks to work with the arrow keys.
Since the arrow keys are more complicated, we'll work with these first.
  • Create 4 "If (this) then (that) blocks.
  • Create 4 operator blocks - two <, and two >.
  • Attach these operator blocks to each if block.
  • Give (note) and (instrument) each one < operator and one > operator.
  • If (note < 108), change (note) by + 12
  • If (note > 12), change (note) by - 12
  • If (instrument < 21), change (instrument) by + 1
  • If (instrument > 1), change (instrument) by - 1
Your final arrow key layout should look like this.
Finally, the keys themselves. We'll start from Q.
  • For Q: Play sound (note - 4) for 0.5 beats.
  • For W: Play sound (note - 3) for 0.5 beats.
  • For E: Play sound (note - 2) for 0.5 beats.
  • For R: Play sound (note - 1) for 0.5 beats.
  • For T: Play sound (note) for 0.5 beats.
  • For Y: Play sound (note + 1) for 0.5 beats.
  • For U: Play sound (note + 2) for 0.5 beats.
  • For I: Play sound (note + 3) for 0.5 beats.
  • For O: Play sound (note + 4) for 0.5 beats.
  • For P: Play sound (note + 5) for 0.5 beats.
Your finished keys should look like this. Make sure to test the keys!

And with that, you're finished! Feel free to decorate the backdrop however you like, and enjoy showing off the fact you programmed an instrument! :>

For a finished example, click >> HERE <<