Trying it out - Getting Edgy with Polygons

Objective

After completing this lesson, you will be able to apply the concepts of the previous lesson - Getting Edgy with Polygons.

Exercise

Your Turn!

Now it’s your turn. Download this document for a recap of the blocks we covered in this lesson and some hands-on exercises for you to explore.

What You Have Learned in This Lesson

Programming with the Pen category

In this lesson, you have learned how to draw the movement of your sprite on the stage using the blocks in the "Pen" category. If you put the pen down, all the sprite’s motions are drawn onto the stage until you put thepen up again. You can delete everything that is drawn on the stage with the clear block. You can set and change the size of the pen.Additionally, you can choose a pen color with different blocks.

Snap!’s pen color is based on the HSB color model. HSB is often used in graphic design programs because its way to generate colors is closer to how we perceive colors and thus it is easier for us to find the values for a specific color.

In the HSB-color model, S and B represent saturation (2) and brightness (3) of a color and they are usually on a scale between 0 and 100.

H stands for hue (1) and is usually set with the angle in a color circle (0-360°).

In Snap!, the hue is instead represented by a color band from 0-99 to have the same scale for all color components.

Combining all three color dimensions lets you generate many different colors.

In most graphic design programs, you can select a specific hue by picking the corresponding angle in a color circle. Angles in a color circle work like angles in geometry (0-360°). However, the hue in Snap! is defined as a color band with values from 0 to 100, meaning a change of 3.6° in the color circle is equivalent to changing the hue by 1 in Snap!

The saturation regulates how „intense“ your pen color is. Setting the saturation to 0 will lead to white (or grey, depending on the brightness of the color), 100 is the maximum color intensity.
A change in the brightness value effects the amplitude of your color, meaning the maximum deflection of light waves. A smaller amplitude (lower brightness) leads to darker colors, a greater amplitude makes brighter colors.

The three color dimensions are also partly represented in the color picker of the set pen color to block. As mentioned, in Snap! the hue is not based on the angle in a color circle but rather on the position in the color band (0-99). The color band is repeating continuously, meaning 0, 100 or 200 is the same shade of red.

Representation of the three color components in the set pen color block. Hue (h) is shown as a horizontal color band along the x-axis of the color picker with 0 on the left and 100 on the right side. Saturation and brightness are represented along the y-axis. In the upper half, saturation (s) varies from 0 at the top to 100 in the middle while brightness remains at 100. The lower half displays colors with varying brightness levels, where brightness is 100 at the center and decreases to 0 at the bottom. In this lower section, saturation is set to 100.

In summary, this is how you can use the three options to set the pen color:

tbd

Writing custom blocks in Snap!

In this lesson you also learned how to write your own custom blocks in Snap! with the "Make a block" feature.

This is useful if you want to use parts of your code more than once in your project or even in several projects. Additionally, you can make your code more readable by hiding away the technical details (abstraction). You can also make multi-purpose blocks by generalizing concepts – in this case we generalized the algorithm for a polygon, and were able to create a block that can draw polygons with any number of sides (generalization).

In Snap!, there are three ways to open the block editor to create custom blocks. You can either click the plus sign at the top of the palette in each category or the "Make a block" button at the bottom, or you can right-click the scripting area and select "make a block…" from the context menu.

In the now opened dialog box you first need to specify your block’s features such as category (1), label (2), block type (3), and availability (4) for your block. Click "OK" when you filled out everything in order to access the block editor.

The dialog box lets you pick a category (1), label (that is the text that will be displayed on the block, 2), block type (3), and availability (this is called the scope of a block – which sprites are able to access the block, 4) of a block.

The block editor shows you the so-called block prototype of your block. It’s a hat block with a version of the future block on it.

Attach the script that is supposed to be executed whenever that block runs to the prototype. That script is called the block definition.

Press "OK" to save your block and close the block editor or "Apply" to just save your block and keep the block editor open.

All custom blocks are located at the end of their category.

Block header and block definition of the square block in the block editor (left). Custom square block at the end of the pen category (right).

Like any other block, custom blocks can also have parameters. To add an input slot to a block, click on the + sign on the block prototype where you want the input to be. This opens a dialog box, in which you can specify whether you want to create an input (default selection) or expand the title text. Enter the name of the parameter or label and press "OK" and "Apply" to generate the input.

Your block now has an input slot on the outside and an orange reporter with the name of the input on it in the block header inside the block editor.

This orange reporter stores the value of the input and can be dragged out of the block header to be used inside the block definition.

Clicking "OK" and "Apply" will add an input slot to your block, which is represented by an orange input reporter inside the block editor. Any value you add as input to the block is stored in the orange reporter and can dragged out of the block header to be used inside the block definition.

Left: Clicking on the plus sign behind the word “square” opens a dialog box. There you can choose to extend the block title or add an input slot (default selection) and specify an input name – in this case “size”. Clicking OK” adds the input slot to the block. Right: In the palette the block gets an empty input slot, in the block editor, an orange reporter with the input name “size”. This reporter stores the input value and can be dragged from the block header to the desired position in the block definition where the input value is needed.

You can edit custom blocks by right clicking the block and selecting "edit…" from the context menu.

One of the main reasons of writing your own blocks is generalization and abstraction - hiding what’s really going on behind a more convenient interface. If you want to know more about abstraction and where you can find it in our everyday life, check out Prof Brian Harvey’s abstraction lecture from UC Berkeley’s "the Beauty and Joy of Computing" course: https://www.youtube.com/watch?v=ok_KcxqVrOk

Reusing scripts across different projects

You can export scripts to use them in other projects. This will also export all custom blocks including their definitions and dependencies.

To export a script, you can right click it and select "export…" from its context menu. This will download an XML file to the downloads folder of your browser.

Right click the script to open its context menu. Click on the “export script…” option to download an XML file to the downloads folder of your browser.

To import the script to another Snap! project, you can drag and drop it from your file browser onto that browser tab. Alternatively, you can select the "Import…" option from the file menu to open your computer or tablet’s file browser. Navigate to the xml file of the script and select "OK" to load it into the current browser tab.