Snap! BYOB: Lesson 7: Building a Wind Blown Tree with Decreasing Branch Size Using Recursion (With Video)

For background information and how to get started with Snap! (formerly known as BYOB), click here for lesson 1.

Snap! homepage

BYOB download

Click here for tutorials 1-6

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.

Screenshot (58)

Adding levels produces a denser tree. This one is 6 levels of 100 height.

Screenshot (59)

And this one is 8 levels of 80 height.

Screenshot (60)
I’m only decreasing height to keep things fitting the screen

I’m using blocks from these categories:

  • Motion
  • Pen
  • Control
  • Operators
  • Variables

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.

Wiki page on recursion

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.

Screenshot (33)

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:

Screenshot (62)

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.

Screenshot (44)

Clicking OK produces a new block.

Screenshot (45)

We can add variables at any plus sign location. Add the following before levels:

Screenshot (46)

Add the following before size:

Screenshot (47)

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:

Screenshot (48)

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.

Screenshot (49)

Let’s build a condition for the if. Begin with the equals block under Operators.

Screenshot (51)

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.

Screenshot (52)

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.

Screenshot (53)

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.

Windblowntree script pic (1).png

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.

Screenshot (54)

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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s