Designing Custom Blocks

From Observation Notes Analysis:
Work with a Partner

So far, you've used blocks that already existed. Now, you'll learn to build your own blocks and use them to make other blocks.

repeat-4(move-50-turn-clockwise-90) You'll start by defining a draw square block with a familiar script (shown right). Then, you'll define draw triangle, then draw house, and finally draw row of houses.
Row of 4 Houses

Creating a block gives a name to a script that can be called inside another block. This is a form of abstraction, tucking away the details of your code to make the overall structure clearer and easier to debug. Soon, you'll learn how to give your script an input, just as move 50 steps takes an input to tell the sprite how far to move.
  1. "U1L3-Polygons"Create a new project called U1L3-Polygons
  2. To get the Block Editor, you can also click Make a block near the bottom of the Variables palette.
    Build the draw square block: right-click (or, on a Mac, control-click) on the gray background of the scripting area and choose "make a block..."
    • The video below shows the first few steps. Finish it yourself, and press OK or Apply.
    • More details about defining blocks are in the big yellow section below.
    make a block
  3. Find your block at the bottom of the palette you chose when you defined it. If you created the block as a Motion block, you will find it in the Motion palette. (If you didn't choose a palette, it will be in with Variables.)
    Test your draw square block, using it the same way you've used other blocks. Fix your code if necessary.
    pen down, draw square
    Congratulations! You have just created your first custom block.
Every block needs a place to "live" (a palette, shown by the color), a title, a category (shown by the shape of the block), and a script that defines its behavior. Pair Programming Swap

Practice building blocks.

    House
    In 1.3.1.4 we show a video of someone drawing the turning at the corner of an equilateral triangle. But we gave away the story in 1.2.4. --AC
  1. Define a draw triangle block that draws an equilateral triangle with the same side length as the square you just drew (50). (Don't click this sentence unless you've been working on this for ten solid minutes and need a hint.)
  2. Create a draw house block that uses your draw square and draw triangle blocks together to draw a "house" shape: a square with a triangular shaped roof sitting on top. (You might have to do some debugging to get the two shapes to connect properly.)
  3. Build a draw row of houses block that draws a row of attached houses, each new one to the right of the previous, until the sprite moves too close to the right edge of the screen. For that, you will need repeat until.
    repeat-until-(x-position-less-than-210) Row of 4 Houses
  4. Now Is a Good Time to Save