- Learnt28 min · a grid of clones, a list, a playhead
- Basic24 min · the beat sequencer
- Challenge 1–327 min · tempo keys, clear and random, saved beats
- Extrabonus · a bass row
Review — a song in two lists 5 min
- Two lists in step keep a note and its length on the same row.
- One loop with
iplays every row. - Two sprites reading the same lists each need their own i.
Quick-fire
A list called pattern holds 1 0 0 1. The script runs replace item (2) of [pattern v] with ((1) - (item (2) of [pattern v])). What does the list hold now?
Reveal the answer
1 1 0 1. One minus 0 is 1, and one minus 1 is 0, so this block flips a slot. It is how today’s pads switch on and off.
Today’s Topic 3 min
- A grid of 32 pads, all clones of one sprite
- A list of 32 slots —
1for on,0for off - A playhead that broadcasts each step
play drum (item (row) of [drums v]) for (0.25) beats
Learning outcome
By the end of this lesson you will be able to:
- Store a grid in one list and find any square in it.
- Make clones that each know their own row and step.
- Build a beat machine that plays whatever pattern you click.
Learnt — a beat machine 28 min
1 · A grid is a list
The machine has 4 rows, one per drum, and 8 steps from left to right. That is 32 pads. We keep them in one list of 32, row after row.
To find the slot for any pad:
((((row) - (1)) * (8)) + (step))
A second list, drums, says which drum each row plays: 6 4 1 2 — hi-hat, cymbal, snare, kick.
2 · 32 pads from one sprite
One hidden Pad sprite makes all 32. Each clone remembers its own row and step, so those variables are for this sprite only. A clone keeps the values the original had when the clone was made.
define build grid
set [row v] to (1)
repeat (4)
set [step v] to (1)
repeat (8)
create clone of [myself v]
change [step v] by (1)
end
change [row v] by (1)
end
when I start as a clone
set [is clone v] to (1)
go to x: (((step) * (44)) - (198)) y: ((100) - ((row) * (44)))
if <(item ((((row) - (1)) * (8)) + (step)) of [pattern v]) = (1)> then
switch costume to [on v]
else
switch costume to [off v]
end
show
How to use this: Click the green flag. Nothing else to press — 32 pads appear, lit the way the pattern list says.
3 · Click to switch a pad
A clone can be clicked like any sprite. The click flips its slot in the list.
when this sprite clicked
if <(is clone) = (1)> then
replace item ((((row) - (1)) * (8)) + (step)) of [pattern v] with ((1) - (item ((((row) - (1)) * (8)) + (step)) of [pattern v]))
if <(item ((((row) - (1)) * (8)) + (step)) of [pattern v]) = (1)> then
switch costume to [on v]
else
switch costume to [off v]
end
end
How to use this: Click the green flag, then click any pad to switch it on or off.
4 · The playhead
The Playhead sprite walks across the 8 steps. At each step it broadcasts tick and rests for half a beat.
when flag clicked
set [bpm v] to (120)
set tempo to (bpm)
forever
set [current v] to (1)
repeat (8)
set x to (((current) * (44)) - (198))
broadcast [tick v]
rest for (0.5) beats
change [current v] by (1)
end
end
when I receive [tick v]
if <<(is clone) = (1)> and <(step) = (current)>> then
if <(item ((((row) - (1)) * (8)) + (step)) of [pattern v]) = (1)> then
switch costume to [hit v]
play drum (item (row) of [drums v]) for (0.25) beats
switch costume to [on v]
end
end
tick, but only the ones in the current column, and switched on, play.Look at the drum block. Instead of picking a drum from the dropdown, we dropped a round (item (row) of [drums v]) block into it. Row 1 plays drum 6, row 4 plays drum 2.
Basic — the beat sequencer 24 min
Build the whole machine from the Learnt, then play with it.
Step 1 — the lists and variables
- Lists for all sprites:
patternanddrums. Type6 4 1 2into drums. - Fill pattern with 32 zeros using the little script below. Click it once — you do not need it after that.
- Variables for all sprites:
bpm,currentandpads. - Hide the two list boxes. Keep bpm showing.
delete all of [pattern v]
repeat (32)
add (0) to [pattern v]
end
Step 2 — the Pad sprite
- Paint a Pad sprite with three costumes:
off(dark),on(bright) andhit(yellow). A rounded square about 36 pixels wide. - Give it three variables for this sprite only:
row,stepandis clone. - Under the green flag:
hide,set [is clone v] to (0), thenbuild grid :: custom. - Add the build grid block, the clone script, the click script and the tick script.
Step 3 — the Playhead
- Paint a tall, see-through yellow bar as the Playhead.
- Give it the forever loop from the Learnt.
Step 4 — label the rows
- Add small Drum-highhat, Drum-cymbal, Drum-snare and Drum sprites down the left, one beside each row.
- Click pads to make a beat. Try a kick on steps 1 and 5, a snare on 3 and 7.
How to use this: Click the green flag. The rock beat starts playing — click any pad to switch it on or off while it plays.
Every pad appears in the same place.
row and step are for all sprites. Delete them and make them again, choosing For this sprite only.
A pad lights up, but the wrong pad’s slot changes.
The slot sum is different in two scripts. Copy the same sum into the clone, click and tick scripts.
Each drum plays twice.
The hidden original is playing too. Check the tick script starts with <(is clone) = (1)>.
The playhead is in the wrong column for the sound.
The playhead’s x sum must match the one the clones used for their x. Both are step × 44 − 198.
A tempo knob
Let the player speed the machine up and slow it down.
- ↑ adds 10 to bpm and ↓ takes 10 away, and the tempo follows.
- bpm never goes below 60 or above 200.
- The beat never stops while you change it.
How to use this: Click the green flag, then click the stage once and press ↑ or ↓ to change the speed. You can still click pads.
Teacher reveal
when [up arrow v] key pressed
if <(bpm) < (200)> then
change [bpm v] by (10)
set tempo to (bpm)
end
The same as the Lesson 9 Extra. The playhead needs no change at all, because it rests in beats.
Clear and random
Two quick tools for a beat maker.
- Make a variable
nto count through the 32 slots. - C switches every pad off.
- R switches each pad on or off at random.
- The pads change colour straight away, and the green flag starts on a random beat.
How to use this: Click the green flag, then click the stage once. Press C to clear the grid or R for a random beat. You can still click pads.
Teacher reveal
when [r v] key pressed
set [n v] to (1)
repeat (32)
replace item (n) of [pattern v] with (pick random (0) to (1))
change [n v] by (1)
end
broadcast [redraw v]
when I receive [redraw v]
if <(is clone) = (1)> then
if <(item ((((row) - (1)) * (8)) + (step)) of [pattern v]) = (1)> then
switch costume to [on v]
else
switch costume to [off v]
end
end
The Stage changes the list; the pads only find out through redraw. Forgetting that broadcast is the common bug: the sound changes but the pads do not.
Saved beats
Keep two beats ready, and switch between them with one key.
- Two more lists,
rockanddance, each holding 32 slots. - 1 copies
rockintopattern; 2 copiesdance. - The pads change as soon as the beat loads, and it keeps playing.
How to use this: Click the green flag, then click the stage once and press 1 for the rock beat or 2 for the dance beat.
Teacher reveal
when [2 v] key pressed
set [n v] to (1)
repeat (32)
replace item (n) of [pattern v] with (item (n) of [dance v])
change [n v] by (1)
end
broadcast [redraw v]
Copying slot by slot, not linking the lists, is why clicks do not spoil the saved beat. A pupil who wants to save their own beat can copy pattern into a third list the same way.
A bass row
Join Lesson 11’s notes to today’s grid: a fifth row that plays a tune, not a drum.
- 8 more pads along the floor, made by a second sprite, Bass Pad.
- A list
basssays which of them are on. A listbass notessays which note each step plays. - A switched-on bass pad plays its note on the Bass instrument when the playhead reaches it.
How to use this: Click the green flag. The beat and the bass line start playing — click any pad, including the bass pads on the floor, to change them.
Teacher reveal
when I receive [tick v]
if <<(is clone) = (1)> and <(step) = (current)>> then
if <(item (step) of [bass v]) = (1)> then
switch costume to [hit v]
play note (item (step) of [bass notes v]) for (0.5) beats
switch costume to [on v]
end
end
The Bass Pad sets its instrument to (6) Bass under the green flag, before it makes its 8 clones. The notes 36, 43 and 41 are a low C, G and F.
Summary 5 min
- A grid can live in one list: slot = (row − 1) × 8 + step.
- Clones keep their own row and step when they are for this sprite only.
- A playhead broadcasts each step; the pads decide for themselves whether to play.
- A round block can go into a dropdown:
play drum (item (row) of [drums v]) for (0.25) beats. - The Music Maker arc is finished: drums, notes, lists and a beat machine.
- Sequencer
- A machine that plays a pattern of steps, over and over.
- Step
- One column of the grid — half a beat of time.
- Playhead
- The marker that shows which step is playing now.
- Slot
- One item of the pattern list, holding 1 for on or 0 for off.
Ship it
- Give the project a title and add instructions in the project notes: click pads, ↑ ↓ for speed.
- Save it, close it for ten minutes, then play it cold, reading only your own notes.
- Share it on scratch.mit.edu, so you have a link to your beat machine.