Chapter 3: Turtles

The sprite in Snap is based on a much older computational tool from the 1960’s called a Turtle. It first appeared in the Logo programming language. The original Turtle was a robot, and then was turned into an on-screen grahical turtle. Here is Seymour Papert, the inventor of the Turtle, with one of the early robot turtles.

https://el.media.mit.edu/logo-foundation/what_is_logo/graphics/seymour.jpg

The turtle has been ported to many other programming languages today, which makes it an excellent place to explore how Snap programs appear in other programming languages. (Thanks to Chloe Nguyen for these examples.)

Section 1: Draw a Star

Here is a simple Snap program for drawing a star.

_images/start-in-Snap.png

We can make this same program in both Python and in Processing. Click Run to try each of them out.

The Python version is pretty close to the Snap version. We have to first import the library turtle in order to make turtles. We create a turtle name it star. Telling the turtle to turn right and move forward is much like it is in Snap.

P5.js can work with turtles, but it takes a bit of extra code. Ignore that stuff at the bottom, and just focus on what’s in the setUp() function. P5.js requires us to create a canvas where the turtle will draw. We set a background color of 0 (black). Then we make a turtle, put the pen down, then create the five lines of the star. The rest of turning and moving forward is pretty much like what you might expect.

Let’s think about this code a little bit.

Section 2: Draw Squares

Here is a Snap program for drawing a pattern with squares.

_images/script-four-squares.png

It creates a stage that looks like this:

_images/stage-four-squares.png

We can make this same program in both Python and in Processing. Click Run to try each of them out.

The Python version is again close to the Snap version. One interesting difference is the speed function. Try changing it to see what it does.

Section 3: Draw A Spiral

Here is a Snap program for drawing a spiral pattern.

_images/spiral-script.png

It creates a stage that looks like this:

_images/spiral-stage.png

We can make this same program in both Python and in Processing. Click Run to try each of them out.

The difference between the Python and Snap version is that random colors are selected in Python with a random number for each of red, green, and blue. In Snap, we select a random hue between 0 and 100.

Section 4: Draw Triangles

Here is a Snap program for drawing a triangle pattern.

_images/triangle-script.png

It creates a stage that looks like this:

_images/triangle-Stage.png

Below is a Python program that generates the same output, but the lines are scrambled. Put them in the right order. Drag the needed blocks of statements from the left column to the right column and put them in the right order. Click on Check to see if you are right. You will be told if any of the lines are in the wrong order or are the wrong blocks.

You have attempted of activities on this page