Scratch for Kids

A Watch the course on Frontend Masters

Using The Pen And Custom Blocks That Take Inputs

Letā€™s start by creating a reusable block that we can pass some inputsā€”just like a lot of the standard ones that weā€™ve seen so far.

define draw polygon with (sides) sides and (length) length
repeat (sides)
	move (length) steps
	turn ccw ([360] / (sides)) degrees
end

Iā€™m also going to quickly create a block for setting everything up.

define setup
set [sides v] to [3]
set pen size to [4]
set pen color to [#F00]
pen up
erase all

And now I can create a loop to generate everything from a triangle to a decagon.

when green flag clicked
setup::custom
repeat [7]
	go to x: [-50] y: [-150]
	pen down
	draw polygon with (sides) and [100] length::custom
	pen up
	change pen (color v) by [10]
	change [sides v] by [1]
end

And now we can programmatically draw some pretty cool shapes.

Your turn: Can you create a sketch that draws a series of squares?

The block should look something like this:

define draw (number) squares

You can see a solution here if you want.

Further Exploration

If you notice that your algorithm begins to break after a certain number of sides, take a moment to think about why. If you squint at this example, youā€™ll likely get a hint.