One of the biggest limits we came across teaching with Scratch this year is the inability to write methods and use parameters. Methods, returns, and parameters are probably the only major concept in our intro CompSci class that we can’t teach in Scratch; so we introduced them in Alice this past year. It worked ok, but we haven’t been able to come up with any really good labs that explain the purpose of parameters well to the students.
This past week I came across an extended version of Scratch called Scratch BYOB, or Build Your Own Block. And it does exactly what I was looking for. It gives you the ability to create methods and optionally include parameters.
I’ve updated this post to do the same using the 3.1 version of BYOB which is current as of Oct ’11. You’re probably better off with that post instead unless you’re using an earlier version of BYOB.
I’m assuming you’ve at least taken a look at Scratch. If you haven’t, head on over to the Scratch website. There’s a ton of good stuff there.
A Quick intro
For my first attempt with this I’m going to do the classic Sierpinski triangle. Seems that’s pretty much the standard recursion problem so it’s a good place to start.
Creating the method
The first thing to do is click on the variables section. You’ll notice an additional option titled “Make a block.”
Clicking on the Make a block option brings up a dialog box where you specify both the name and any parameters. This is where I found the only negative that I’ve come across so far with BYOB. If you have a long block specification it goes off the right edge of the text box and does not scroll. You’ll notice in the image below that I used several parameters and ran out of room. In fact there are 7, although you can only see 6.
A Reporter and Boolean are both return methods. A Reporter returns a string or number and the boolean returns, well, a boolean.
Our first method
The first method to create is one that draws a line. Sure, we could put all this in one method but that sort of defeats the purpose of using a method, right?
So with a little dragging from the block list into our method we have the following drawLine method which will get used to draw triangles in our next method.
Nothing too complicated here. Pen is picked up, the sprite moves to the first point, the pen is dropped, sprite moves to the second point, and the pen is picked up just in case. The wait was put there originally so you could see what’s going on. But I found that if the wait wasn’t there Scratch would appear to freeze while the triangles draw and then the entire design would show up. With a very short wait you can see each line drawn.
Our triangle drawing method needs 7 parameters. 6 to hold the 3 x,y pairs we need and 1 to keep track of the recursion level. Were we doing this in another language we could pass in the points as a list or maybe even make a class to hold the points. But passing in 6 numbers works well enough to teach the concept.
After a little more dragging and dropping we wind up with the following. Thanks to KodeClutz and Google for the algorithm.
A little breakdown… The first 3 lines draw the outer triangle going from (x1,y1) to (x2,y2), (x2,y2) to (x3,y3) and back from (x3,y3) to (x1,y1). The next 6 set local variables to the x and y for the 3 midpoints. And the last few lines recursively call our method to draw the smaller triangles if count is greater than 0.
This also brings up the idea of local variables and will be a good place to introduce the concept of scope, something else that we have struggled to find a good way to get clicking in the students.
Starting it up
We need to call our drawTri method once to get the ball rolling. I also wanted to pick up the pen, clear the screen, and hide the sprite.
And a few samples…
Below are sample drawings with count set to 3, 5, and 7 so you can see how they look. Much past 7 didn’t really look all that much different. It just took longer to draw.
Does it work?
Honestly, don’t know yet. We’re done with Scratch for the year so we won’t know until next year. Even if we don’t get to recursion in Scratch though, the ability to introduce methods in this environment should help our kids pick up the concept earlier in the year and without as much difficulty.