Scratch Beginners – Challenge 14 – Create a Piano

This week and part of last week we created a piano! The initial keyboard looked like this:

Each white and black key is a separate SPRITE. We began by drawing the white Middle C SPRITE which has two costumes. The first costume is white and the 2nd is a different colour (you choose) that flashes up when the SPRITE is CLICKED. This helps us to see which sprite is responding to the click.

Initially, this is the script for the 1st key (Middle C):cda-s5-challenge-14-piano-key_code

Note the SET INSTRUMENT block! when you initially use this block, you must change the instrument number using the drop down arrow. If you use this method for more than one SPRITE, it gets difficult to change the instrument manually since you must do it for every sprite every time. So we created a VARIABLE called INSTRUMENT. When we create this variable, a block appears on our STAGE. Right click this variable block on the STAGE and choose SLIDER from the menu. This will create a slider look to the variable block so you can slide it up and down to change the value (see image 1). So now all you have to do is drag and drop the variable INSTRUMENT into the SET INSTRUMENT TO (_) BLOCK. (See  image 2, above).

In the PLAY NOTE block, be sure to select the proper note for the key that you want to play – Middle C note for the Middle C key and the D note for the D key and so on. Be sure to label all your Key SPRITES!!! Once you have the MIDDLE C KEY coded, you can copy it to a new SPRITE and name it NOTE D. Change the PLAY NOTE BLOCK FOR THIS KEY. You must create a new SPRITE for the black keys which is smaller and, of course, black instead of white, but it has the same scripting as the white keys. cda-s5-challenge-14-piano-sprites

You will need 8 white key SPRITES and 5 black key SPRITES. The coding is essentially the same for all of them, be sure to change the note in the PLAY NOTE block and make sure the coloured costume is switched to first.

To tie this challenge into our previous challenges, I decided to make my piano a little more user friendly. Instead of having the user move a slider back and forth to choose the instrument sound, I want to import a few more sprites that – when clicked- broadcast a message that tells the MIDDLE C key to change the variable INSTRUMENT to a specific value! cda-s5-challenge-14-piano-upload_instument_images

I used the IMPORT SPRITE FROM FILE after I went to the internet and saved a couple of cartoon images of instruments to my desktop. I found a flute, acoustic guitar, electric guitar and a saxophone. I imported the FLUTE and cleaned up it’s costume by using the paint bucket to fill the white backdrop with transparent. I then shrank the flute down and moved it to the bottom of my stage. In the script area for my flute, I dropped two blocks – WHEN THIS SPRITE CLICKED and BROADCAST (FLUTE).cda-s5-challenge-14-piano-flute_code cda-s5-challenge-14-piano-sax_code Once this message is broadcast, the MIDDLE C SPRITE needs to respond by changing the VARIABLE INSTRUMENT to the correct number for a flute (13). So the MIDDLE C sprite has new code:cda-s5-challenge-14-piano-C_key_code

Each instrument SPRITE you import broadcasts a different message to the MIDDLE C sprite. The MIDDLE C sprite responds by changing the VARIABLE INSTRUMENT to the appropriate number. Check your coding often to be sure you haven’t scrambled the instruments! As we progress in programming, neatness and good organizational skills is a MUST! In the end, my game looked like this and is on the SCRATCH.MIT.EDU website! My coder name is cdathenry1516. cda-s5-challenge-14-piano-end_game

We will be away for the next two weekends due to the hall being unavailable and the May bank holiday weekend. We are back on the 7th of May when we start writing our own programs in the pursuit of a Coder Dojo Belt for Beginners scratch!!!

Good luck,


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s