Binary Hero
Binary Hero
Binary hero
Score points by playing the notes of a song as they
scroll past
Step 1 Introduction
In this project you will make a game in which you play the notes of a song as they scroll down the Stage.
What you will make
The notes will fall from above, and you will have to press keys to “catch” and play the notes.
Software
Scratch 3 (either online (https://rpf.io/scratchon) or offline (https://rpf.io/scratchoff))
Downloads
Offline starter project (https://rpf.io/p/en/binary-hero-go)
How many notes can you play with four keys? It might be more than you think!
Click on the sprite called ‘1’, and add code to change the sprite’s costume if the v key is pressed.
when clicked
forever
switch costume to on
else
When you test your code by pressing the v key, the sprite should light up.
Do the same for the other three sprites so that they light up if the z, x, or c keys are pressed.
Step 3 Binary numbers
You will use different combinations of pressing the four keys to play different notes. Each of the keys is either on
(pressed) or off (not pressed). This means that you can think of each combination of keys as a binary number.
Moving from right to left the keys double in value: 1, 2, 4, and 8. By adding up the numbers above the keys that are
pressed, you can work out the value of the note.
There are 24 = 16 combinations of pressing the four keys. This means that you can play 15 different notes, as 0 will
mean that no note plays.
Create a new variable called note, and drag it next to the four note sprites.
Type in the name of your variable. You can choose whether you would like your variable to be
available to all sprites, or to only this sprite. Press OK.
Once you have created the variable, it will be displayed on the Stage, or you can untick the
variable in the Scripts tab to hide it.
note will store the value of the note that should be played.
Add code to the Stage to use the combination of pressed keys to calculate the value of note.
For example, when c and v are pressed, the value of note should be 3.
I need a hint
forever
set note to 0
change note by 1
change note by 2
change note by 4
change note by 8
Step 4 Play notes
The Music section then appears at the bottom of the blocks menu.
Broadcast a ‘note change’ message whenever any of the four keys is pressed.
when clicked
forever
switch costume to on
else
I need a hint
This is what your code should look like:
Test your code. Can you hear that a note is repeatedly played when you hold down a key?
Add code so that the all the key sprites only play a note once when a key is held down.
when clicked
forever
switch costume to on
else
You need to make notes scroll down the Stage so that the player knows which keys to press and when to press
them.
Create two lists called notes and times.
Make a list
Click on Make a List under Variables.
Type in the name of your list. You can choose whether you would like your list to be available to all
sprites, or to only a specific sprite. Click OK.
Once you have created the list, it will be displayed on the stage, or you can untick the list in the
Scripts tab to hide it.
Click the + at the bottom of the list to add items, and click the cross next to an item to delete it.
New blocks will appear and allow you to use your new list in your project.
Add the following numbers to your notes and times lists. Note: make sure to add these exact
numbers in the right order.
The notes list stores the notes of the song (from 1 to 15), in order
The times list stores the times when the notes should be played in the song
You should see that the ‘note’ sprite has 15 different costume, one for each different note from 1 to 15.
Add code to create a ‘note’ sprite clone for every note stored in notes. Each clone should be created at
the correct time stored in times. Each clone should be created two seconds before its note needs to be
played. This gives the clone two seconds to move down the screen. You’ll create the code to move your
clones in a little bit!
I need a hint
This is what your code should look like:
when clicked
reset timer
hide
delete 1 of times
delete 1 of notes
When you test your code now, nothing seems to happen, because the ‘note’ sprite is hidden. If you show (or don’t
hide) the sprite, then you should see clones being created on top of each other.
Add code to make each ‘note’ clone glide from the top to the bottom of the Stage before being deleted.
go to x: 20 y: 160
show
At the moment, notes are removed from the lists after being played, so you’re left with empty lists:
You’re now going to add code to store songs in your project, so that you don’t have to add to your lists each time.
Make a new block called load 'happy birthday' that clears both the notes and times lists, and
then adds the correct numbers back into both lists.
Making a block
Making a block
Click on My Blocks, and then click Make a Block.
You will see a new define block. Attach code to this block.
You can then use your new block just like any normal block.
The code attached to your new define block is run whenever the block is used.
Making a block with parameters
You can also create blocks that have ‘gaps’ for adding data. These ‘gaps’ are called ‘parameters’.
To add parameters, first make a new block, and then click on the options below to choose the
type of data you want to add. Then give your data a name, and click OK.
You will see a new define block as usual, except that this one contains the data gap you added
and which you gave a name.
You can then use your new block, filling in data in the gap.
As usual, the code attached to your new define block is run whenever the block is used.
I need a hint
This is what your code should look like:
define load 'happy birthday'
add 1 to notes
add 5 to times
add 1 to notes
add 3 to notes
add 6 to times
add 1 to notes
add 7 to times
add 6 to notes
add 8 to times
add 5 to notes
add 9 to times
Test your new block by running it at the start of your project.
when clicked
hide
reset timer
The newest section of code is difficult to read, so you’re going to use more custom blocks to make it simpler.
Make another block called clear song that deletes all items from both lists. Use this block before
adding numbers back intothe lists.
When you test your code, it should work just as it did before.
clear song
add 1 to notes
add 5 to notes
So that your code is even easier to read, make another block that allows you to specify a note to be
played and a time to play the note at.
You can create blocks that have ‘gaps’ to add data. These ‘gaps’ are called parameters. To add
parameters, click on the options below to choose the type of data you want to add. Then give
your data a name, and click OK.
You can then define your new block, and use the data by dragging the circular blocks to use them
in your code.
Now you add data as parameters into the gaps of your new block.
Use the new define block with the gaps you have filled in by attaching code to it and adding it to
your script.
If you want to add some text between parameters, you can add label text:
I need a hint
This is what your code should look like:
define Add note note at time secs
clear song
Improve your game by giving the player points for playing the correct note.
Create a new variable called score, and place it at the top of your Stage.
Add to score whenever the player plays the correct note at the correct time. Remember to set score to
0 at the start of the game.
I need a hint
This is what your code should look like:
go to x: 20 y: 160
show
change score by 1
go to x: 20 y: 160
show
change score by 1
broadcast correct
when clicked
go to x: 20 y: 160
show
change score by 1
broadcast correct
else
???
To do this, you need to add code that’s very similar to the code that changes the backdrop when the correct note
is played. The project contains another backdrop you can use.
Step 9 What next?
Try these other projects to build you knowledge of other programming languages.
About me (https://projects.raspberrypi.org/en/projects/about-me?utm_source=pathway&utm_mediu
m=whatnext&utm_campaign=projects) is a great introduction to Python.
Happy birthday (https://projects.raspberrypi.org/en/projects/happy-birthday?utm_source=pathway&
utm_medium=whatnext&utm_campaign=projects) introduces HTML and CSS.