0% found this document useful (0 votes)
2 views

Binary hero

The document outlines a project to create a game called 'Binary Hero' using Scratch 3, where players score points by pressing keys to play notes as they scroll down the screen. It details the necessary hardware and software, the steps to implement key presses, binary number combinations, and how to manage notes and timing through lists. Additionally, it guides users on creating custom blocks to streamline the coding process for storing and playing songs within the game.

Uploaded by

12307-003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Binary hero

The document outlines a project to create a game called 'Binary Hero' using Scratch 3, where players score points by pressing keys to play notes as they scroll down the screen. It details the necessary hardware and software, the steps to implement key presses, binary number combinations, and how to manage notes and timing through lists. Additionally, it guides users on creating custom blocks to streamline the coding process for storing and playing songs within the game.

Uploaded by

12307-003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

11/22/24, 1:50 PM Binary hero

Projects

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.

What you will learn


How to use lists to store sequences of notes and timings
How to use custom blocks with inputs

https://projects.raspberrypi.org/en/projects/binary-hero/print 1/38
11/22/24, 1:50 PM Binary hero

What you will need


Hardware
A computer capable of running Scratch 3

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)

Additional notes for educators

You can find the completed project here (https://rpf.io/p/en/binary-hero-get).

https://projects.raspberrypi.org/en/projects/binary-hero/print 2/38
11/22/24, 1:50 PM Binary hero

Step 2 Key presses

How many notes can you play with four keys? It might be more than you think!

Open the ‘Binary hero’ Scratch starter project.


Online: open the starter project at rpf.io/binary-hero-on (https://rpf.io/binary-hero-on). If you have a
Scratch account, you can click on Remix in the top right-hand corner to save a copy of the project.
Offline: open the starter project (https://rpf.io/p/en/binary-hero-go)in the offline editor. If you need to
download and install the Scratch offline editor, you can find it at rpf.io/scratchoff (https://rpf.io/scratchof
f).

Start by showing which key is being pressed.

https://projects.raspberrypi.org/en/projects/binary-hero/print 3/38
11/22/24, 1:50 PM Binary hero

Click on the sprite called ‘1’, and add code to change the sprite’s costume if the v key is pressed.

when clicked

forever

if key v pressed? then

switch costume to on

else

switch costume to off

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 4/38
11/22/24, 1:50 PM Binary hero

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 5/38
11/22/24, 1:50 PM Binary hero

Create a new variable called note, and drag it next to the four note sprites.

Add a variable in Scratch


Click on Variables in the Code tab, then click on Make a Variable.

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 6/38
11/22/24, 1:50 PM Binary hero

note will store the value of the note that should be played.

https://projects.raspberrypi.org/en/projects/binary-hero/print 7/38
11/22/24, 1:50 PM Binary hero

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

This is what your code should look like:

https://projects.raspberrypi.org/en/projects/binary-hero/print 8/38
11/22/24, 1:50 PM Binary hero

when clicked

forever

set note to 0

if key v pressed? then

change note by 1

if key c pressed? then

change note by 2

if key x pressed? then

change note by 4

if key z pressed? then

change note by 8

https://projects.raspberrypi.org/en/projects/binary-hero/print 9/38
11/22/24, 1:50 PM Binary hero

Step 4 Play notes

Play notes when the keys are pressed.

https://projects.raspberrypi.org/en/projects/binary-hero/print 10/38
11/22/24, 1:50 PM Binary hero

Add the Music extension to your project.

How to add the Music extension


To use the Music blocks in Scratch, you need to add the Music extension.

Click on the Add extension button in the bottom left-hand corner.

Click on the Music extension to add it.

The Music section then appears at the bottom of the blocks menu.

https://projects.raspberrypi.org/en/projects/binary-hero/print 11/38
11/22/24, 1:50 PM Binary hero

https://projects.raspberrypi.org/en/projects/binary-hero/print 12/38
11/22/24, 1:50 PM Binary hero

Broadcast a ‘note change’ message whenever any of the four keys is pressed.

when clicked

forever

if key v pressed? then

switch costume to on

broadcast note change

else

switch costume to off

https://projects.raspberrypi.org/en/projects/binary-hero/print 13/38
11/22/24, 1:50 PM Binary hero

Add code to the Stage to play a note when a combination of keys is pressed.
Your notes should start at middle C, which is note 60.

play note 60 for 1 beats

I need a hint
This is what your code should look like:

when I receive note change

stop all sounds

play note 59 + note for 1 beats

Test your code. Can you hear that a note is repeatedly played when you hold down a key?

https://projects.raspberrypi.org/en/projects/binary-hero/print 14/38
11/22/24, 1:50 PM Binary hero

Add code so that the all the key sprites only play a note once when a key is held down.

when clicked

forever

if key v pressed? then

switch costume to on

broadcast note change

wait until not key v pressed?

else

switch costume to off

https://projects.raspberrypi.org/en/projects/binary-hero/print 15/38
11/22/24, 1:50 PM Binary hero

Step 5 Scrolling notes

You need to make notes scroll down the Stage so that the player knows which keys to press and when to press them.

https://projects.raspberrypi.org/en/projects/binary-hero/print 16/38
11/22/24, 1:50 PM Binary hero

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 17/38
11/22/24, 1:50 PM Binary hero

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 18/38
11/22/24, 1:50 PM Binary hero

Add the following numbers to your notes and times lists. Note: make sure to add these exact numbers in
the right order.

Here’s how songs are stored in your game:

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

So with the two new lists:


Note 1 (middle C) should be played at 5 seconds
Note 1 should be played again at 5.5 seconds
Note 3 should be played at 6 seconds
etc…

https://projects.raspberrypi.org/en/projects/binary-hero/print 19/38
11/22/24, 1:50 PM Binary hero

Click on the ‘note’ sprite and then click on show.

Then click on Costumes.

You should see that the ‘note’ sprite has 15 different costume, one for each different note from 1 to 15.

https://projects.raspberrypi.org/en/projects/binary-hero/print 20/38
11/22/24, 1:50 PM Binary hero

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

repeat until length of notes = 0

wait until timer > item 1 of times - 2

switch costume to item 1 of notes

create clone of myself

delete 1 of times

delete 1 of notes

https://projects.raspberrypi.org/en/projects/binary-hero/print 21/38
11/22/24, 1:50 PM Binary hero

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.

when I start as a clone

go to x: 20 y: 160

show

glide 2 secs to x: 20 y: -130

delete this clone

https://projects.raspberrypi.org/en/projects/binary-hero/print 22/38
11/22/24, 1:50 PM Binary hero

Step 6 Store your song

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 23/38
11/22/24, 1:50 PM Binary hero

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.

Give your new block a name and then click OK.

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 24/38
11/22/24, 1:50 PM Binary hero

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 25/38
11/22/24, 1:50 PM Binary hero

I need a hint
This is what your code should look like:

https://projects.raspberrypi.org/en/projects/binary-hero/print 26/38
11/22/24, 1:50 PM Binary hero

define load 'happy birthday'

delete all of notes

delete all of times

add 1 to notes

add 5 to times

add 1 to notes

add 5.5 to times

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

https://projects.raspberrypi.org/en/projects/binary-hero/print 27/38
11/22/24, 1:50 PM Binary hero

Test your new block by running it at the start of your project.

when clicked

load 'happy birthday'

hide

reset timer

Each of your lists should now contain six numbers.

https://projects.raspberrypi.org/en/projects/binary-hero/print 28/38
11/22/24, 1:50 PM Binary hero

Step 7 More custom blocks

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.

define clear song

delete all of notes

delete all of times

When you test your code, it should work just as it did before.

define load 'happy birthday'

clear song

add 1 to notes

add 5 to notes

https://projects.raspberrypi.org/en/projects/binary-hero/print 29/38
11/22/24, 1:50 PM Binary hero

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.

Making a block with parameters

Click on My Blocks, and then click Make a Block.

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 30/38
11/22/24, 1:50 PM Binary hero

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:

https://projects.raspberrypi.org/en/projects/binary-hero/print 31/38
11/22/24, 1:50 PM Binary hero

define Add note note at time secs

add note to notes

add time to times

define load 'happy birthday'

clear song

Add note 1 at 5 secs

Add note 1 at 5.5 secs

Add note 3 at 6 secs

Add note 1 at 7 secs

Add note 6 at 8 secs

Add note 5 at 9 secs

https://projects.raspberrypi.org/en/projects/binary-hero/print 32/38
11/22/24, 1:50 PM Binary hero

Step 8 Keep a score

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 33/38
11/22/24, 1:50 PM Binary hero

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:

when I start as a clone

go to x: 20 y: 160

show

glide 2 secs to x: 20 y: -130

if note = costume number then

change score by 1

delete this clone

https://projects.raspberrypi.org/en/projects/binary-hero/print 34/38
11/22/24, 1:50 PM Binary hero

Broadcast a message called ‘correct’ when the correct note is played.

when I start as a clone

go to x: 20 y: 160

show

glide 2 secs to x: 20 y: -130

if note = costume number then

change score by 1

broadcast correct

delete this clone

https://projects.raspberrypi.org/en/projects/binary-hero/print 35/38
11/22/24, 1:50 PM Binary hero

Add code to your Stage to briefly change the backdrop when the player plays the correct note. The project
already contains a second backdrop for this.

when clicked

switch backdrop to normal

when I receive correct

switch backdrop to correct

wait 0.3 seconds

switch backdrop to normal

https://projects.raspberrypi.org/en/projects/binary-hero/print 36/38
11/22/24, 1:50 PM Binary hero

Challenge!

Challenge: take it further


Your game is done now, but there are a few things you can do to make it even better if you want to!
For example, can you add code to change how the Stage looks if the correct note is not played?

when I start as a clone

go to x: 20 y: 160

show

glide 2 secs to x: 20 y: -130

if note = costume number then

change score by 1

broadcast correct

else

???

delete this clone

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.

https://projects.raspberrypi.org/en/projects/binary-hero/print 37/38
11/22/24, 1:50 PM Binary hero

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_medium=
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.

Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons license (htt


ps://creativecommons.org/licenses/by-sa/4.0/).
View project & license on GitHub (https://github.com/RaspberryPiLearning/binary-hero)

https://projects.raspberrypi.org/en/projects/binary-hero/print 38/38

You might also like