Making a Square Function

Exercise: Make a Draw Square Block

Today, we’re going to make our own blocks! We’re going to teach the computer how to draw a square so that we can have a block “draw square”. Please follow the steps below.

Click on “make a block” at the bottom of the “more blocks” tab.

 

More Blocks

This will open up the “make a block” dialog box, as shown below.

Dialog Box

Today, we’re going to make our own blocks! We’re going to teach the

Type in the name into the purple block in the dialog box. In this case, since we’re drawing a square, you should type “draw square”. Click “OK” to save this block.

dialog box

Now a block to define this new block will pop up in the scripts area. Also, this block will show up in the “More Blocks” tab.

define draw square

More Blocks tab 2

We’re going to define the block now! Build the program as shown below.

Define

Now it will Use your block with the pen down block to draw the square

Pen Down

Now we’re going to go back and edit the block. We can right click (or shift and click) on the new block and select “edit” to go back to the block editor.

Edit

This time, click the small triangle next to “Options” to show more tools. Clicking on these allows you to add more text or arguments. Hover above the arguments and click the “x” to delete them.

Dialog box

\We want to add the input “size” to our function. It will be a number, so will want to click the first option, “Add number input”.

add number

Type in “size” into the box that pops up to name this input.

add size

Now we have a variable inside of our block definition.

define with size

Drag the variable size down into the move block. Whenever we need a new copy of the variable we just grab it from the variable in the top row.

define draw square

When we click OK we’ll see that our “draw square” block now takes an argument. We can put different numbers in the blank to be able to draw squares of different sizes!

pen down

 

Make a Draw-a-Pentagon Block

Exercise: Make a Draw Pentagon Block

Make a block that draws a pentagon. It should take an input “size” just like the square block.

X
X