Snap! is a visual, drag-and-drop programming language. It is an extended reimplementation of Scratch that allows you to Build Your Own Blocks. It also features first class lists, first class procedures, and continuations. These added capabilities make it suitable for a serious introduction to computer science for high school or college students.
Snap! is the latest version of the program I’ll be referencing. It was formerly called Build Your Own Blocks (BYOB). Snap! has more options so you want to begin there if you’re interested in building more complicated programs. If you’re a casual user or using the program for kids, BYOB is a better option. I’m using BYOB 3.1.1 because a) it’s something I’m very familiar with and b) I have several programs built with that version.
Snap! and BYOB are similar enough that tutorials can apply to both.
BYOB is a program we used in my Introduction to Computer Science course. It’s perfect for those with no programming or computer science experience. It’s ease of use also makes it suitable for kids and adults alike. It’s not the best choice if you want to learn computer science quickly, but if you’re willing to learn the slow (and thorough) way–it’s a good starting point.
The great thing about BYOB is that it teaches you the kind of logic you need for building algorithms and/or more advanced programming languages. It also translates nicely to pseudocode.
- By logic, I mean the ability to think through an algorithm, deciding which blocks make the program work efficiently and seamlessly.
- Algorithms are a step-by-step recipe. To read more about algorithms, click here.
- Pseudocode is an informal description of the operating principle of a program or algorithm. To read more about pseudocode, click here.
Snap! and BYOB are 100% free and found on the Berkeley website. Snap! runs on browsers and BYOB can be downloaded to your desktop. I use the desktop version.
Getting to Know BYOB
You only need to know a few things before getting started. Blocks are on the left–click around and see what’s available–and the sprite–the yellow guy–is our performer. Don’t worry about what the individual blocks do at this point. It’s best to learn them as you go.
When a block is needed, click and drag it into the big gray area. When it’s not needed, simply drag it off that area. Blocks snap together; notice their shapes because some blocks can’t be snapped together. When you’re ready to run a block, click it once. A block that is running is highlighted around the edges.
The sprite follows our exact directions. If we tell him to make 5000 circles, he will do just that. Right click the sprite for resizing (he starts out way too big). If he ever winds up facing a funny direction, there are controls on top to fix that.
Everything else you learn by using the program. I’m starting very simple today, but you can make cool animations and fun games with BYOB. But you’ll never get there if you can’t build a square.
Building a Square
Starting with a basic shape allows us to learn how BYOB works and gets our logical minds going. This takes a few steps so don’t make the blocks “go” until we’re ready. For this exercise, I’m using blocks from the following categories:
- Motion (blue)
- Pen (green)
- Control (brown)
(Here’s the logic) What is a square? It’s a shape with 4 sides of equal lengths with 90 degree angles. If our sprite is going to draw a square, he needs to walk x distance and turn 90 degrees. Grab the move _ steps and turn _ degrees blocks under Motion. Give the sprite a number of steps and degree measure by clicking inside the white boxes. Be careful not to click the block (blue part) because it makes the program run.
I’m using 100 because the sprite’s steps are small (this number can vary). The degree must be 90. We need this movement to happen 4 times to make 4 sides. Right click to duplicate our connected blocks, not the individual blocks. Repeat until you have the movement 4 times.
Connecting them together, we wind up with this:
Unfortunately, our sprite isn’t a magical little guy leaving lines wherever he goes. If we ran the blocks above, the sprite would leave no evidence of movement. We need to equip him with a pen. Grab the clear and pen down blocks under Pen. The clear block gives us a clean slate before running the blocks.
Clicking the block set results in the sprite drawing a square.
You might notice the sprite moves at incredible speeds–good for algorithms–not so good when we want to see the step-by-step movements. Grab the wait _ secs block under Control. Insert a wait 1 sec block between each turn.
Now the sprite will walk 100 steps, turn, wait 1 second, and repeat until the square is drawn.
The wait block doesn’t add anything to the program but it allows us to see steps instead of one instant movement. I use it when building complicated programs to see exactly what’s happening along the way. It’s especially helpful when you’re running into an issue.
Let’s take one more look at our square program.
A good rule of thumb in programming is when you see things being repeated, you should find a cleaner way to convey that information. Grab the repeat _ block under Control. Instead of having this long string of code that says do x, do y, do x, do y, do x, do y–you could say, do x then y a certain amount of times.
View this block-by-block. What is happening here? First: The screen is cleared. Second: Pen goes down. Third: The blocks inside the repeat continue 4 times.
- What happens if clear and pen down are placed inside the repeat?
- What happens if the number of repeats is changed?
- What happens if the sprite takes 200 steps instead of 100?
- What happens if the angle is changed?
Hint: Only one wouldn’t break the program.
These questions are what I mean by thinking logically. Once you work on trickier programs, you should ask yourself these types of questions. Something isn’t working? Why is that? Well, step 1 is doing x. Step 2 is doing y. Repeat is doing c. What happens if step 2 moves here, or there? Building algorithms requires taking a step back and asking yourself what makes sense or why something isn’t behaving like you want.
You might be thinking, “So I built a square. What’s the big deal?”
It’s a big deal because this logic can be applied to every shape. You can now build triangles, stars, diamonds, octagons, all the -gons.
By the way, an allthegons sounds like an awesome shape.
You can even start playing around with different versions of shapes. Below is a hex spiral.
These are the blocks I used for it:
This is a bit more advanced than the square but something you could easily learn once you get the hang of shapes. I won’t get into the specifics of the hex spiral in this post, but I wanted to share this example to show how something as simple as building shapes can translate into fancier designs.