For background information and how to get started with Snap! (formerly known as BYOB), click here for lesson 1.
This post is a departure from previous lessons. I’m not getting into my usual level of detail, but will share the blocks that make the magic happen. You will also find a short video demonstrating the blocks in action at the bottom of this post.
This project is a windblown tree of varying levels and size. Each branch level produces smaller branches than the last and they all lean left.
We can produce a sparse tree. This one is 3 levels of 120 height.
Adding levels produces a denser tree. This one is 6 levels of 100 height.
And this one is 8 levels of 80 height.
I’m using blocks from these categories:
This lesson is similar to my last Snap! post, Snap! BYOB: Lesson 6: Building a Tree with Decreasing Branch Size Using Recursion (with Video). It uses the same blocks and works recursively, but instead of spreading the branches evenly, we’re simply cutting out a few blocks to produce a windblown effect.
I like working with trees because it’s a great way to practice recursion. As I mentioned in my previous lesson, recursion is solving a problem’s solution in term’s of itself. Basically, it’s when you have a function that calls itself until it reaches a base case (e.g. a number reaching 0 or 1, an edge being touched, etc.) then gives back all the answers (or moves) that were getting built up.
I used the online Snap! tool to screenshot this but it was originally built in an older version (BYOB). After importing my project, I gave my Sprite a classic look because I’m just attached to the little guy.
First, we’ll begin with a typical setup. I think of that as clearing the stage, getting to the right location, grabbing a pen, and anything else that needs to happen before building the “real” program.
Piece together the following blocks, grabbing from Motion and Pen:
The first block clears the stage. The next 2 blocks position us at the bottom of the stage and faces us upward. The last 3 blocks set the pen down and adjust pen color and pen mark size. It’s okay to omit the pen color and pen size blocks. They don’t add anything to the functionality of the project, but they do make things pretty.
For the tree, we need a custom block. This feature is found under Variables. Scroll to the bottom of the category and click Make a block.
Clicking OK produces a new block.
We can add variables at any plus sign location. Add the following before levels:
Add the following before size:
Note: You can name the custom block and variables anything you’d like. Name doesn’t matter as long as you’re following the recipe.
You should wind up with something like this:
When we’re working with recursive functions, we always need a base case. This is typically some variation of an if else statement.
Grab the if else block under Control.
Let’s build a condition for the if. Begin with the equals block under Operators.
Drag and drop a copy of the levels variable we made, insert it into the equals block, and enter 1 into the right hand side of that block. Grab 2 move _ steps blocks under Motion.
Drag and drop a copy of the size variable we made and insert it into the first move_steps block. Grab the negation block under Operators, insert it into the second move_steps block, then drag and drop another size variable into it.
This is our base condition for the recursive function. Its purpose is to bring the function to a close. Without it, we would create an infinite loop or build something that just doesn’t work.
Piece together the remaining blocks.
Notice we call on the wind blown tree block inside the wind blown tree block. That’s recursion. To demonstrate how this works, I’m adding wait blocks between moves. This allows us to see every movement of the tree being built.
The following video steps through the tree using wait blocks. This helps us see how the recursion is working. Then, wait blocks are removed and the tree is built at normal speed.