Creating Animated Videos Using turtleSpaces

The above animation was created inside turtleSpaces, and then exported as a webm video file using the savewebm primitive. I then used Final Cut Pro to add the audio and the titles.

The Logo code used to create the animation is fairly straightforward. The turtle moves in an arc outwards from the center, creating arcs around it and generating the pattern. The location and sizes of the arcs depends on the current loop iterations. Graphics rendering is suspended while each ‘frame’ is created, after which the code renders the frame and pauses to ensure the user sees it, then continues on.

It runs much faster via the desktop application than in the web version, so you may need to adjust it to slow it down. Tinker with it! Play around. It’s the best way to learn.

TO arcflower3d
  cs
  ;clearscreen

  ;maximize
  ;the above line forces full-screen display. This is needed
  ;with savewebm, because the video capture routine uses the
  ;webgl canvas, and so whatever resolution it displays at
  ;is the resolution the video file will be. Uncomment it if
  ;saving video

  ht
  ;hideturtle

  setpw 2
  ;setpenwidth

  ;savewebm 260
  ;uncomment the above line to record video

  repeat 3600 [
    norender
    ;suspend rendering graphical output

    cs
    
    repeat 128 [
      setpc 1 + remainder repcount 15
      ;set the pencolor to the remainder of a modulus
      ;of the current loop count (repcount)

      pu
      ;penup

      rarc 0.1 * repabove 1 repcount
      ;move in an arc trajectory to the right
      ;based on the current repcount and the 
      ;repcount of the loop above (repabove)

      rr 0.1
      ;rollright

      dn 0.1
      ;down

      arc 90 repcount
      ;create a 90 degree arc based on the current
      ;repcount
    ]

    render
    ;resume rendering

    nextframe
    ;wait until one frame is rendered
  ]

END

 

A Starry turtleSpaces Logo Introduction Part Three: Starwarp Improvements

In the previous episode, we created a progressively-generated starfield we moved through with the camera turtle, creating a cool flying-through-space effect.

But it has a few issues we should address. Firstly, it is possible for a star to end up flying through the windscreen of our spaceship! Which is cool, but looks a bit strange. Second, as the program runs it piles up all of these stars behind us, which can slow everything down. We need to get rid of those. Finally, it would be pretty neat if we could have the stars ‘pop’ into view, so we’ll explore how we can do that.

Improvement One: Spaced Out Stars

So, firstly we want to ensure that stars aren’t placed too near to the center, where they could possibly fly through our windshield. Ideally we want to detect if we’re going to place a star within the narrow barrel our ship is flying through, and if so don’t place it there, place it somewhere else.

There are a few new tools we can use to accomplish this:

distance – takes two lists of coordinates, eg [x1 y1 z1] [x2 y2 z2], and returns the distance between them. This will be useful to us, because we are going to provide the prospective new spot position as one list, and {0 0 zpos} as the second list, giving us the distance between the new spot and the XY center of the space at Myrtle’s current Z position.

dountil – repeats a list of commands until it gets the desired results. In this case, we’re going to want to have dountil pick a random position, and then use the distance function to check if that position is outside of our no-go range. dountil executes the list of instructions it is provided before checking the condition it needs to stop executing, while its cousin until checks first.

So, to ensure we don’t place a star within that ‘barrel’, instead of the existing setposition command, we do the following:

dountil 100 < distance position {0 0 zpos} [
  setposition {-1500 + random 3000 -500 + random 1000 zpos}
]

So, until 100 is less than the distance between the turtle’s position and the XY center of the turtle’s current Z position, keep choosing a new position — and choose a new position before doing the first check.

Note: because of the way Logo’s parser works, if you do a comparison operation (<, >, = etc) where one side is a single parameter (eg a number) you’re best to put that FIRST and the complex parameter second.

Why? Because Logo collects things up right to left, and while Logo will evaluate the stuff to the right of the operator correctly, passing that to the operator, the parser will give the operator the first ‘complete’ thing it sees to the left of it, which if you switched things around would be {0 0 zpos} and is not what we want!

So to solve this you would need to put round brackets () around distance position {0 0 zpos} to ensure that Logo evaluated and gave < what we really wanted to give it. It’s easier in this case just to put the single parameter on the left.

This solves our problem! Stars will keep out of our way. However, this method also moves the turtle every time we try a new position, and all of these false jumps will stay in the turtle’s ‘turtle track’ or list of things the turtle has done.

There are a few methods we could use to stop this from happening, but this one is probably the simplest:

dountil 100 > distance :position {0 0 zpos) [
  make "position {-1500 + random 3000 -500 + random 1000 zpos}
]
setposition :position

Rather than set the position every time we try, we make a container (variable) called position containing the prospective coordinates, and test that instead. Then, once we have a good set of coordinates, we set that :position using the setposition command.

A colon before a word indicates to the parser that it is meant to pass the contents of a container with that name to the next command or function in the chain.

The colon is shorthand for thing, which returns the value of the container named passed to it. So, for example, you could have used setposition thing “position instead. Note that the name of the variable is preceded by a quote, not a colon. If you used a colon, thing would return the value of the container with the name CONTAINED inside of the container you referenced with the colon!

The mind boggles, doesn’t it?

This is also why you generally make “container rather than make :container — if you did the second, you would make a container with the name contained inside of the container you referenced, which does have practical applications but can be a bit confusing at first.

For now, just remember you make with a quote, retrieve with a colon.

Okay, moving on…

 

Improvement Two: Galactic Janitorial

This procedure creates a lot of stars. Like, a lot. While you can have a lot of things in turtleSpaces (so many things!) they can start to gum up the works if they get to extreme numbers. A computer can only remember so much you know! And so, we should clean out the stars behind us, because they don’t matter to us anymore anyway.

But the stars are part of Myrtle’s ‘turtle track’, and so we can’t just clean out some of them, can we? There are the clean and clearscreen commands but they get rid of everything!

Never fear, tag is here!

tag allows you to wrap one or more turtle track entries so that you can reference them later, to replace, copy or delete them. We’re going to put tags around stars so we can delete them. We do this with begintag and endtag.

We need to give each tag a name, and so we need to give begintag a name to use. endtag doesn’t take a name, since we can only close the last opened tag. If you create a tag within a tag, that tag has to be closed first, before you can close the tag above it.

What we’re going to do is every 2000 stars, we’re going to close off the existing tag (the stars still  in front of the camera turtle), then create a new tag for the next batch of stars, and erase the tag that came before the one that we just closed off (the stars now behind the camera turtle). It’s a real slight-of-hand magic trick!

To set up our tags, first we have to add the following before the forever loop:

  begintag 0
  endtag
  ;dummy 'first' tag (numbered 0)
  make "tag 1
  ;create tag container
  begintag :tag
  ;create second tag using the tag
  ;container value

Because we’re erasing the tag before the previous tag, we need to create a dummy 0 tag to erase when we get started. Then we create a tag container, which contains the value (number) of the current tag, and then we create a tag with a name of that value (1).

We’re all ready to go! Now, inside of the forever loop, we need to add:

    if divisorp 2000 loopcount [
      ;every 2000 loops:
      endtag
      erasetag :tag - 1
      ;erase the tag BEFORE the tag we just closed
      inc "tag
      ;increment the tag container
      begintag :tag
      ;start a new tag
    ]

if is sort of like dountil, except that it checks if the condition is true first and then executes the list of instructions provided to it, but it only does it once and only if the condition is true.

divisorp is a boolean, it returns only true or false. Because of booleans, unlike in other programming languages if does not require a comparison operator. Comparison operators (=, >, < etc) are themselves boolean operators — they return either true or false to if, until, dountil or whatever other command needs a boolean parameter passed to them.

divisorp returns true if the first value passed to it divides equally into the second value. divisorp  in this case returns true if 2000 divides equally into loopcount, which is the total count of the number of loops executed  — for example, the number of times we’ve been through the forever loop.

So every 2000 times through the forever loop, divisorp returns a true value and that causes if to execute the contents of the list. Which is:

endtag – end the current tag

erasetag :tag – 1 – erase the tag before the tag we just closed

inc “tag – increment the tag counter

begintag :tag – start a new tag

And that’s all there is to it. The janitor will come every so often and sweep out those old stars, keeping things running smoothly!

 

Improvement Three: View Screen On

I’m kind of torn on which is cooler, having the galaxy wink into existence around us or approaching it as if we’ve arrived from the intergalactic void, but if you want to try the winking-into-existence option, here’s how to do it:

First, before the forever loop, we put in a norender command. This stops turtleSpaces from updating the ‘render’, or representation of items in 3D space.

Second, inside the forever loop, before everything else, we need to put the following:

    if loopcount = 1500 [
      render
      print |Engage!|
    ]

which as you may remember from the previous section, after 1500 loops will cause if to execute the render command, which turns rendering back on. This ensures there are stars for us to see before we start to see them.

That’s all there is to it!
Bonus Improvement: Spinning Through Space

As a final bonus improvement, after the cam:forward command, insert the following:

cam:rollright 1/10

This will simulate the gentle roll of the spacecraft as it travels through the stars, causing the stars to spin slightly around the spacecraft.

Congratulations! You’ve graduated from starwarp academy! Good job. Welcome to turtleSpaces.

 

A Starry turtleSpaces Logo Introduction Part Two: Starwarp

In this second part of our introduction to turtleSpaces Logo, we’re going to take the stars we made in the first part, and create a ‘rolling’ starfield we are going to move through using the camera turtle, to create a Star Trek-style warp effect.

To create this effect, the drawing turtle, Myrtle, is going to create stars deep into the space. The camera turtle, Snappy, will move forward (the camera turtle points into the space, towards Myrtle, by default) following Myrtle as she moves deeper, creating stars.

This tutorial is in three parts: First we’ll create the moving starfield, then we’ll cause old stars to vanish (and explain why we need to do that), and finally we’ll set things up so that the starfield ‘pops’ into view, rather than being shown from the beginning.

To begin, we’ll create a new procedure:

TO starwarp
END

and then we’ll start with some setup commands:

TO starwarp

  reset
  hideturtle
  penup
  lower 3000

  setfillshade 12
  setpenshade -12
  gradient

END

We know all of this already from the first part, except for lower, which causes the turtle to descend, from its point of view.

Then we’ll add the main forever loop, which will repeat, well, forever:

  forever [
  ]

Then we’ll populate it with the commands needed to make the rolling starfield, and explain them:

  forever [
    
    lower 10
    setposition {-1500 + random 3000 -500 + random 1000 zpos}
    randomfillcolor
    setpencolor fillcolor
    spot 0.1 * (1 + random 50)
    cam:forward 10

  ]

This is the basic routine, but it can use a lot of work, which we’ll get to in a moment.

lower 10 – lowers the turtle 10 turtle units. The turtle descends from its position and orientation, like an elevator.

setposition {-1500 + random 3000 -500 + random 1000 zpos} – this is a bit complicated. setposition sets the turtle’s position in 3D space. It takes a list of three values, X Y and Z. It does not change the turtle’s orientation.

The center of 3D space is [0 0 0]. From Myrtle’s default position, to her left is negative on the X axis, to her right is positive. To her rear is negative in the Y axis, to her front positive. Below her is negative in the Z axis, while above her is positive.

And so, we’re passing a list to setposition made up of two random calculations (for the X and Y coordinates) and Myrtle’s existing Z co-ordinate, which is expressed by the zpos primitive, which is a function that returns Myrtle’s current Z coordinate.

Why the curly braces? Well, traditional Logo lists such as [pig duck cow] aren’t dynamically generated — if you want to remove, add or change values inside of them, you need to do so using commands that manipulate the list. But this can be a bit tedious and so we created the concept of ‘soft lists’ (as opposed to traditional ‘hard lists’) which are lists whose contents are evaluated (or solidified) at runtime, when the interpreter actually processes and executes the command to which the list is attached.

And so, with soft lists, each item is usually either a function (such as random) or a value (such as 10). If you want to add a string value to a softlist, you need to precede it with a ” eg “duck or surround it with pipes eg |duck|.

So, when the setposition command is evaluated, the parser (the part of Logo that decides what to do next) sees the softlist, and evaluates its contents, turning it into a hard list. So it generates the two random numbers, and gets the zpos, and then creates a hard list of 3 items, passing it back to setposition.

randomfillcolor – sets a random fill color (as in part one)

setpencolor fillcolor – sets the pen color to the fill color (as in part one)

spot 0.1 * (1 + random 50) – creates a spot of a size from 0.1 to 5

cam:forward 10 – moves the camera turtle (cam is a shortcut turtle name for the current view turtle). Prefixing a command with turtle: causes the named turtle to execute that command.

…and that’s it for version one! Run the starwarp procedure and see what happens.

Pretty cool huh? But it has a few shortcomings, which we will address in part three.

TO starwarp

  ;this procedure recreates the classic
  ;'moving starfield' effect
  
  ;the turtle starts deep into the workspace
  ;(by 'lowering' or decreasing its Z-coordinate)
  ;then creating stars (at least a certain distance
  ;away from the center using the distance function)
  ;and continuing to lower. Meanwhile the camera
  ;moves forward (from its perspective), following
  ;the turtle.
  
  ;Like in reality, the stars are not moving, the
  ;turtles are!
  
  ;On to the code:
  
  reset
  ;reset the workspace
  hideturtle
  ;hide me!
  penup
  ;don't draw
  lower 3000
  ;'lower' into the distance
  
  setfillshade 12
  setpenshade -12
  gradient
  ;gradiented stars
  ;gradiented shapes graduate between the
  ;pencolor / penshade and the fillcolor / fillshade
  
  forever [
    
    lower 10
    ;move the 'star turtle' deeper into the scene
      
    setposition {-1500 + random 3000 -500 + random 1000 zpos}
    ;flat-ish galaxy
    
    ;curly braces denote a 'soft list', a list that
    ;is evaluated and created upon execution
    
    randomfillcolor setpencolor fillcolor
    ;spots and other shapes use the fill color
    ;which randomfillcolor randomly chooses
    ;we set the pencolor to the fill color
    ;for the gradient, because we're only
    ;gradienting the shade
    
    spot 0.1 * (1 + random 50)
    ;make a randomly-sized star
    ;between 0.1 and 5 turtle units in size
    
    cam:forward 10
    ;the camera turtle points towards
    ;what it's looking at, so moving
    ;forward decreases its z position
    ;(in its default orientation)
    
  ]
  ;do this forever and ever
  
END


 

A Starry turtleSpaces Logo Introduction Part One: Starfield

Traditional Logo had new users build a house as an introduction, but due to turtleSpaces’ 3D nature, starfields are much more impressive, so we’ll start there.

Click and drag the window above to see all the stars!

Cool huh? First, we’re going to create this simple starfield that wraps around the camera position.

We’ll start by creating the procedure:

TO stars
END

Then we’ll add in some setup stuff:

TO stars

  reset
  hideturtle
  penup

  setfillshade 12
  setpenshade -12
  gradient

END

reset – resets the workspace to its default configuration
hideturtle – hides the turtle
penup – doesn’t draw lines. The turtle draws lines as it moves by default

setfillshade 12 – sets the fill shade to 12. Shades have a range of -15 (light) to +15 (dark) where 0 is normal
setpenshade -12 – sets the pen shade to -12 (light)
gradient – causes shapes that support gradients to use them. They graduate from the pen color / shade to the fill color / shade

We don’t need to use the gradients, but the starfield looks so much better with them enabled!

So, now we’ll carry on and create our main loop:

  repeat 1000 [
  ]

This will create 1000 stars, once we fill in the rest of it. Let’s fill in the loop and then go through each command:

  repeat 1000 [
    randomvectors
    forward 500 + random 1000
    up 90
    randomfillcolor
    setpencolor fillcolor
    spot 1 + random 10
    home
  ]

…and that’s it! Not a lot, is it? Let’s go through it step-by-step.

randomvectors – sets a random three-dimensional orientation for the turtle. This is the equivalent of going left random 360 up random 360 rollright random 360 but in an easier and faster method. Why vectors? Because vectors describe the turtle’s orientation in 3D space, as well as the orientation of all other objects in it. Why build-in a shortcut? Because we wan’t to be easy to use!

forward 500 + random 1000 – move forward 500 turtle-units PLUS 0-999 turtle-units. random returns a random value between 0 and one less than the given value, because 0 is one of the (in this case) possible 1000 choices. This gives our starfield depth while making sure the stars aren’t too close!

random is a function, it doesn’t do anything on its own. Try typing random 30 at the prompt and see, you’ll get:

I don’t know what to do with 10

for example. That value needs to be ‘passed’ to another function or command — its output needs to become someone else’s input, in this case +.

Then +‘s output is passed to forward, which then moves the desired number of turtle-units. This is a big part of how Logo works, and is why commands can be stacked on the same line — they gobble up all of the inputs, and once they do they are ‘complete’ and we know to move on.

up 90 – spots are created around the turtle on its z-plane and so we need to tilt the turtle up 90 degrees so that the stars are facing back towards our view point near the center of the space.

(Note that if you used the lower primitive instead of forward, you wouldn’t need to tilt the turtle up.)

(Note also that different shapes may be positioned in different orientations relative to the turtle. The best way to build things is to progressively build them through the REPL (the command-line interface), using the backtrack command to undo any mistakes.)

randomfillcolor – picks a random fillcolor (the color shapes are colored), a value between 1 and 15 that is NOT the current fillcolor. The alternate form of this is complex: make “oldfillcolor fillcolor dountil fillcolor != :oldfillcolor [setfillcolor 1 + random 15]randomfillcolor is nicer. But you could do it the hard way if you want!

setpencolor fillcolor – because we’re using gradients we need to make the pencolor the new fillcolor so that the stars don’t gradient to a different color. fillcolor is a function that returns the current fillcolor.

spot 1 + random 10 – create a spot graphical primitive between 1 and 10 turtle-units in diameter around the turtle. Remember, random 10 will return a value from 0 to 9. If you just did spot random 10 without adding the 1 you might get 0., which while not an error won’t create anything of substance (literally).

home – return the turtle to the home position, which by default is [0 0 0], the center of the space.

Finally, all of this ‘filling’ is a list, passed to repeat to execute. Logo uses lists for all sorts of things, as you’ll see as you progress in your journey through Logo!

Congratulations, you’ve reached the end of this first (ahem) turtorial! Next in part two, we’re going to make a moving star ‘warp’ effect.

Here’s the full commented listing:

TO stars
  ;TO declares a procedure, in this case one called
  ;'stars'. Procedures can be simply called by name
  ;to execute them. So, at the prompt, you can
  ;type 'stars' (without quotes) to execute this
  ;procedure
  
  ;this is a very simple starfield generator and
  ;a good first project for turtleSpaces. All
  ;we're doing is randomly orienting the turtle,
  ;moving forward a random amount and creating
  ;a randomly-sized spot 1000 times.
  
  ;10 commands, one function. Dead simple!
  
  ;But first a little setup stuff...
  
  reset
  ;reset the workspace
  
  hideturtle
  ;hide the turtle
  
  penup
  ;don't draw lines
  
  ;we could omit this but it looks much better this way:
  setfillshade 12
  setpenshade -12
  gradient
  ;gradiented stars
  ;gradiented shapes graduate between the
  ;pencolor / penshade and the fillcolor / fillshade
  
  ;...and that's it for setup stuff!
  ;Now on to the main event:
  
  repeat 1000 [
    ;this means 'do this 1000 times'.
    ;things between square brackets are lists.
    ;repeat is a command that takes the number
    ;of times it is supposed to execute the contents
    ;of a list, and that list itself. What follows
    ;is the contents of that list:
    
    randomvectors
    ;give the turtle a random 3D orientation.
    ;you could do this yourself using a bunch
    ;of movement commands but we like making
    ;things easy!
    
    forward 500 + random 1000
    ;move forward 500 turtle units
    ;plus 0-999 turtle units
    ;(random returns a value between
    ;0 and the number passed to it
    ;excluding that number)
    
    ;random is a function that does not
    ;'do' anything on its own. Try typing
    ;'random 30' at the prompt to see.
    ;It returns a random value, which is
    ;then passed to another function or
    ;a command. In this case, random's
    ;output is passed to the + function,
    ;which then adds 500 to it and then passes
    ;its output to the forward command
    
    ;this is a big part of how Logo works
    
    up 90
    ;spots are created around the turtle
    ;on the z-plane, and so we need to tilt
    ;the turtle up
    
    ;try creating a spot eg 'spot 100'
    ;after the workspace has been reset
    ;to see how the spot is placed relative
    ;to the turtle
    
    ;different shapes may be places different
    ;ways relative to the turtle
    
    randomfillcolorsp
    ;shapes use the fill color
    ;and randomfillcolor picks a random
    ;fill color. You can pick one arbitrarily
    ;using the setfillcolor command
    
    setpencolor fillcolor
    ;because we're using a shade gradient, we
    ;need to set the pencolor to the fillcolor
    ;otherwise it would gradient to the default
    ;pencolor as well
    
    spot 1 + random 10
    ;make a spot between 1
    ;and 10 turtle-units in diameter
    ;(remember, random 10 returns
    ;a value between 0 and 9)
    
    home
    ;return to the home position
    ;(where the turtle started)
    
  ]
  ;perform the above list 1000 times
  ;as you can see, lists can be spread out
  ;across many lines
  
  ;So, 14 commands, a fillcolor and a couple of
  ;randoms and that's it.
  
  ;Click and drag the mouse over the view window
  ;to rotate the camera and see all the stars!
  
END

Past and Future Turtles: Logo’s Adventures in Academia (Part 2)

This is a continuation of Past and Future Turtles: The Evolution of the Logo Programming Language (Part 1)

So, Logo was the greatest thing that had ever happened to education, and it was going to foster a bright new generation of geniuses who were going to change the world. We just had to get it out there, and Logo would do the rest.

That was the message its creators sent to the world. And people were listening. In particular, computer manufacturers. It was the early 1980s, an era where the price of a personal computer had dropped into the range of affordability for most of the Western market, with computers such as the VIC-20 and the Sinclair ZX81 costing as little as $100, and even those at the higher-end of the market falling below $1000.

Schools and homes could afford to own computers — barely. But to justify the expense, they needed a little additional something that promised a real benefit to those children that used them. And Logo made that promise: Logo would make children ‘think better’. The children that used Logo were going to build a better world.

An ad for TI-Logo II

And so, nearly every computer manufacturer commissioned their own version of Logo: Apple Logo, Atari Logo, Commodore Logo, TI Logo… the list went on. And they used their Logo to sell their computers: our Logo will teach Timmy and Janie how to think like rocket scientists! Our Logo will give your children the skills they need to become doctors and mathematicians. Just buy them a computer and Logo will do the rest. You can sleep peacefully at night knowing your children now have a bright, secure future!

The message resonated. The manufacturers sold plenty of computers! So many computers. Then the discount wars began between Atari, Commodore, Tandy, Coleco and Texas Instruments and they sold even more computers, with TI and Coleco eventually giving up and dumping their computers on the market for as little as $49.

Sadly, while the computer manufacturers did well, Logo did not. I mean, it did okay in the sales sense, but it didn’t do what it promised. There was a flaw in the Logo ethos: the idea was that if you gave children a tool, and showed them what the tool could do, their natural inquisitiveness would take hold and they would explore the potential of that tool to its limits, learning all of the concepts related to that tool along the way. Sounds great, doesn’t it?

But if they had only looked at domains outside of computer programming, they would have realized that this was a flawed premise. How many pianos and guitars languish in children’s bedrooms, having been played only once or twice? Painting sets? Meccano? Electronics kits? I could go on and on… the point of course is that the vast majority of people (not just children) don’t really do anything without an impetus to do so, an external drive. Very few of us are actually driven to create on our own.

To make matters worse, the Logo of the early 1980s did not actually have great utility. While the language was easy to learn and use, it was very resource-hungry and slow. And this meant it wasn’t useful for creating the thing those children who actually had that self-drive wanted to create: games! And so, Logo’s purpose for them was to simply demonstrate that you could make the computer do things, and once they understood that, they quickly moved on to BASIC and Assembly language, and Logo languished unused.

This was perhaps the only Logo ‘magazine’, a small section inside TI99er magazine

Logo’s lack of utility also meant there were few resources available for it, because programs that can demonstrate a programming language’s utility were an extension of that utility, which was extremely limited graphically to drawing algorithmic art and charts. While it was still very useful for text-based applications, everyone wanted graphics! Graphics were king. Sound was queen (and all most versions of Logo could do was TOOT a tone given in hertz, which made even playing Mary had a Little Lamb a tedious exercise of transcribing from a table buried in the back of a reference manual).

And so libraries and bookstores were full of books and magazines that had listings in BASIC and machine language, maybe a little Pascal and Forth and virtually no Logo. Logo what? Never heard of it! Turned out into the cold, the turtles retreated back into the schools, their quest to find a place out in the wider world a failure (they went to make it on Broadway and, as the story goes, they didn’t make it.)

But they still had education — they were well-established there, the teachers had at least been taught the basics and they were going to keep teaching it until told otherwise. Logo was there to stay.

There were two main companies that had been commissioned by the computer manufacturers to develop versions of Logo who were more than happy to continue to support Logo in the education market. The first was Logo Computer Systems Inc (LCSI), which was founded by some of the original Logo designers including Seymour Papert, and which had created Logo for the Apple II and Atari.

Terrapin started out in 1979 making robotic turtles

The second was Terrapin, which had developed versions of Logo for a number of computers including the Commodore 64. These two companies continued to develop and release new versions of Logo for subsequent computer systems, including the Macintosh and those running Microsoft Windows, adding utility as time went on to encourage Logo’s use, and emphasizing Logo’s strengths such as list manipulation.

For example, LogoWriter, released by LCSI, presented a word-processing like environment where users could use Logo to manipulate and generate text inside it, creating macros, templates and performing search-and-replace operations, amongst others. This was a moderately successful attempt to find an application for Logo that was less reliant on performance and better showcased its features.

Terrapin, meanwhile, created more accessible versions of turtle graphics for younger users. This trend continued, with Terrapin seeming to cater more for younger children, while LCSI grew with its users, working to make their offerings suitable for older students, into junior and senior high school.

From LogoWriter they went on to develop Microworlds, a Logo-based environment that leveraged the graphical user interface of the computer’s operating system while at the same time binding itself to it. However, there was never a goal of Microworlds projects ever leaving its environment — like Hypercard, what was made in Microworlds stayed in Microworlds.

Early editions of Coding for Kids for Dummies used Microworlds as their learning platform

Which was fine, because the purpose of Microworlds was to demonstrate the power of programming and other concepts, not to be an end unto itself. But it reinforced the notion that Logo was not a practical programming language, and outside of the education setting, Logo fell further into irrelevance, despite the fact that as computers advanced in power, it became increasingly more viable.

But thankfully, post-secondary academia would come to the rescue, realizing Logo’s potential. University of California, Berkeley lecturer Brian Harvey would spearhead development of a version of Logo first released in 1992, UCB or Berkeley Logo, which was more suited to post-secondary computer science education and featured concepts such as multi-dimensional arrays and advanced list handling functions.

As computers became capable of handling large amounts of independent turtles, those interested in real-world simulation took notice. In 1999 Northwestern University released NetLogo, designed to use Logo to model a number of phenomena in economics, physics and chemistry using turtles and ‘patches’, areas that influenced the turtles in various ways.

In 2001, MIT developed StarLogo, which took Logo and adapted it to facilitate agent-based simulations where hundreds or even thousands of turtles could interact with each other based on simple rules, allowing for the exploration of virtual ant colonies, for example.

On the other end of the education spectrum, Mitchel Resnick and others obtained a grant in 2003 to develop a new programming environment for children. They took up residence at the MIT Media Lab — the site of the development of the original Logo language — and began to develop a coding environment based on the idea of blocks — draggable representations of commands that could be snapped together, eliminating the need to remember complex syntax or rules involving hierarchy.

With Scratch, if the program could be snapped together, it would at the very least execute, although it may not do what you want. You can drag and place objects (turtles) into a starting position, building a scene and then animating it, using logic. This borrowed a lot from Microworlds, while adding the blocks element. Scratch has been very successful, becoming the defacto tool for teaching introductory coding (a new version, Scratch Jr, does away with language entirely, using pictographic blocks).

Blocks based coding became all the rage. But it created a new problem: how to transition children from blocks to text? Particularly when the complex syntax of languages like Python was the reason for the creation of blocks in the first place?

In some places, this is still Logo — LCSI and Terrapin both continue to provide versions of Logo that use text. But the numbers of schools that use them are seemingly in decline, with a continuing increasing number of schools doing their best to jump their students from Scratch to Python, with limited success, in the pervasive (and false) belief that most children can ‘only learn one text-based programming language’, as if children have never had any success learning a second spoken language, but yet the belief exists (just like some people believe COVID-19 doesn’t).

And so, Logo stands today at a crossroads. How can it overcome efforts by some parts of the Python community to dispose of it, and take its place as the first text-based language for children? How can it prove itself as a viable programming language in its own right, to overcome concerns (true or not) held by educators that it may be the only language their students are capable of learning, and beat back Python?

After all, despite Logo’s successes (and they are many, numbering into the millions at the level of individual students), Papert’s vision of it as a language with low threshold and no ceiling has not yet come to fruition. Can it with today’s technology? Or how about tomorrow’s?

To explore potential answers to these questions, come back next time for part three: Logo Moving FD

Example: Towers of Hanoi

This rendition of Towers of Hanoi is a simple game to code and makes introductory use of 3D shapes. Recreating it could serve as a great introduction to turtleSpaces and Logo coding in general.

In the game, you attempt to transfer the disks on post 1 to post 3 ending in the same order the disks started in (smallest to largest, starting from the top). To do this, you can transfer disks between posts, but disks can only be placed on top of disks smaller than they are.

This example uses lists to store the disks on each post, and the queue and dequeue primitives to ‘transfer’ the disks from one ‘post’ (list) to another.

The graphical representation of the game is optional and can be added after first creating the game logic, using text output. A potential project would be to first create the game logic, using the game rules (and for the instructor, this example) as a guide. In its most basic form, it can be created using conditionals (if), lists (make, queue, dequeue). loops (label, go), and input/output (print, question) primitives.

Then, add the graphical representation, once the game is working correctly. The example here uses voxels, cylinders and typeset text. Finally, add some camera movements.

The example follows, and is commented:

SETABOUT |Towers of Hanoi (run myrtle:hanoi)|

CREATORS [melody]

NEWTURTLE "myrtle

TO hanoi
  ;This is a simple recreation of the classic
  ;Towers of Hanoi game. It has fairly basic logic
  ;and would nake a great introduction to game creation
  ;using turtleSpaces

  ;as a project, we would create the text-game
  ;first and then add the graphical representation
  ;later (as a 'stretch goal')

  reset
  hideturtle
  penup
  ;reset and set up workspace

  make "moves 0
  ;initialize the moves counter

  ;*** OPTIONAL ***
  make "camerastep 1
  ;define camera 'step'
  cam:orbitdown 90
  ;position camera
  ;*** END OPTIONAL ***

  print |*** TOWERS OF HANOI ***|
  print |Move the disks from rod 1 to rod 3, keeping the same order|
  print |(largest at the bottom to smallest at the top)|
  cursordown
  playsound "doodoo
  ;instructions

  label "start
  question |Number of Disks (3-9)?|
  make "disks answer
  if not numberp :disks [go "start]
  if or :disks < 3 :disks > 9 [go "start]
  ;select number of disks to play with
  ;and validate choice

  if :disks > 6 [print |Wow! You're brave! Good luck!| cursordown]
  ;message of encouragement for hard levels

  ;*** OPTIONAL ***
  control "snappy {"raise 2 * :disks}
  control "snappy {"pullin 140 - (8 * :disks)}
  make "snappypos snappy:position
  make "snappyvec snappy:vectors
  ;position camera based on number of disks
  ;and store the camera position.
  ;this can be omitted if text-only game
  ;*** END OPTIONAL ***

  make "rod1 reverse range {1 :disks}
  make "rod2 []
  make "rod3 []
  ;define the rod lists, creating the disks
  ;on the first rod

  label "select
  ;labels can be returned to using 'go'

  (show :rod1 :rod2 :rod3)
  ;show the rod lists
  (show |Moves elapsed:| :moves)
  ;and the move count

  ;*** OPTIONAL ***
  drawdisks
  ;draw the graphical representation
  ;(via the optional drawdisks user procedure)
  ;*** END OPTIONAL ***

  question |Rod?|
  make "selection word "rod answer
  if not namep :selection [print |Invalid selection!| go "select]
  if (count thing :selection) = 0 [print |Invalid rod!| go "select]
  ;select the source rod and check for validity

  label "dest
  question |Destination?|
  make "destination word "rod answer
  if not namep :destination [print |Invalid selection!| go "dest]
  if (last thing :destination) < (last thing :selection) [
    print |Invalid move!|
    go "select
  ]
  ;select the destination rod and check for validity

  make "disk dequeue :selection
  queue :disk :destination
  ;move the disk, by 'dequeuing' or removing the last
  ;item from the selected rod into a variable,
  ;and then queuing the item (adding it as the last
  ;item) to the destination rod

  inc "moves
  ;increment the moves counter

  ;*** OPTIONAL ***
  ;the following orbits the camera around the game
  ;to spice it up a little:
  inc "camerastep
  ;increment the camerastep container
  cam:repeat 45 [orbitright 1]
  ;orbit the camera 45 degrees right
  if divp 3 :camerastep [cam:repeat 45 [orbitright 1]]
  ;if camerastep divisible by 3 then orbit another 45
  ;so we're not looking at the side of the game
  ;*** END OPTIONAL ***

  if :rod3 = reverse range {1 :disks} [
    drawdisks
    ;update the graphics (OPTIONAL)

    control "snappy {
    "setposition :snappypos
    "setvectors :snappyvec
    }
    ;restore the camera position (OPTIONAL)

    print |You win! Great job. Try more disks!|
    (print |Moves taken:| :moves)
    playsound "applause
    finish
  ]
  ;check the third rod for a completed tower
  ;and if complete, displays 'win' message and quits

  go "select
  ;return (loop back) to select label

END

TO drawdisks
  ;the graphical representation of the towers is
  ;optional, and makes a good 'stretch goal'

  ;you can start with just rendering the disks,
  ;then add a base and rods, and finally
  ;rod numbers (to make it easier to keep track
  ;of the rods when playing with camera rotation
  ;enabled)

  norender
  ;don't update graphics until render is called

  clean
  ;erase graphics

  setposition {-50 - 4 * :disks 0 - 4 * :disks 0}
  setfillcolor brown
  voxeloid 2 * (50 + 4 * :disks) 2 * (4 * :disks) 5
  ;create base of appropriate size

  repeat 3 [
    setposition {-100 + (50 * repcount) 0 0}
    rollright 180
    setfillcolor brown
    cylinder 2 5 + :disks * 5 10
    rollright 180
    raise 5
    ;create rod of appropriate height

    if 0 < count thing word "rod repcount [ ;if there are disks on the rod: foreach "i thing word "rod repcount [ ;for each disk on the rod: setfillcolor :i ;set the color based on the disk size cylinder 2 + (2 * :i) 5 10 * :i ;create the disk raise 5 ] ] ] ;create disks on each rod up 90 settypesize 5 + (:disks / 4) repeat 3 [ setfc item repcount [13 11 14] setposition { (-100 - :disks / 2) + 50 * repcount 0 - 4 * :disks (-14 - (:disks / 3)) - (:disks / 4) } typeset repcount ] ;print numbers under towers rollright 180 repeat 3 [ setfc item repcount [14 11 13] setposition { (100 + :disks / 2) - 50 * repcount 4 * :disks (-14 - (:disks / 3)) - (:disks / 4) } typeset 4 - repcount ] ;print numbers under towers opposite side rollright 180 down 90 ;return the turtle to the proper orientation render ;resume rendering if :moves > 0 [playsound "knock]
  ;if not the start of the game, make a sound

END

NEWTURTLE "snappy


NEWTURTLE "libby


 

How to create and 3D print a chess pawn in turtleSpaces Logo

First, open the weblogo.

Then, click in the bottom right REPL area

Create the ‘head’ of the pawn using the ico primitive

cs penup ico 20


If you start the line with a cs, you can use the up arrow to go back to the line after adding each command (and seeing the result) to edit what you’ve done and add more! Append all of the following instructions on to the same line, then just keep re-executing it.

We’re going to be making a cone next, and cones are created under the turtle. So we need to tiilt the turtle down, and lower it close to the bottom of the ico, in preparation for creating a ‘cut cone’:

dn 90 lo 17

Next we create a cutcone, lower the turtle and create another cutcone. Type help “cutcone to see the parameters…

cutcone 10 20 5 20


Lower the turtle and create the next cone segment

lo 5 cutcone 20 15 5 20


Lower the turtle again and create a larger cone

lo 5 cutcone 10 20 40 20


… smaller cone, but deeper than is visible so that the 3D printer slices the pawn correctly …

lo 40 cutcone 22 28 20 20


… a torus … (type help “torus to see the parameters)

lo 15 torus 5 24 20 20

… another torus …

lo 5 torus 3 28 20 20

… and a cylinder to finish the base! (type help “cylinder to see the parameters)

cylinder 31 5 20


All done! Now you can download the STL file under the File menu, open it up in your slicing program and print it. But don’t forget to type hideturtle first or you might get a surprise!

The pawn sliced in Ultimaker Cura…

The whole line of code should look something like this:

cs ico 20 dn 90 lo 17 cutcone 10 20 5 20 lo 5 
cutcone 20 15 5 20 lo 5 cutcone 10 20 40 20 lo 40 
cutcone 22 28 20 20 lo 15 torus 5 24 20 20 lo 5 
torus 3 28 20 20 cylinder 31 5 20 hideturtle

Easy peasy! Click and hold the left mouse button over the model and drag to rotate it.

Read through the shape guides available under the Docs menu on this website and think about how you could create other chess pieces!

This is the chess pawn as a procedure:

TO pawn
  cs penup
  dn 90 
  ico 20 
  lo 17 
  cutcone 10 20 5 20 
  lo 5 
  cutcone 20 15 5 20 
  lo 5 
  cutcone 10 20 40 20 
  lo 40 
  cutcone 22 28 20 20 
  lo 15 
  torus 5 24 20 20 
  lo 5 
  torus 3 28 20 20 
  cylinder 31 5 20 
  hideturtle
END

You can turn it into a procedure just by typing to pawn in the REPL, pressing the up arrow until you retrieve the pawn code, press enter, and then type end. Then you can save it!

A Fully Commented turtleSpaces Logo Listing of PONG

When I was a kid I had a home PONG machine, one of those that was sold through a department store (in this case Sears) in the late 1970s, which my Dad bought from a garage sale for $5. It was black and white, and the paddles were controlled by knobs on the front of the unit, and the NES had come out by this point and so it wasn’t very enticing for the other kids in the neighbourhood, but my brother and I spent hours playing it anyway.

I’ve decided to take a different approach with this version of PONG, using a single turtle and a single thread taking a linear path through the code, rather than a multi-turtle approach because many programming languages do not have threads and it’s important to think about how you can accomplish things without them.

So, in this example, while the turtle acts as the ball, it also draws the paddles and the scoreboard as needed, and some tricks are used to smooth this over, the way you would in other single-threaded programming environments. Meanwhile, it also demonstrates the directional capabilities of the turtle, and how it operates in the turtleSpaces environment from its own perspective.

This project is divided into a number of user-defined procedures which could be worked on in groups in a classroom setting. There are a number of problems to be solved: moving the ‘ball’, bouncing it off of the walls and paddles, moving the paddles using the keyboard, updating the score. Pong is a well-known game and so its mechanics require little explanation. The key here is how do we do all of that with a single turtle?

Read on to find out!

 

SETABOUT |Use A and Z to control left paddle, K and M to control right paddle|

CREATORS [melody]

NEWTURTLE "myrtle

TO pong

  ;this version of Pong uses a single turtle to re-create
  ;the game, employing a more traditional linear method
  ;rather than a multi-turtle, multi-threaded method.
  ;For a demonstration of the latter, see turtleSpaces
  ;Breakout, under the Examples menu in the web interpreter

  ;this is an extremely thoroughly commented listing, so
  ;please read through. It should be fairly straightforward
  ;to adapt this into a series of lessons for your class.

  ;There are many more comments than lines of code in this
  ;listing! Hopefully this will demonstrate to both you
  ;and your students the simplicity and power of Logo

  ;It might be helpful to first read the introduction to
  ;one of the Logo books available on the turtleSpaces
  ;website

  setup
  ;executes the setup procedure. Here we initialize containers
  ;(variables), draw the playfield, create the ball and paddles

  ;Note: medium-blue keywords indicate user-defined procedures

  forever [
    checkkeys
    ;checks for and acts on player keypresses

    moveball
    ;moves the ball depending on a few factors

    checkball
    ;checks the ball position and acts if necessary
  ]

  ;square brackets indicate lists. In this case, we're
  ;providing the forever primitive (or command) with a list
  ;containing the three procedures we wish to execute
  ;'forever' (and also some comments, which get ignored)

  ;lists can generally be spaced out over multiple lines
  ;for readability. They also discard whitespace between
  ;items in the list. Logo is not a stickler for whitespace
  ;or formatting!

  ;there, that was easy, right? ;)
  ;now for the nitty-gritty...

END

TO setup

  reset
  ;reset the workspace. This returns all the turtles to
  ;their default states and positions

  resettime
  ;we're going to use the system time to 'speed up' the ball
  ;as gameplay goes on, and so we'll reset it now. The time
  ;is not reset by the reset primitive and so we need to
  ;declare it seperately

  penup
  ;no need to draw lines here! But for fun you can
  ;try to comment this line out and see what happens.
  ;It gets kind of messy, particularly because the turtle
  ;'ball' sneaks away and moves the paddles and updates
  ;the scoreboard while you aren't looking...

  noaudiowait
  ;don't delay while audio is played. For historical reasons,
  ;sound primtives (commands) such as toot and playnotes cause
  ;execution to pause while they are being played, but we can
  ;turn that off to make things proceed more smoothly

  makemodel
  ;create the turtle model (a voxel, or 3D pixel, to keep
  ;in the 1970s mood). This is a user-defined procedure

  arena
  ;draw the 'arena' or playfield. This is also a user-
  ;defined procedure

  make "keytimer 0
  ;here we 'make' a 'container' used as a counter
  ;to limit the rate at which keys can be pressed
  ;and prevent 'flooding' of the game with too many
  ;keypresses

  make "movepaddle false
  ;used to indicate if the paddle has been moved
  ;and increase the speed of ball movement to allow the
  ;game to 'catch up'

  make "leftscore 0
  make "rightscore 0
  ;initialize the score containers and assign them
  ;values of 0

  updatescore
  ;set up and draw the scoreboard. This is a user-defined
  ;procedure

  make "leftpaddle -20
  drawpaddle "leftpaddle
  ;set the initial position of the left paddle
  ;and draw it, using the user-defined drawpaddle
  ;procedure, to which we pass the name of the paddle

  make "rightpaddle -20
  drawpaddle "rightpaddle
  ;set up and draw the right paddle

  home
  ;reset the turtle's position to the home position
  ;and its orientation to the default

  showturtle
  ;show the turtle (ball)

  right 10 + (random 70) + ((random 3) * 90)
  ;set starting angle for the ball by turning
  ;the turtle to the right a random amount, ensuring
  ;that it doesn't send the ball straight up or down!
  ;That would get boring very quickly...

  ;Note:

  ;Round brackets () ensure the order of operations
  ;is correctly processed. turtleSpaces uses BEDMAS
  ;but processes equal-order operations right to
  ;left (like original Logo), which means that without
  ;brackets, the last 'random' primitive would be passed
  ;270 (3 * 90) which is not what we want!

  ;All right, we're all ready to go!
  ;let's return back to the main pong procedure...

END

TO makemodel

  setpremodel [setvectors [[0 1 0] [0 0 1] [1 0 0]]]
  ;setpremodel takes a list of commands, in this case
  ;we're providing it with a single command which itself
  ;takes a list of three lists, indicating orientation
  ;vectors (you don't need to worry about vectors for now).
  ;But you can see in this example how lists can get nested
  ;on a single line.

  setmodel [
    setfillcolor yellow
    ;there are 16 default colors, and they each have an
    ;associated keyword, which just returns the index
    ;number of the color, which in the case of yellow is 13

    setfillshade -5
    ;you can set a shade for the color, which can be a
    ;value between -15 and 15. Excluding pure white and black
    ;there are 434 default colors, created using a combination
    ;of shade and color. But you can also define arbitrary
    ;colors using the definecolor primitive

    back 2.5 raise 2.5
    ;raise elevates the turtle in the Z dimension,
    ;and this is the extent of the use of 3D movement
    ;primitives in this source code listing

    slideleft 2.5 voxel 5
    ;because voxels are created to the front,
    ;right and beneath the turtle, we need to
    ;move the turtle before making the voxel if
    ;we want the voxel to be centered on the
    ;turtle's position
  ]

  ;The contents of lists can be spaced out across
  ;multiple lines for easier readability

  ;some explanation:

  ;The setvectors command inside the setpremodel command
  ;'fixes' the orientation of the voxel used as the turtle
  ;model regardless of the orientation of the turtle itself
  ;to appear more like a classic Pong pixel, while
  ;the setmodel primitive creates the voxel model itself.

  ;(For technical reasons, you can't assign a fixed
  ;orientation to a model inside a setmodel command)

  ;Comment out the makemodel command in the setup procedure
  ;by preceding it with a semicolon (like these comments are)
  ;to play instead with the actual turtle, and watch it as
  ;it changes direction when it bounces off the walls and
  ;paddles (THIS IS RECOMMENDED)

  ;This is because we use the turtle's current 'heading'
  ;or direction to determine how much to turn when we bounce
  ;the ball (or turtle) off of things. In reality, the turtle
  ;is constantly moving forward

END

TO arena

  ;let's draw the playfield:

  setpencolor lightblue
  setpos [0 -100]
  ;setpos takes a list of two values, X and Y.
  ;Remember, coordinates behind and to the left of
  ;the turtle's default position (at the center of
  ;the screen) are negative.

  ;Another primitive, setposition, takes a list
  ;of three values (X, Y and Z) with Z being positive
  ;above the turtle's default position, and negative
  ;below it. But because we're only working in two
  ;dimensions, we can use setpos here

  repeat 20 [mark 5 forward 5]
  ;draw dotted center line
  ;using 20 'dashes' or marks.
  ;the mark primitive uses the pen color
  ;to create a filled rectangle, like a marker

  setpc mediumblue
  ;setpc is shorthand for setpencolor. setfc is
  ;similarly shorthand for setfillcolor

  ;mediumblue returns 6, the palette index of
  ;the color that is a medium blue. Functions can
  ;be chained in intricate ways, passing their return
  ;values to other functions and finally commands
  ;(primitives that do not return a value). Logo is
  ;very flexible this way!

  setpos [-200 -100]
  right 90
  ;turn the turtle to the right 90 degrees
  mark 400
  ;mark the bottom line

  setpos [-200 100]
  mark 400
  ;mark the top line

  ;That was simple! You could make it more complex
  ;if you like, but the retro aesthetic is groovy!

END

TO moveball

  if :movepaddle = false [
    repeat 4 [forward 1]

    ;if the paddles haven't been moved since the last
    ;time we moved the ball, let's move it four turtle
    ;units forward

    if and xpos < 145 xpos > -145 [
      make "move time / 1000
      if :move < 20 [repeat int :move [forward 1]]
      else [repeat 20 [forward 1]]
    ]
    ;if the ball is presently well inside the area between
    ;the paddles, lets move the ball a bit more based on the
    ;amount of time elapsed since the round has started
    ;(to a maximum of 20 turtle units)

    ;This way, the ball will get faster and faster
    ;until someone misses it!

  ]

  else [
    ;if the paddles HAVE been moved...

    if and xpos < 145 xpos > -145 [
      ;and the ball is well inside the area between the paddles...

      repeat 10 [forward 1]
      ;move 10 turtle units instead of 4 so we can 'catch up'
      ;and reduce the 'lag' caused by moving the paddle

      make "move time / 1000
      if :move < 20 [repeat int :move [fd 1]] [repeat 20 [fd 1]] ;also apply additional time-based 'speed' as above... ;fd is a shortcut for forward. Also if you supply a second ;list of instructions to an if statement, it will execute ;the second list if the comparison provided is false, ;similarly to the else primitive (although you can use ;else later in a procedure as it will take note of the ;result of the last comparison.) ] else [ repeat 4 [forward 1] ] ;but if the ball is closer to the paddles then let's move ;it only four, to provide a little more help catching it ;but also to ensure we detect the ball has 'hit' the ;paddle and doesn't accidentally pass through it, which ;will cause the player distress! make "movepaddle false ;finally, reset the movepaddle 'flag' to false ;(since we've dealt with it) ] ;and we have moved the ball! END TO checkball ;in this procedure, we check to see if the ball has ;passed over the boundary at the top and the bottom of ;the playfield, or if it has 'touched' the paddles, ;or if it has gone out of play, and we act accordingly if ypos > 100 [
    ;if the ball has exceeded the top boundary of
    ;the playfield (the center of the playfield
    ;has x and y values of 0, which increase going
    ;upward and to the right, and decrease going downward
    ;and to the left):

    toot 600 10
    ;make a 600 hz tone for 10/60ths of a second

    if heading > 180 [left 2 * (heading - 270)]
    ;the turtle's heading is a degree value increasing
    ;from zero in a clockwise direction (to the right)
    ;and so when the turtle is pointing right, its heading
    ;is 90, when pointing down 180, and when up 0.

    ;Here we check if the turtle is pointing to the left,
    ;(has a heading value greater than 180) and if so, we
    ;turn the turtle left twice the value of the heading
    ;minus 270 degrees, because we know the value of the
    ;heading is going to be greater than 270 degrees since
    ;the turtle is at the top wall.

    ;And so we turn double the angle between the turtle's
    ;current heading and the angle of the wall, thus causing
    ;the turtle to 'bounce' off of the wall

    else [right 2 * (90 - heading)]
    ;otherwise, we can assume the turtle is pointing to
    ;the right, and we do a similar calculation, instead
    ;subtracting the heading from 90 degrees, because
    ;we know the heading is going to be 90 degrees or less,
    ;based on the turtle striking the top boundary, and
    ;the turtle pointing to the right.

    forward 2 * (ypos - 100)
    ;because the ball can move more than one turtle unit
    ;at a time in order to make the gameplay speedy, we
    ;need to bring the ball back 'in bounds' so that we
    ;don't inadvertently read that the ball is out of bounds
    ;again before it has a chance to re-enter the playfield

    ;there is probably a more accurate way to do this, but
    ;simply doubling the distance the ball is out of bounds
    ;seems to be sufficient. But if the ball ever gets
    ;'stuck' out of play, you know what you need to fix!

  ]

  if ypos < -100 [ toot 600 10 if heading > 180 [right 2 * (90 + (180 - heading))]
    else [left 2 * (90 - (180 - heading))]
    forward 2 * abs (ypos - -100)
  ]

  ;this is similar to the above, except with the bottom
  ;boundary. Note that because the location of the bottom
  ;boundary is negative, we need to get the absolute (positive)
  ;value of the current turtle position minus the boundary
  ;(using the abs primitive) because the result of that
  ;calculation is otherwise negative

  ;now we check the paddles:

  if and xpos > 164 xpos < 171 [ ;if the ball is in the right paddle X 'zone': if and ypos > :rightpaddle ypos < (:rightpaddle + 40) [ ;AND if the ball is in the right paddle Y 'zone' (the ;area currently occupied by the paddle): toot 500 10 ;make a 500hz tone for 10/60ths of a second if heading > 90 [right 2 * (90 - (heading - 90))]
      else [left 2 * heading]
      ;this is similar to the top and bottom boundary
      ;calculations, except instead of changing based on
      ;if the turtle is facing right or left, here we
      ;do different calculations based on if the turtle is
      ;pointing downward or upward

      right -20 + (ypos - :rightpaddle)
      ;apply 'english' to the ball -- depending on the
      ;location on the paddle the ball is striking, turn
      ;the turtle to the left (which it does when a negative
      ;number is provided to the right primitive) or
      ;the right a related number of degrees. This allows
      ;the player to affect the trajectory of the ball
      ;and makes the game more interesting!

      forward 2 * (xpos - 164)
      ;make sure the ball is no longer in the 'strike'
      ;zone for the paddle, because otherwise it could
      ;be detected again and cause some strange behavior.
      ;There's probably a better way to do this, but
      ;this method seems to suffice

    ]
  ]

  ;Let's do this all again for the left paddle:

  if and xpos < -164 xpos > -171 [
    ;if the ball is in the left paddle X 'zone':

    if and ypos > :leftpaddle ypos < (:leftpaddle + 40) [ ;and the ball is in the vertical area occupied by ;the paddle: toot 500 10 ;toot if heading > 270 [rt 2 * (360 - heading)]
      else [lt 2 * (90 - (270 - heading))]
      ;bounce

      left -20 + (ypos - :leftpaddle)
      ;apply 'english'

      forward 2 * (abs (xpos - -164))
      ;get away from the paddle
    ]
  ]

  ;finally, we check if the ball has sailed past a player:

  if or xpos > 200 xpos < -200 [ ;if the ball's position exceeds either the left ;or right boundaries: toot 100 100 ;make a 100hz tone for 100/60ths of a second ;(1.66 seconds) if xpos > 200 [inc "leftscore]
    else [inc "rightscore]
    ;if the ball is past the right boundary, credit the left
    ;player with a point. Otherwise, credit the right player
    ;with a point. The inc primitive increases the value of
    ;the specified container by one. Note that it takes a
    ;quoted name, not a colon name for the container.

    updatescore
    ;update the score

    resettime
    ;we reset the time because we're using it to
    ;speed up the ball

    wait 100
    ;wait 100/60ths of a second, for the toot to
    ;finish sounding

    if or :leftscore = 10 :rightscore = 10 [
      ;if either player's score is now 10:

      hideturtle
      setpos [-90 -20]
      setfillcolor orange
      foreach "i |GAME OVER| [typeset :i wait 10]
      ;type out GAME OVER, but with a delay between
      ;each character, for dramatic effect

      audiowait
      playnotes "L3B3A3G3F3L6E3
      ;play a little ditty, and wait for it to finish

      finish
      ;game over, man, game over!
    ]

    clean
    arena
    updatescore
    drawpaddle "leftpaddle
    drawpaddle "rightpaddle
    ;Because we're using a single turtle for this
    ;game, it is a good idea to 'clean' and redraw
    ;the game elements between rounds, because otherwise
    ;the 'turtle track' will gradually accumulate all of
    ;the ball movements and slow down its rendering over
    ;time. We want a speedy game so let's clean it up

    home showturtle
    right 10 + (random 70) + ((random 3) * 90)
    ;reposition, and randomly orient the ball
  ]

  ;we're done for now!

END

TO checkkeys

  ;in this procedure, we will check to see if a key
  ;has been pressed, and if so, act accordingly
  ;(by moving a paddle, if a paddle movement key
  ;has been pressed)

  inc "keytimer
  ;Because of key repeat, we want to ensure the player
  ;can't 'flood' the game with too many keypresses, and
  ;by using a simple counter, we can ensure this
  ;doesn't happen

  if and keyp :keytimer > 1 [
    ;and so, we check to see if a key has been pressed
    ;(keyp) AND the :keytimer container's value is
    ;at least two. That means at least two ball movement
    ;cycles have to pass between paddle moves, keeping
    ;the game moving!

    make "keytimer 0
    ;reset the keytimer container to 0

    make "key readchar
    ;take a key from the keybuffer and put it into
    ;the key container

    clearchar
    ;clear the keyboard buffer. If we don't, key
    ;repeat (or a player hammering the key) can clag
    ;up the game

    if :key = "a [if :leftpaddle < 60 [ make "leftpaddle :leftpaddle + 20 drawpaddle "leftpaddle ] ] ;if the 'a' key is pressed, and the left paddle isn't ;already as high as it can go, increase its position ;by 20 turtle units and redraw it if :key = "z [if :leftpaddle > -100 [
        make "leftpaddle :leftpaddle - 20
        drawpaddle "leftpaddle
      ]
    ]
    ;if the 'z' key is pressed, and the left paddle isn't
    ;already as low as it can go, decrease its position by
    ;20 turtle units and redraw it

    if :key = "k [if :rightpaddle < 60 [ make "rightpaddle :rightpaddle + 20 drawpaddle "rightpaddle ] ] ;if the 'k' key is pressed, and the right paddle isn't ;already as high as it can go, increase its position by ;20 turtle units and redraw it if :key = "m [if :rightpaddle > -100 [
        make "rightpaddle :rightpaddle - 20
        drawpaddle "rightpaddle
      ]
    ]
    ;finally, if the 'm' key is pressed, and the right paddle
    ;isn't already as low as it can go, decrease its position
    ;by 20 turtle units and redraw it

  ]
  ;that's all for now!

END

TO updatescore

  ;this procedure updates the scoreboard

  hideturtle
  ;hide the ball

  norender
  ;suspend drawing the graphical elements while
  ;we update the scoreboard

  settypesize 20
  ;set the size of the type, the graphical text

  if tagp "score [erasetag "score]
  ;if there is already a score 'tag', erase it.
  ;Tags mark areas of the 'turtle track' so that
  ;they can be copied, removed or used to create
  ;turtle models

  begintag "score
  ;create a score tag

  home
  ;reset the turtle's position and orientation

  setpos [-60 50]
  setfillcolor red
  typeset :leftscore
  ;type the left player's score

  setpos [40 50]
  setfillcolor green
  typeset :rightscore
  ;type the right player's score

  endtag
  ;close the score tag

  render
  ;resume rendering -- voila, the score is updated!

END

TO drawpaddle :paddle

  ;drawpaddle takes a parameter, which is 'passed' into
  ;the :paddle container, which exists only inside this
  ;procedure. Once we exit this procedure, it vanishes!

  ;We use the value contained in :paddle (the value we
  ;passed to drawpaddle) to decide which paddle to draw
  ;and reduce the amount of code we need to write (since
  ;we only need one procedure for both paddles)

  norender
  ;because we are going to erase the old paddle and
  ;then draw a new paddle, stop rendering the graphics
  ;so that it just seems like the paddle moved.
  ;It's magic!

  make "heading heading
  make "pos pos
  ;save the current position and heading of the turtle
  ;into two containers with similar names

  home
  ;reset the turtle's position and orientation

  if :paddle = "leftpaddle [
    setfillcolor pink
    setpos {-170 :leftpaddle}
  ]
  ;if the paddle we're drawing is the left paddle,
  ;set the fill color to pink and move to the left paddle's
  ;position

  ;Curly-braces indicate a 'soft list', a list that is evaluated
  ;at the time of execution. Soft lists can contain :containers
  ;and functions which then get resolved to their values / results
  ;before being passed to the primitive they are attached to.
  ;This is how you can dynamically pass values to primitives
  ;that ordinarily take 'hard' [] lists

  ;Note that if you pass a string value in a soft list, you
  ;will need to precede it with a " or place it between pipes ||

  else [
    setfc cyan
    setpos {165 :rightpaddle}
  ]
  ;otherwise set the fill color to cyan and set the right
  ;paddle's position

  if tagp :paddle [erasetag :paddle]
  ;if there's already a paddle, erase its 'tag' from the
  ;turtle track. This erases the paddle, if it exists

  begintag :paddle
  ;create a new 'tag' with the name of the paddle, eg
  ;leftpaddle

  ;tags mark sections of the turtle track for manipulation
  ;later, such as to erase them, or use them to create a
  ;turtle model

  voxeloid 5 40 5
  ;create the paddle voxel

  endtag
  ;close the tag

  setpos :pos
  setheading :heading
  ;reset the turtle's heading and position

  make "movepaddle true
  ;set the movepaddle container to true. This tells
  ;code in the moveball procedure that the paddle has
  ;moved and to make up for the time we lost doing it

  render
  ;start updating the graphic elements again
  ;abra cadabera! The paddle has moved!

  ;We've reached the end of this listing!

  ;Now, exactly how much Python code would you
  ;have had to have written in order to accomplish
  ;all this? Hint: a lot more!

  ;Thanks for reading! I hope this helps you on your
  ;journeys inside turtleSpaces. Bon Voyage!

END

NEWTURTLE "snappy


NEWTURTLE "libby


An Introduction to Logo Movement with Myrtle the Turtle

This catchy song introduces the turtleSpaces Logo movement primitives.

This animation was made inside turtleSpaces, and demonstrates its ability to create animated content.

You can use screen capture software such as ScreenFlow or the built-in SAVEWEBM primitive to export a recording of the screen, and then sync it to your music.

You can also load music in OGG format into turtleSpaces and then work on synchronizing your animation with it in realtime using the SLEEP and WAIT primitives. This animation was done that way. Keep in mind that the animation may play back at different speeds on different computers unless you use the TIME primitive to keep everything locked to timing points!

It took around three hours to create the animation in this video using the Logo programming language. The captions are done by creating a turtle (I named “caption”) and then directing it to create the captions using the CONTROL primitive, eg control “caption [typeset |And I ORBIT all round…|]

 

Example: Plane Trapped in a Torus

This example demonstrates the use of various camera-related functions, shape inversion, the premodel primitive and others to create this cool animation of a plane trapped in a torus!

The procedure first sets the turtle model to the built-in plane model, before creating a tag that sets the color of the torus. Then it creates an inverted torus (one whose inside is rendered instead of its outslde) because we’re going to fly inside it!

We then position the turtle to prepare it for its orbital flight path, position the camera behind it and set up its light. Then we begin orbiting, and while we do so we use setpremodel to move the turtle relative to the camera, creating the drifting motions of the plane as it flies.

Every so often, we change the color of the torus by replacing the contents of the color tag. Cool stuff!

Read through the source code below and take note of the comments, which explain what various parts of the trapped procedure do.

TO trapped
  reset
  setmodel "plane
  ;sets Myrtle's model to the built-in plane model
  
  begintag "col
  setfillcolor 1 + random 15
  endtag
  ;the contents of tags can be used to create models, or
  ;they can be disabled or replaced. We're going to replace
  ;the contents of this tag later in our procedure, to change
  ;the color of the torus 'on the fly' (ba-dum)
  
  torus 30 -50 20 20
  ;creates an inverted torus by inverting the radius parameter.
  ;Closed shapes such as the torus don't ordinarily have an 'inside'
  ;unless we create them inverted.
  
  ;Why do we want to invert it?
  
  ;We're going to switch the camera turtle to Myrtle, and so
  ;we want to see inside the torus, not the outside. To do this,
  ;we invert it, as demonstrated above.
  
  penup
  ;don't forget, the turtle draws a line by default
  
  dropanchor
  tether
  pullout 50
  left 90
  ;We're going to use the orbit primitive to move Myrtle
  ;inside of the torus. So we 'dropanchor' at Myrtle's position,
  ;to set the 'anchorpoint' that the orbit primitives rotate around,
  ;'tether' to keep the anchor point static (because otherwise
  ;when Myrtle turns the anchor point moves to stay in front of
  ;her) 'pullout' 50 turtle units from the anchor point, and then
  ;turn left 90 degrees (which we can do because we called tether)
  
  ;To demonstrate the need for tether, try:
  ;reset repeat 36 [repeat 90 [orbitleft 4] right 10]
  ;As you can see, the point Myrtle orbit arounds moves when she
  ;turns right. Put a 'tether' primitive before the first repeat
  ;and notice the difference!
  
  setpremodel [rt 10]
  ;'setpremodel' allows us to put commands between the 'turtle track'
  ;that contains everything the turtle draws and the turtle model
  ;itself.
  
  ;When we attach the camera to a turtle, its position is
  ;set before premodel (and the turtle model) and so we can use
  ;setpremodel to change the position and orientation of the turtle
  ;as seen from the camera. And so we will see the model pointing
  ;slightly to the right
  
  setview "myrtle
  ;set the camera to show Myrtle's point of view
  
  snappy:setlight 0
  ;turn Snappy (the normal camera turtle)'s light off.
  ;Snappy has a light on by defalt.
  
  setlight 2
  ;turn Myrtle's light on. The value 2 is a point light,
  ;which casts light around Myrtle
  
  setdiffuse [40 40 40 100]
  setambient [0 0 0 0]
  ;these set paramets related to the light.
  ;see their help entries for more information
  
  setviewpoint [5 10 -50]
  ;sets the position of the camera relative to the turtle
  ;as a list of [x y z]. So in this case, to the right, above
  ;and behind the turtle
  
  forever [
    
    setpremodel {
    "right 20 - 30 * (sin 0.5 * loopcount)
    "slideright -5 + 10 * (sin 0.5 * loopcount)
    "up 10 * (sin loopcount)
    "raise 2.5 - 10 * (0.5 * sin loopcount)
    }
    
    ;curly braces indicate a 'softlist', a list that is
    ;evaluated at the time of execution (the point at
    ;which the interpreter interprets the list).
    ;They allow us to use functions and containers to
    ;assemble a list dynamically.
    
    ;In this case, we're using the sin function and loopcount
    ;to move the turtle model relative to its position and
    ;simulate the motion of an aircraft in flight.
    
    ;loopcount is similar to repcount, but is used in non-repeat
    ;loops such as forever, while, until, dowhile, dountil and
    ;foreach.
    
    ;it counts the number of times the loop has been executed
    
    orbitleft 0.5
    ;orbit to the left half a degree. In this case, 'to the left'
    ;causes the turtle to appear to move forward. But remember,
    ;we turned the turtle to the left earlier in the procedure!
    
    if divp 300 loopcount [
      replacetag "col {
      "setfillcolor 1 + random 15
      "setfs -15 + random 30
      }
    ]
    ;divp (or divisorp) returns true if the numerator (the first
    ;number) divides equally into the denominator (the second
    ;number, in this case the loopcount.)
    
    ;if divp returns true, we replace the "col tag we created
    ;earlier in the procedure with new contents setting a random
    ;color and shade. We need to use curly braces so that the
    ;random numbers are generated at the time we call replacetag.
    
    ;note that the primitives need to have a " in front of them
    ;so they are not themselves executed when the softlist is
    ;evaluated! By putting a quotation mark in front of them,
    ;they are evaluated as words and passed verbatim to
    ;the replacetag primitive.
    
    ;there are other ways to do this that you can do in other
    ;Logo environments but they are more cumbersome.
    
    sleep 5
    ;sleep for 5 milliseconds
    
  ]
  ;do this forever, or until we press escape or the stop button
  
END