Once you've learned how to program in one language (at least any language that your AP Computer Science Principles class would have used), it's much easier to learn another language. And Snap! will be especially easy to pick up quickly because it's all visual; you just snap blocks together, and you don't have to worry about any syntax (parentheses, commas, semicolons, quotation marks, etc.).
This page is designed to get you up and running in Snap! as quickly as possible. It will help you pick up Snap! quickly if you take the time to read (or at least skim) this whole page. Click the links below to learn about each topic.
You may want to create a Snap! account so you can save and access all your Snap! projects in the cloud. (You don't need an account; you could save all your Snap! projects on your own computer and drag them onto a Snap! window to load them.) Instructions for creating an account and logging in to and out of your Snap! account are on the first page of the Beauty and Joy of Computing CSP curriculum:
To save a project, choose "Save" from the Snap! File menu, (or press control-s).
To load a project, choose "Open..." from the Snap! File menu (or press control-o), select the project you wish to open, and click "Open." Always remember to save the project you were working on before opening a new one.
There are two primary ways to share your Snap! projects: sending an XML file containing a copy of your project; and creating a link to your active project file.
In Snap!, you drag and attach blocks together to create your scripts. You can learn more about a particular block by right-clicking it (or control-clicking it on a Mac) and selecting "help..." which will bring up a little window with information about how the block works. You can also just click a block to see what it does. Click for an example of accessing the Help menu for a pen block.
Every script you build in Snap! belongs either to a sprite or to the stage. Identifying words in block names such as "I" or "my" refer to that sprite or to the stage (depending on which one is selected in the sprite corral):
Some blocks have little arrows that allow to you to control the inputs:
itemblock above, you can either choose from the menu or type your own value into the box. If the box is block-colored, you can only use the choices in the menu.
There are several different block shapes in Snap!, and they each have their own meaning and places they can be used:
false. They are used inside other blocks to help a program decide when to do something or what to report. Here are a few predicate procedures:
false) as input:
Of course, you can use global variables (more about them below) to share information between sprites, but you can also send messages between your sprites or have one sprite tell another sprite what to do:
broadcastblock sends a message to all the sprites. To set the message, click the down arrow and select "new..."
when I receivehat block with that same message will run:
Telling sprites what to do: You can tell a specific sprite (or the stage) what to do by using the
tellblock to send instructions (not just messages):
Asking sprites questions: You can ask a specific sprite (or the stage) a question using the
askblock to send an expression:
There are several kinds of variables in Snap!. Here are the most important:
draw squareblock shown at right. There is more information about creating blocks with input parameters below.
script variablesblock to change the name; you can click the right arrow (▶) to create additional variables at the same point in your program. To use a script variable, drag it from the
script variablesblock to wherever you want to use it (in the same script).
foruses i (unless you change the name) to hold the index number of the iteration, and
for eachuses item (unless you change the name) to hold each list item as it traverses (travels through) the list:
Because Snap! is a visual language (not a text-based language), you can use multi-word variable names. It will be clear to you and others when reading the code that those separate words are all part of the same variable name:
To program your own block:
reportblock; whatever input you give to
reportwill be the value reported (returned) by your new block.
There are several kinds of input slots that tell the programmer what kind of data is expected:
You can chose the input type for the input variables in your custom blocks by clicking the right arrow (▸) to the right of the "Input name" option in the "Create input name" window (as shown below). It's not necessary to set an input type in Snap!. If you don't select one, your input slot will accept any type of input.
filterin some other languages)
reducein some other languages)
lambdain other languages. Instead of requiring a parameter name (the
(x)in many function definitions), Snap! just uses an empty input slot to indicate where the argument should be plugged in:
You can read about several other commonly used Snap! list blocks on the Snap! Cheat Sheet page:
Some other common list functions aren't built into Snap!, but are available in a library: Select "Libraries..." from the Snap! File menu, and choose "List utilities" near the top of the list.
Python users can use Snap! lists to recreate the behavior of other data aggregates:
tupleis just a list that you don't modify.
setis unordered; so just use a list and don't make any assumptions about order. Also, Python sets aren't supposed to include the same value more than once. So when you extend a set, use
remove duplicatesfrom the Snap! "List Utilities" library.
dictionaryis just a list of two-item lists. In each small list, the first item is the key and the second item is the value. In Snap!, this might look like:
assocfrom the "List Utilities" library to search for a key-value pair and then use
item (2) ofto select the value.
Snap! has libraries (collections of additional procedures) for many things, including lists, accessing data on the Internet, external devices, pen colors, audio, and maps. Click for an example of opening the "Words, sentences" library in Snap!
You can export one or more blocks into an XML file on your computer, which you can then import into another project or share with another programmer for them to use.
If you ever see this
Obsolete! block (shown right) in code you have imported, it means that a block used by the block you imported was not included in the export. You'll have to go back to the original project and export again being sure to select all of the blocks needed by the blocks you want.
Here's how to call a procedure from inside itself in Snap!:
Click for an example of a recursive command that draws one triangle, and at each corner draws a smaller triangle, and at each corner of those draws another smaller triangle and so on until it reaches the last requested level.
draw triangle fractal with (4) levelsblock shown above:
Recursion also lets you traverse a list, just as
for each does, but without the use of index variables. Instead, you split the list into its first item and everything else:
You compute some function of the first item, and combine that with a call to the same procedure you're writing, but on the smaller list that remains:
"Visible Stepping" allows you to control how quickly Snap! steps through the blocks of your code.
Visible stepping can be useful as you read someone else's code because you can watch it run at human speed instead of computer speed. It's also useful in debugging your own code when something goes wrong.
You can also use to place breaks in your code. When your program reaches this block, every script that is running will pause until you click the yellow Run button ().
Click for an example of using
There are several ways to see the value of variables and sprite attributes while your code is running:
You can also use the block to get information about the state of your program while it's running. Click for an example of using
say for to debug a rectangle drawing script.
One way to avoid having bugs in your program in the first place is to keep your code organized:
You can add comments to your Snap! code by right-clicking (or control-clicking) in the Scripts area and choosing "add comment." Click for an animation that shows how.
You can create a help message for your block by attaching a comment to the hat block in the Block Editor. Then people using your block can find out what your block does without having to look inside. These work just like the help messages for built-in blocks.
You can learn more about programming with Snap! on the Snap! Cheat Sheet, by reading some of the very thorough Snap! Reference Manual, or by connecting with other Snap! programmers on the Snap! community website.
There is also a "find blocks button" in the palette in case you are thinking of a block you can't find: