Tidy Blocks
A long program is hard to read and harder to fix. Break it up. One block per part, each with a name that says what it draws, and the bottom of your program becomes a summary anyone can follow.
π‘ The idea
Give every part of a drawing its own named block, and the program reads like a list of parts.
- Write all your to ... end blocks first, then call them in order at the bottom.
- Each block should do one job, and its name should say what that job is.
- Test each block on its own before joining them up.
- A block can set its own colour and width, so it does not depend on what came before.
- If a block is getting long, that is a hint that it is really two blocks.
π Words to know
- procedure
- another word for a named block of code
- structure
- how a program is arranged into parts
- test
- run one part on its own to check it
π Watch this
A tree: a fat brown trunk block and a green leaves block, joined by the last two lines.
to trunk colour brown width 10 forward 90 end to leaves colour green circle 45 end trunk leaves
βοΈ Your turn
- Run it, then delete the last line. Only the trunk is left.
- Swap the last two lines around and explain what changes.
- Change circle 45 to circle 70 for a bigger crown.
- Add fill on inside leaves to make the crown solid, and see that a block controls its own pen.
- Write a third block called grass and call it first.
π― Challenge: Add a block called fruit that draws a red dot, then put two fruits on the tree. Give the trunk its brown colour back as well.
Show me one answer
to trunk forward 90 end to fruit colour red dot 16 end trunk fruit goto 40 90 fruit
Your program