Category Logo Coding Tutorials

Project Idea: How to Code a Hangman-style game in Logo

You can open this project in the web-based IDE environment by following this link: https://turtlespaces.org/weblogo/?pub=122

Due to Logo’s enhanced string-handling capabilities and built-in dictionary, making Hangman-style games in turtleSpaces is easy-peasy!

The mechanics of a Hangman game are simple: the computer chooses a word, and then the player chooses letters. If the letters they choose are in the chosen word, then those letters are revealed. If they aren’t in the chosen word, the ‘hanged man’ is built — although these days we probably shouldn’t do that due to nasty historical context, and choose something else.

In our example, we’ve decided to reconstitute the Egyptian god Konshu (aka Khonsu) as a shameless reference to the Marvel TV show Moon Knight. Once Konshu is fully-reconstituted then he will block out the sun forever — we don’t want that! Another potential conceit could be a space portal being built by aliens which once complete will deliver an invading armada.

Whichever way you choose to present it, the mechanic is the same — once you run out of ‘misses’ the game is over.

  • Computer chooses a word
  • The number of ‘misses’ is specified
  • We prompt the user for a selection
  • We check to see if the letter is valid (hasn’t been picked before)
  • We search through the target word looking for the letter
  • If we find it, we reveal it by placing it into the ‘solution’
  • We show the player if they’ve uncovered any letters or not
  • If not, we add more on to Konshu (or the space portal, or what-have-you)
  • We repeat until the target word is entirely revealed, or Konshu is built

Here are the ways in which Logo makes building this program simple:

  • We can use the pick and english primitives to choose a word from the built-in dictionary
  • We can use forever to repeat our game loop… forever!
  • We can use question and answer to prompt for and receive input from the user
  • We can queue the user’s selection into the list that logs the user’s choices
  • We can use the boolean containsp to determine if the user’s choice has already been chosen, by passing it the user’s current choice and the list containing the users previous choices
  • We can use count to ensure the user only provides a single letter as input
  • We can use dountil to repeat the question until we get a valid answer
  • We can use foreach to cycle through each letter of the selected word and check for matches.
  • We can use a combination of setitem and loopcount to set the appropriate space in the ‘solution’ container to the guess, should it match. If we initially set the solution container to be a series of dashes, we can then check for dashes using containsp later, to see if we’ve completed the solution
  • We can call another procedure to build our ‘hangman’, passing it the number of tries remaining, so we know which part of the hangman to build. We can use switch and case to build the relevant part of our hangman
  • We can use decrement to decrease a counter by one, and use if to check to see if we are out of tries.

Because we have all of these tools at our disposal in Logo, the game logic does not occupy very many lines of code, and is nearly english-readable:

TO game
 reset hideturtle
  
  (print |You must determine the magic word required to 
   banish Konshu back to the netherrealm.|)
  (print |If Konshu is able to reconstitute himself in 
   this realm and awaken, he will banish the sun!|)
  ;instructions (in brackets for display purposes)
  
  make "attempts 10
  ;this could be made more, or less
  
  make "target pick english 6
  ;similarly, we could pick a different target word length
  
  make "solve "------
  ;this container reflects correct player choices
  ;if we change the word length, we will need to change 
  ;the number of dashes
  
  make "picks []
  ;this holds the players choices so we can discount 
  ;duplicate picks
  
  forever [
    dountil (and
      not containsp answer :picks
      1 = count answer
    ) [question |Pick letter:|]
    ;repeatedly ask which letter until the player enters only
    ;one letter, and it has not already been picked
    
    queue answer "picks
    ;add the selection to the :picks list
    
    make "hit false
    ;set the :hit boolean to false
    
    foreach "i :target [
      ;for each letter in the :target word:
      
      if :i = answer [
        ;if the letter matches the users choice
        
        setitem loopcount "solve answer
        ;update the appropriate character in the :solve
        ;container to reveal the matching letter
        
        make "hit true
        ;set the :hit boolean to true, so we don't
        ;advance the building of Konshu
      ]
    ]
    
    show :solve
    ;show the current state of the :solve container
    
    if not :hit [
      ;if our selection wasn't in the answer:
      
      dec "attempts
      ;decrease the :attempts container by one
      
      konshu :attempts
      ;execute the konshu procedure, passing the
      ;number of remaining attempts to it
      
      (print |Attempts remaining:| :attempts)
      ;placing round brackets around a statement allows more
      ;parameters to be passed to it than its default
      
    ]
    
    if not containsp "- :solve [
      ;if there are no dashes left in the :solve container:
      
      print |You have banished Konshu!|
      finish
    ]
    
    if :attempts = 0 [
      ;if we've no attempts left:
      
      (print |The word was:| :target)
      print |Darkness descends.... forever!|

      finish
    ]
  ]
END

See? That was pretty easy — it only took an hour to write that code! Then comes the building of our ‘hangman’ model:

TO konshu :attempts
  
  ;this procedure builds the relevant part of the Konshu
  ;model.
  
  switch "attempts
  ;switch takes a literal container name
  ;and then subsequent case statements execute
  ;based on the contents of that container:
  
  case 9 [
  ;this is what we build if there are 9 tries remaining
  ]
  case 8 [
  ;...
  ]
  ;etc...
END

You simply put the code to create each stage of construction inside each case block. This could make a great project to tie language / string manipulation to game coding, with a bit of 3D artistry (putting the A in STEAM) in the mix.

Logo is such a powerful, easy-to-use language it can be hard to decide what makes the best introduction for your students. But you know them best, and we hope that one of our examples will work for them, and you!

Thanks for checking out this project!

Logo Game Design Introduction: Downhill Skiing

Downhill Skiing exercises Logo’s list handling capabilities and leverages turtleSpaces 3D functionality to create an engaging yet simple game.

Once loaded, click the flag icon to start.

Click here to open this project in the web IDE.

Herein lies a wonderful introduction to game development in turtleSpaces. The game mechanic behind downhill is simple: the player travels down the ski slope, avoiding randomly-positioned trees while shifting left and right to end in the target zone.

The core game mechanic is similar to simple early text-based ‘action’ games written in languages such as BASIC, where the player needs to avoid asteroids in an asteroid field, for example. It is straightforward and easy to understand.

As such, this program would make an excellent project for students moving from Scratch to text-based coding. It provides an introduction to the turtle, programmatically-generated lists, 3D model creation, user input and various sound primitives, without using any sophisticated math or complex language primitives.

The game logic is as follows:

  • Create initial graphic elements (stars, ground, slope)
  • Create the ‘landing zone’ and store its location in a container (variable)
  • Move the turtle across the ‘slope’ to each spot on a 39 x 40 grid
  • Decide randomly to create a tree or not, create that tree and log that decision in a list
  • Each row has a separate list (trees0 trees1 etc), made up of 39 values (one for each column)
  • Once finished, place the turtle randomly at the top, in a location free of trees
  • Loop through each row of trees (and their respective list containers) in descending order
  • Check the relevant list to see if the player is occupying the same location as a tree, if so crash
  • Check to see if the player has pressed a key, and act accordingly, moving the player
  • Pause between rows but for less time each iteration, so that the player speeds up as they descend
  • When we get to the ‘bottom’, check to see if we’re in the target zone
  • If we’re not in the target zone, retry with the same level
  • If we are in the target zone, create a new level increasing the odds of trees

As you can see, developing a lesson plan is easily achievable. Let’s explore each area of the code in depth. This tutorial assumes some familiarity with text-based coding on the part of the reader, and sometimes speaks in general terms. The fully-commented source code for this game is available at the end of this article, and inside the turtleSpaces environment itself as a published project.

In the beginning…

When the procedure starts, the first thing we need to do is reset the workspace, to ensure we’re working with a clean slate. Next, we need to provide the user with some instructions!

For this, we’ll use the simple text command print. We can change the color of the text using settextforeground. Note that for every set command there is a corresponding function to return the parameter’s status, such as textforeground. settextforeground takes a number as a parameter, which represents a color (type showcolors in the console to see a list of colors and their numeric values). However, turtleSpaces has a number of color primitives (red, orange, yellow etc.) that return their respective numeric values, so that you can say, for example, setttextforeground yellow (which works because yellow is a function that returns the number 13.)

The print command itself takes either a word (a single word preceded with a quote “) a list (a series of words wrapped in square brackets []) or a ‘long word’ (a series of words wrapped in pipes ||). You can use either a list or a long word to display a sentence, eg print [Welcome to turtleSpaces!] or print |This also works.| The advantage to using a list is that you can cherry-pick one word from it easily, eg print first [Welcome to turtleSpaces!], however it can be more complicated when pairing text with numeric values, for example, when using a long word would be preferable.

Note that print “Welcome to turtleSpaces” will throw an error, because the parser will attempt to execute to and turtleSpaces” as commands.

We can use cursordown to put a blank line between sections of text. We can also say text as well, to make things more interesting!

Next, we need to create some default containers (variables). We can create these using the make primitive, which is the traditional method in Logo (eg make “container 5), although turtleSpaces also supports put x in “container and “container := x (note the colon before the equals). A colon before a word in Logo returns the value stored in the container of that name, eg :container. But make needs the container name specified with a quote ” — this is because if you used a colon, make would create a container with the name of the value stored inside that container!

We need to set the initial :level (0). We’re also going to create a container containing the number of available ‘retries’ (we will give the player limited opportunities to generate a new level if the level provided to them is impossible.)

make “level 0 make “retry 3

Notice that we can ‘stack’ multiple commands on the same line! This can sometimes be a blessing, sometimes a curse, so use your discretion when stacking commands.

Finally, we’re going to declare a label, which can be returned to using the go primitive:

label “start

so that we can return here at the start of a new level.

My God, It’s Full of Stars!

It’s time to start building the graphical environment in which the game will take place. First, let’s create a starfield. This is a fairly simple algorithm:

  • set a ‘static’ z (depth) position
  • set a random x and y position
  • set a random ‘fill’ shade and color
  • set a random fill opacity
  • create a spot of varying size
  • do this all 100 times

In the code we use the setxy and setz primitives to accomplish the first two items, and the random function to generate the random values we need to give to each. There’s some turtleSpaces-specific primitives such as randomfillshade and randomfillcolor (the color used to create shapes is the ‘fill’ color, as opposed to the color used to create lines which is the ‘pen’ color.)

We wrap all of this in a repeat loop, which repeats the contents of the list provided to it the given number of times, for example repeat 20 [print repcount] which counts from 1 to 20.

We actually need to create two starfields, one behind the hill, and one in front of it (so that we see stars regardless of which direction we’re facing, either looking at the hill, or from it while we’re playing. We can make the single loop do double-duty by using the oddp primitive which returns true if the value passed to it is odd. As you saw with the above example, repcount returns the current repeat loop iteration, and so if we pass repcount to oddp, then oddp will be true on odd iterations of the repeat loop.

We can then pass oddp to if, which will then execute the first list of instruction if oddp is true, and the second list of instructions if it is false. In this way, we set the z position of the turtle in an alternating fashion.

Note that the back side of a spot is not ‘lit’ and so we need to enable twosided mode before we create the stars to ensure both sides of the spot are lit, since we’re going to be looking at them from both directions.

Making Ground

Next, we’re going to create the ground. This is another spot, like the stars, just a very big one! We use setxy and setz to position the turtle in the lower-centre of the screen, and then tilt it up 90 degrees (so that we see the ‘dark’ back side of the spot), setting random fill and shade colors before making the spot. The value passed to spot signifies its radius, and is given as ‘turtle units’, the same unit of measure used by all shapes and lines.

To choose the fill color we use the pick primitive, which randomly selects an item from a list, eg print pick [1 3 5 7 9]

The landing area is where our skiball will end up assuming the player avoids all of the trees. This is created using the quad primitive, which creates a filled rectangle. A container is created containing a random value signifying the left (from this perspective) side of the target area. It’s width is dependent on the level, but to begin with, it could just be arbitrary (eg 100 ‘turtle units’ wide). We will use the value in this container later, to determine if the player is inside the target zone.

There are, then, three quads, the left side before the target zone, the target zone itself, and the right side after the target zone. We color the target zone dark blue to distinguish it from the rest of the landing area.

Finally, we create the ski hill itself. Its angle does not matter that much, as from now forward the turtle will operate on the plane of the hill. In this example we’ve used the gradient mode to create a gradient fill on the quad that makes up the ski hill. In gradient mode, the pen and fill colors are both used to create the gradient, the pen color being the ‘near’ color and the fill color being the ‘far’ color.

It’s time to populate the hill with trees.

Seeing the forest for the trees

When this game was first developed, the ‘trees’ were simple icospheres. In creating a lesson plan, you can take two approaches here:

  • Create and validate the core game logic first, using a placeholder such as a sphere
  • Create the trees first, and then develop the game logic later

This is really a choice between whether we have vegetables or dessert first, but here we’ll start with dessert.

The trees are made up of firstly a cylinder (for the trunk), then a series of cutcones and then finally an icosphere on the top. We select the color of each trunk and tree using the pick primitive, and select a random fillshade for each layer of the tree.

We need to do a bit of turtle acrobatics to ensure the trees are upright relative to the ground, and not upright relative to the slope! Shapes are created beneath the turtle, and so as a result the turtle needs to be ‘flipped over’ and the trees trees created successively from the trunk ‘downward’ to the icosphere on the top of the tree.

From a default startup state, type cylinder 20 50 20 into the console, then click and drag on the view area to rotate the camera:

As you can see, the cylinder is created beneath the turtle. To create additional shapes at the opposite end of the cylinder, we will need to lower the turtle the length of the cylinder.

cutcones are similarly created under the turtle. However, icospheres are created around the turtle. See the shape guides available on this website for more information about shape orientations relative to the turtle.

And so we sweep left to right, working up the slope, deciding at each ‘stop’ whether or not to build a tree, and then noting in the list for the current row if we have created a tree at the current column.

Here comes the vegetables. At the start of each row we create a list:

make word “trees repcount []

word combines two inputs into a single output. We’re using it here to create a new list container [] based on the current repcount, prefixing it with the word “trees, creating for example :trees11

As we sweep across each row, we use the queue primitive to add either a 0 (no tree) or a 1 (tree) to the end of the list:

queue 1 word “trees repabove 1

repabove returns the iteration of the parent repeat loop, in this case the row repeat loop. Using repcount here would return the current column, as we are iterating through the row.

We will use these lists later to cross-reference with the turtle’s position and reveal if a tree is present at that location.

Introducing the Player

Once we’ve created all the trees, we could spend a bunch of time creating an actual skier model, but I like just using the amigaball, which creates a checkered ball based on the current fill and pen colors. We use setmodel to make it the turtle model. Later on we can use setpremodel to make it ‘roll’ down the hill, which looks pretty cool! Note that we will need to elevate the turtle the radius of the amigaball to ensure it’s ‘sitting’ on the slope.

Skiing the Slopes

So basically now we’re ready to position the turtle at the top of the slope. We pick a random location and then check :trees40 and :trees39 to ensure there isn’t a tree at the random location, or below it, respectively. We do this by using a container called :pos which contains the column number occupied by the player.

Alright, so now we enter the main game loop, which is a repeat loop based on the number of rows (40):

  • Check for keypresses and action them as appropriate
  • Move the ball down one row
  • Check for collisions and act accordingly

We use if to check keyp which returns true if there are any keypresses waiting in the keyboard buffer. If there are, we use peekchar to ‘peek’ at what the first keypress in the buffer is. If the key is j or k we check our :pos and if valid we then move the turtle player and increment or decrement the :pos container as appropriate.

We can also check for a press of the r key and then initiate a ‘retry’ but that is something that we can add in later. We don’t need to deal with it now.

Once we’ve dealt with the keypress, we can remove it and any superfluous keypresses from the key buffer using clearchar

The second thing we do in the loop is move the turtle forward 10 turtle units (one row). This is pretty straightforward.

Third, we check for a collision:

if 1 = item :pos thing word “trees 40 – repcount [do this stuff…]

Whew! That’s a mouthful, isn’t it? The first thing you’ll notice is the meat of our sandwich is on the right — other programming languages would tend to have the sides of the equals reversed, with the complicated bit first followed by the = 1. But Logo ‘parses’ right to left, which means that if we did that, the = comparator would only check 1 against repcount, ignoring all the rest of it.

We can solve that problem by putting round brackets () around the entire expression (the meat) but that adds extra clutter we don’t need if we just put the value first.

  • item returns the nth list item. So print item 3 [1 2 3] will return 3, for example. item :pos returns the list item based on the player’s current position.
  • thing returns the value stored in the container of the given name. It’s the longhand of :container. But it’s useful here because unlike using the colon method, we can construct the name of the thing to be retrieved, as we did with make earlier.
  • word acts similarly here to when we created the lists, with the exception of subtracting repcount from 40 in order to get the current row list in descending order.
  • And so, if the value returned from all of that is 1, we’ve hit a tree! Do the stuff in the list.

Ouch! Well, when all else fails, try, try again… we can use a label go pairing to send the player back up to the top, logically speaking. And we can wrap a tag around the player’s progress down the hill so we can erase it, sending the player turtle back to where it started, turtle-y speaking.

If we make it to the bottom of the hill we need to check to see if we’re in the target zone:

if and xpos > -200 + :zone xpos < -100 + :zone – 10 * :level [do this stuff…]

xpos holds the turtle’s current x position. We can use it along with the value stored in the :zone container to decide if we’re in the zone. This statement assumes we’ve factored the :level into the width of the :zone.

If we’re in the zone, congratulations! Then (if we’re not on the last level) we increase the level and send the player off to try again. If we’re not in the zone, too bad, and we send the player off to try the level again.

Of course, the version available to play has a few more bells and whistles, you can read all about them in the documented source code below.

Note: This was created as a monolith procedure, but you could break bits of it out into separate procedures in order to demonstrate Logo’s abilities in that area. For example, the tree model creation could be its own procedure, as could the contents of the main game loop, the crash logic, the level completion logic and so forth.

TO downhill
  ;myrtle builds a ski slope and then rolls down
  ;it in the guise of a rolling amiga ball.
  
  ;this is a wonderful introduction to game
  ;development in turtlespaces. The game mechanic
  ;is quite simple:
  
  ;the player needs to travel down the ski slope
  ;while avoiding the randomly-placed trees
  ;and finish in the highlighted zone.
  
  ;most of this procedure is window-dressing.
  ;at its core it simply fills a number of lists
  ;with 0s and 1s, and then checks these against
  ;indices inferred by the player's position, eg
  ;the 'row' being the current iteration of the
  ;player's progress down the ski hill, and the
  ;column being the player's horizontal location
  ;on that hill. The row matches against the list
  ;generated with the row number, eg :trees 10
  ;while the column (:pos) matches with the item
  ;number in that list, eg item :pos :trees10
  
  ;this game would be rather pedestrian with a
  ;simple text representation, but in turtlespaces
  ;we can 'jazz it up' and make it more engaging.
  
  ;Let's go!
  
  reset
  cam:pullout 10
  ;the slope doesn't quite all fit in the screen!
  ;so we'll pull out the camera turtle a little bit.
  ;'cam' is shorthand for whichever turtle is the current
  ;view turtle
  
  cleartext settextforeground red
  print |Welcome to Amigaball Backcountry Downhill Skiing!|
  ;bars indicate a 'long word' or a word that contains spaces
  ;while quotes indicate a single word and square brackets
  ;indicate a list. Curly braces indicate a list 'resolved'
  ;when the list is processed during execution. (Don't worry
  ;if you don't know what that means just yet!)
  
  cursordown
  settextforeground yellow
  print |Press J and K to steer your skier (Amigaball)|
  print |Land in the Blue Zone. Don't hit the trees!|
  print |More trees each level. Try to make it to level 10.|
  print |Press R to get a different map (3 retries) Q to quit|
  say |Welcome. Don't hit the trees!|
  make "level 0
  ;'make' creates a container with the given name containing the given value
  
  make "retry 3
  ;although the odds of trees on lower levels is less,
  ;it is still possible to get a very dense level if you are unlucky!
  ;and so we'll give the player three opportunities to regenerate
  ;the level
  
  ;hint: if you open this in the web IDE you can hover
  ;over the primitive names with the mouse and a popup will
  ;appear with their syntaxes
  
  label "start
  ;we will return here at the start of each 'level'
  ;the program gets redirected to labels using 'go'
  
  ;create the 'mountain':
  clearscreen hideturtle penup
  ;you can 'stack' multiple directives on the same line
  
  twosided ; by default the back side of faces is not 'lit'
  
  ;make the background stars:
  repeat 200 [
    if oddp repcount [setz -600] [setz 600]
    setxy -800 + random 1600 -100 + random 600
    randomfillshade ; aka randfs
    randomfillcolor ; aka randfc
    setfillopacity 50 + random 50 ; aka setfo
    ;opacity is the transparency of the shape
    ;it takes a value from 0 to 100 (full)
    spot 2 + random 7
  ]
  
  setfo 100 ; aka setfillopacity
  
  setz 0
  setxy 0 -115
  
  ;create the 'ground spot':
  notwosided
  up 90 raise 1
  setfs -5 + random 15 ; aka setfillshade
  setfc pick [4 5 8 9] ; aka setfillcolor
  spot 1000
  slideleft 200 lower 1
  
  ;pick colors:
  setbg pick [2 6 7] ; aka setbackgroundcolor
  setbs -5 + random 15 ; aka setbackgroundshade
  setpc pick [5 6] ; aka setpencolor
  setps random 10 ; aka setpenshade
  setfc pick [10 15 7 14] ;aka setfillcolor
  setfs -10 + random 10
  gradient
  ;this causes many shapes to transition from the
  ;pen color to the fill color
  
  ;create the landing area:
  make "zone random 300
  ;pick the 'target zone' area
  
  ;containers that hold values are created using 'make'
  ;with the given name and value
  
  twosided
  quad :zone 400 ; creates a quad stretching to the start of the zone
  ;values in containers are retrieved by specifying the container name
  ;prefaced with a colon. You can also use the 'thing' function to retrieve
  ;the value of containers, eg 'quad thing "zone 400'
  slideright :zone ; aka sr
  make "pencol pencolor
  ;we need to store the pencolor and restore it later
  setpc blue ; several color names translate to their color number
  quad 100 - 10 * :level 400 ; create the zone based on the level
  sr 100 - 10 * :level
  setpc :pencol
  ;this is where we restore the pencolor
  quad 300 - :zone + 10 * :level 400 ; create the remainder of the landing area
  sl 100 + :zone - 10 * :level ; return to the start
  
  
  ;create the ski slope:
  notwosided
  down 120
  quad 400 400
  nogradient
  
  
  ;position the turtle for creating trees:
  fd 5 ; aka forward
  sr 5 ; aka slideright
  
  ;place the trees:
  repeat 40 [
    ;we'll have 40 rows of trees
    
    make word "trees repcount []
    ;our strategy here is to make a series of lists, each of which
    ;indicate the presence or absence of a tree in a particular
    ;location on a particular 'line' of the ski slope
    
    repeat 39 [
      ;there are 39 columns on each row of the ski slope
      sr 10
      
      if 1 = random 10 - :level [
        ;the higher the level the more likely a tree will be
        ;placed in a given spot
        
        noise (10 * repcount) + (200 * repabove 1) 1
        ;let's make a little noise based on position
        
        up 150
        ;most shapes are created under the turtle, so we need
        ;to flip it over
        
        make "size 0.5 + 0.1 * random 10
        ;pick a random size
        
        setfc pick [5 8 9]
        cylinder (1 + 0.1 * random 10) * :size 10 * :size 4 + random 10
        ;create the tree trunk. cylinder takes radius, depth and sides
        
        lo 10 * :size
        ;remember, these shapes are created under the turtle
        ;so we have to 'lower' to continue to build 'beneath' them
        
        setfc pick [4 5 6 7 12 14]
        
        ;create the body of the tree:
        repeat 5 [
          randfs
          cutcone (7 - repcount) * :size (5 - repcount) * :size 2 * :size (5 + random 10) * :size
          ;cutcone takes 4 parameters: the starting radius, ending radius, depth and sides
          lo 2 * :size
        ]
        
        ;put a little 'cherry' on the top of each tree:
        randfc ico 1 * :size
        
        ra 20 * :size
        dn 150
        ;return to the base of the tree and reorient back
        
        queue 1 word "trees repabove 1
        ;a 1 in the list indicates a tree
        ;queue adds it to the end of the list
      ]
      [
        ;this second list executes if the if condition is false:
        queue 0 word "trees repabove 1
        ;a 0 indicates no tree
      ]
    ]
    sl 390
    fd 10
    ;move ahead a row and return back to the starting column
    
  ]
  ;return to the start of the repeat loop and loop until finished
  
  ;we're done making trees! Let's place the player:
  
  bk 10
  ;pick a random placement:
  make "pos 5 + random 30
  sr 10 * :pos
  ;and check to make sure there's no tree there or directly in front:
  until (
    and 0 = item :pos :trees39
    0 = item :pos :trees40) [
    sr 10
    inc "pos
    if :pos > 39 [sl 400 make "pos 1
      ;if we hit the end of the row, circle back to the start
    ]
  ]
  rt 180
  ;turn to face down the hill
  ra 5
  ;elevate a little bit off the hill
  
  make "startpos :pos
  ;store the starting position to return back to it later
  
  label "attempt
  ;we will return here if the player's attempt fails
  
  setpremodel []
  ;setpremodel allows for the manipulation of the orientation
  ;and position of the turtle model.
  ;we use setpremodel later and need to clear it if
  ;the player needs another attempt
  
  begintag "route
  ;we're going to place the players 'route' into a 'tag'
  ;so that we can erase it if the player's attempt fails.
  ;this way we can keep the level we've generated and
  ;they can try again
  
  setview "myrtle
  ;view the scene from myrtle's perspective
  myrtle:setlight 1
  snappy:setlight 0
  ;switch the light to myrtle's position
  setviewpoint [0 10 -30]
  ;set the viewpoint offset from myrtle's position
  
  randpc ; aka randompencolor
  setpenwidth 4 ; make the turtle draw fat lines
  
  pendown ; let's draw the route the player takes!
  ;because why not? It'll look interesting later
  
  clearchar
  ;clears the keyboard buffer so the player doesn't
  ;end up with a bunch of buffered keypresses
  ;influencing movement
  
  setmodel [
    setfc red
    ico 5 ; aka icosphere
  ]
  ;sets the turtle 'model' to the given contents
  ;setmodel can also use tags
  
  ;Ready set go:
  showturtle
  type "Ready...
  toot 1000 30
  ;frequency and duration in 60ths of a second
  
  setmodel [
    setfc white
    ico 5
  ]
  type "Set...
  toot 1000 30
  print "Go!
  setmodel [
    setfc red
    setpc white
    amigaball 5 10 20
    ;creates a chequred ball
  ]
  toot 2000 60
  ;and we're off!
  
  repeat 39 [
    setpremodel {"dn repcount * 20}
    ;the premodel processor doesn't know what 'repcount' is, and so we need
    ;to use curly braces to make a 'soft list' that is evaluated when it is
    ;executed -- that is, repcount is resolved into its value, which is then
    ;passed to setpremodel
    
    fd 10
    ;move forward
    
    if keyp [
      ;has a key been pressed?
      
      if and :pos < 39 "j = lowercase peekchar [ ;if it's the j key and the position is less than 39: sl 10 ; aka slideleft inc "pos ; increment the 'pos' container value ] if and :pos > 0 "k = lowercase peekchar [
        ;similarly, if the k key and the position is greater than 0:
        sr 10 ; aka slideright
        dec "pos ; decrement the 'pos' container value
      ]
      
      if and :retry > 0 "r = lowercase peekchar [
        ;if the player presses the r key and has retries remaining:
        dec "retry
        (print :retry |retries remaining...|)
        setview "snappy
        snappy:setlight 1
        setlight 0
        ;switch the light and view back to snappy, the default view turtle
        endtag
        ;close the route tag
        go "start
        ;jump back to the start label
      ]
      
      if "q = lowercase peekchar [finish]
      ;I quit!
      
      clearchar
      ;clear the key buffer
    ]
    
    if 1 = item :pos thing word "trees 40 - repcount [
      ;are we where a tree is?
      
      ; - 'thing' returns the value of the given container.
      ;   we need to use it because we programatically
      ;   created a series of list containers earlier named
      ;   trees0 trees1 trees2 etc. and so we're using thing
      ;   and 'word' to create the appropriate container name
      ;   and then using 'item' to retrieve the value in the
      ;   relevant column (list index)
      
      ; - 'repcount' returns the current iteration of the
      ;   repeat loop. there is a similar function for other
      ;   loops called 'loopcount' and indeed loopcount will
      ;   also return the current repeat loop iteration. But
      ;   repcount is used by a number of different Logos and
      ;   it's a useful distinction
      
      ; so apparently we did:
      
      pr |Ouch! You Crashed! Try again.| ; aka print
      playsound "crash
      say pick [|too bad| |ouch| |that hurt| |dont hit the trees| |wipeout|]
      
      setview "snappy
      snappy:setlight 1
      setlight 0
      ;return the view and light back to 'snappy' the default view turtle
      
      repeat 2 [ht wait 30 st wait 30] ; aka hideturtle and showturtle
      ;wait causes the program to pause for the given number of 60ths of a second
      ;this is due to early computers operating at 60 frames per second
      ;you can also use 'sleep' which takes milliseconds
      
      make "pos :startpos
      ;return the value of the 'pos' container to its starting value
      endtag
      ;close the 'route' tag
      erasetag "route
      ;erase the 'route' tag. This returns the turtle to the position
      ;it was at at the start of the tag, as we have erased part of its
      ;'turtle track'
      go "attempt
      ;return to the 'attempt' label and continue execution from there
    ]
    ;indicates the end of instructions to execute if the player hits a tree
    
    noise repcount * 200 (40 - repcount) * 0.5
    wait (40 - repcount - :level) * 0.5
    ;make a little static noise based on progress down the hill
    ;and similarly wait a little bit based on the progress and the level.
    ;this makes us speed up as we descend the hill
    
  ]
  
  ;we've made it to the bottom!
  up 60
  ;reorient the turtle upward
  
  repeat 100 [
    setpremodel {"fd repcount "dn 10 * repcount}
    ;this causes the ball to move ahead of the camera
    setmodel [
      setfc 1 setpc 15
      setfillopacity 100 - repcount
      setpenopacity 100 - repcount
      amigaball 5 10 20
      ;this causes the ball to fade out
    ]
    setpenopacity 100 - repcount
    ;this causes the line to fade out
    forward 1 noise 8000 - 80 * repcount 1]
  ;do a little victory 'lap'
  
  endtag
  ;close the 'route' tag
  
  setfo 100 setpo 100 ; aka setfillopacity setpenopacity
  ;set the fill and pen opacity back to full
  
  setview "snappy
  snappy:setlight 1
  setlight 0
  ;set the view and light back to snappy, the default view turtle
  
  if and xpos > -200 + :zone xpos < -100 + :zone - 10 * :level [
    ;are we in the zone? if so:
    
    say pick [|great job| |good effort| |way to go|]
    playsound "cheers
    inc "level
    
    if :level < 10 [
      (print |Great Job! Try level| word 1 + :level "!)
      ;round brackets allow more than the default number of
      ;parameters to be 'passed' to a primitive, in this case
      ;print
      
      go "start
      ;return back to the 'start' label to create a new level
    ]
    
    print |You Win! Game Over.|
    say |you win|
    finish
    ;this finish isn't strictly necessary because we will fall out
    ;of execution at this point anyway...
  ] [
    ;we're not in the zone! Let's try this again:
    
    playsound "aw
    print |You missed the Blue Zone! Try Again.|
    say pick [|try again| |go again| |you can do it| |you'll get it this time|]
    wait 120
    make "pos :startpos
    erasetag "route
    go "attempt
  ]
  ;that's all folks!
END

 

Alpine Trees: an Introduction to Terrain

turtleSpaces new terrain functionality provides some great opportunities for simple coding examples with few lines of Logo code, in this case less than 30.

First, we need to introduce the setterrain primitive. setterrain creates ‘terrain’ or a contiguous object of varying elevation.

The above is an example of the object created by setterrain. The setterrain primitive takes a number of parameters, and is a good opportunity to explain the different types of parameters used in turtleSpaces to students.

The setterrain command used to create the above is:

setterrain [-5 -5 5 5] [0 50 10 40] random 1000 [fbm 2] “voxel

As you can see, the command has a number of different parameters.

The first parameter is a list containing the number of quadrants (blocks of ten elevation points, by default spaced one turtle-unit apart) in each direction from [0 0], the center of the turtleSpace, expressed as X Y pairs.

So, in this case, we are creating a grid of 25 quadrants in each of the four directions (forward/left, back/left forward/right back/right) surrounding [0 0], for a total of. 100 quadrants.

The second list contains four values: The elevation (or z value) of the ‘bottom’ of the terrain, the elevation of the highest points of the terrain mathematically generated, the ‘floor’ of the terrain which is the lowest level of the generated terrain rendered (so you can create lakes) and the ‘ceiling’ of the terrain which is the highest elevation of the generated terrain rendered (so you can create plateaus).

You can use the setterraincolors primitive to set the colors of various elevations, but this is beyond the scope of this article.

The third parameter is the ‘seed’ of the terrain generator. In the case of our example, we are using the random function to generate a random number to use as the seed, to create a different terrain each time we execute the command. But you can use an absolute number, eg 433, which will create the same terrain every time. This allows you to build additional objects on the terrain and ensure that terrain is the same every time you execute your program.

The fourth parameter, the algorithm used to generate the terrain, is a chameleon of sorts: it can either take a list or a word value depending on your intent. You can provide just a word, eg “fbm (the only algorithm type currently available) or a list, eg [fbm 2] — the second value in the list provides the ‘magnification’ of the algorithm. The larger the number, the less dense the terrain will be in terms of peaks and troughs. If you use a fraction eg 0.2 then the terrain will become more dense, to the point of looking more like a cityscape then terrain!

The final parameter is a word that specifies the style. There is currently only one style, voxel, hence the parameter in our example is “voxel

And so, as you can see, we have lists, numbers and words all as parameters passed to setterrain.

You can play with these values a bit and see the results. Be aware that you need to either call reset or clearterrain (not clearscreen, which does not affect terrain) before you execute the setterrain command again, because you cannot ‘overwrite’ existing terrain. But keep in mind you can create different blocks of terrain in different areas, so long as they do not overlap!

The voxels in the terrain by default are only one turtle-unit cubed, which are very small, You can increase the size of the voxels used in the terrain by using the setterrainresolution primitive to change that size, eg setterrainresolution 10. This will expand the size of the terrain by a factor of 10, so keep in mind your camera may end up ‘inside’ the terrain and you will need to use the scroll wheel on your mouse to pull the camera to the outside of it. Or you could use commands like:

cam:setorbitdistance 1000

cam:orbitdown 60

which pulls the camera turtle away from the center point and ‘orbits’ it down 60 degrees, cam: denoting that we are talking to the camera turtle, by default ‘snappy’ (if you use the setview primitive to change the camera turtle, then cam: commands that turtle instead.)

And so the start of our program looks like this:

TO trees
  reset hideturtle penup
  setterrainresolution 10
  
  setterrain [-5 -5 5 5] [0 50 10 40] random 1000 [fbm 2] "voxel
  ;create random terrain
  
  cam:setorbitdistance 1000
  cam:orbitdown 60
  ;position camera
END

and results in this:


And so we have our terrain, which is by default all white and suits our purposes. But now we need to put trees on it!

Our trees are made up of cylinders, of varying size and side counts, to provide variety. A random icosphere adorns the top of each tree.

First, we’ll create a repeat loop of 100, to create 100 trees:

repeat 100 [

]

The lines between the square brackets are where we’ll put the lines we want to repeat. Next we want to position the turtle in a random spot on the terrain, but we want the treeline to be at least 200 turtle-units on the positive Z axis.

We’re going to be using the elevation primitive, which returns the elevation at the given x and y co-ordinates expressed as the z co-ordinate of the elevation NOT the voxel count (remember, we’ve set the terrainresolution to 10, which means the actual z co-ordinate will be 10 times the voxel count).

We’re also going to be using dountil, which repeats an action until the condition provided to it is satisfied. And so we’re going to position the turtle in random spots within the area of the terrain and measure the elevation there, only continuing if the elevation is 200 or greater:

    dountil 200 < elevation pos [
      setxy -480 + random 960 -480 + random 960
    ]

Once we’ve found an appropriate spot, we’re going to set our elevation to the top of the terrain and flip the turtle upwards 180 degrees, because cylinders are created beneath the turtle:

    setz elevation pos up 180

We need to pick a color for the trunk (8 is brown and 9 is orange) and a random shade:

    setfillcolor pick [8 9]
    randomfillshade

Next we’ll create containers containing randomly-generated values determining the size of the tree and the number of sides it has:

    make "size 5 + random 20
    make "sides 3 + random 20

We’ll use the values contained in these containers in our cylinder commands to create our tree appropriately. Next we make the trunk based on these values:

    cylinder :size / 2 2.5 * :size :sides

cylinder takes three parameters: the radius of the cylinder, the depth of the cylinder and the number of sides. :size returns the value stored in the “size container, similarly with :sides.

Next we’ll lower the turtle to the end of the cylinder:

    lower 2.5 * :size

Now comes the creation of the tree’s ‘foliage’. First we need to select the tree’s color, then create 20 cylinders making up the foliage, then place a randomly-colored icosphere on the top.

    setfillcolor pick [4 12 14]

4, 12 and 14 are greenish colors.

repeat 20 [

]

We’re going to make 20 cylinders. Inside the repeat loop we place the following:

      setfillshade 15 - 1.5 * repcount
      cylinder 2.5 * :size - ((2.5 * :size) / 20) * repcount :size / 4 :sides
      lower :size / 4
      right 90 / :sides

repcount returns the current repeat ‘loop’ we’re in, and so first we set the fill shade (fill colors are used by shape primitives such as cylinder) based on the repeat loop. Second we create the cylinder, third we lower the turtle to the bottom of the cylinder, and finally we turn the turtle right based on the number of :sides the cylinder has, creating the spiral effect.

    randomfillshade randomfillcolor
    raise :size / 4 lower :size / 5
    icosphere :size / 5 up 180

Next, we set a random fillshade and fillcolor, raise the turtle back up so that we can lower it with a more appropriate value for our icosphere, then create the icosphere based on the :size value. Finally, we create the icosphere. Then we repeat all of this 100 times for 100 trees, ending up with something like this:

Here is the listing in full:

TO trees
  reset hideturtle penup
  setterrainresolution 10
  ;the terrain resolution affects all terrain! If you change it, terrain is regenerated.
  
  setterrain [-5 -5 5 5] [0 50 10 40] random 1000 [fbm 2] "voxel
  ;create random terrain
  
  cam:setorbitdistance 1000
  cam:orbitdown 60
  ;position camera
  
  ;create 100 trees:
  repeat 100 [
    dountil 200 < elevation pos [
      setxy -480 + random 960 -480 + random 960
    ]
    ;position the turtle someplace where the elevation is 200 or greater
    
    setz elevation pos up 180
    ;raise the turtle to that elevation and flip it on its back
    
    setfillcolor pick [8 9]
    randomfillshade
    ;select trunk color / shade
    
    make "size 5 + random 20
    make "sides 3 + random 20
    ;select random values
    
    cylinder :size / 2 2.5 * :size :sides
    ;make the trunk
    
    lower 2.5 * :size
    setfillcolor pick [4 12 14]
    ;select foliage colors
    
    repeat 20 [
      ;create 20 'rings'
      setfillshade 15 - 1.5 * repcount
      cylinder 2.5 * :size - ((2.5 * :size) / 20) * repcount :size / 4 :sides
      lower :size / 4
      right 90 / :sides
      ;this creates a spiral effect in the tree
    ]
    
    randomfillshade randomfillcolor
    raise :size / 4 lower :size / 5
    icosphere :size / 5 up 180
    ;make the 'ball' on top
  ]
END

Don’t forget that you can click and drag the mouse to move the camera around the model, and scroll in and out to zoom!

 

Tutorial: Fancy a game of darts?

Consider the below game of ‘pub darts’:

You can open it in our Javascript-based IDE by clicking here

If you hover over the various primitives in the editor, a popup will tell you what they do.

The game consists of three main parts: the dartboard, the darts and the game itself.

The dartboard is constructed primarily using the ringarc primitive and a number of repeat loops. It uses the oddp boolean to decide which color each segment of each ring should be, allowing us to match the colors of a standard dartboard.

TO board
  ;create the dart board
  
  hideturtle
  rt 9 setfc 1 polyspot 5 20
  ;bullseye
  
  lo 0.1 setfc 4 polyspot 10 20
  ;outer bullseye
  
  repeat 20 [if oddp repcount [setfc 13] [setfc 0] ringarc 25 10 20 1 rt 18]
  repeat 20 [if oddp repcount [setfc 4] [setfc 1] ringarc 5 35 20 1 rt 18]
  repeat 20 [if oddp repcount [setfc 13] [setfc 0] ringarc 20 40 20 1 rt 18]
  repeat 20 [if oddp repcount [setfc 4] [setfc 1] ringarc 5 60 20 1 rt 18]
  ;rings
  
  lo 0.1 setfc 0 cylinder 80 10 20 lt 9 penup setfc 15 rt 180
  ;backboard

The numbers around the outside of the dartboard are created using the inscribe and orbitleft primitives. We offset the numbers as required to center them on their relevant wedges. We also use the orbit / pullin / pullout functionality to create the wire frame overlaid on the dartboard.

 dropanchor pullout 65 ra 2
  foreach "i [20 1 18 4 13 6 10 15 2 17 3 19 7 16 8 11 14 9 12 5] [
    lt 90 if :i > 9 [bk 10] [bk 5] inscribe :i
    if :i > 9 [fd 10] [fd 5] rt 90 orbitleft 18
  ]
  ;numbers
  
  home setpw 1.2 setpc 5 setfc 5
  dropanchor tether pullout 5 orbitleft 9
  repeat 6 [
    pd
    repeat 20 [orbitright 18 ico 0.6]
    pu switch {repcount}
    case 1 [pullout 5] case 2 [pullout 25]
    case 3 [pullout 5] case 4 [pullout 20]
    case 5 [pullout 5]
  ]
  ;metal rings
  
  home pullout 10 orbitleft 9
  repeat 20 [pd pullout 55 pu pullin 55 orbitright 18]
  ;metal lines
  
  home
  
END

The darts are assembled using cylinders, cones, cutcones and poilyspots (for the fletchings).

TO dart :color
  ;create the dart models
  
  lower 50 setfillcolor 10 cone 1 4 20
  raise 20 cylinder 1 20 20
  ra 10 setfc 5 cutcone 3.5 2 10 20
  ra 20 setfc 10 cylinder 3 20 20
  ra 5 setfc 5 cylinder 3.5 5 20
  setfc item :color [11 6]
  ra 20 cutcone 2.5 3 20 20
  ra 5 setfc 10 cutcone 2 2.5 5 20
  setfc 5 ra 5 cutcone 1.5 2 5 20
  ra 10 setfc 0 cutcone 1 1.5 10 20
  ra 20 cutcone 1 1 20 20 up 180
  cone 1 15 20 rr 90
  
  repeat 3 [
    up 60 setfc 10 twosided
    polyspot 13 6 setfc :color
    ring 2 13 6
  ]
  ;fletchings
END

The game itself consists of a setup section and the main game loop. Inside the game loop we have the aiming section and the scoring section. Every three darts we switch between players. If one of the players score exceeds 301 then they are declared the winner and the game ends.

TO game
  ;world's smallest dart game
  
  reset
  clearfrozen
  cleartext
  print |Welcome to darts! Two players take turns until one scores more than 301.|
  cursordown
  print |Try to aim the dart with the mouse and press the mouse button to throw...|

The setup section creates the dart ‘room’, draws and ‘freezes’ the board (breaks it off into its own ‘turtle track’) and creates the dart models using the dart procedure. Default containers (variables) are created using surnames (container classes), one for each player. Surnames enable us to use the same code for each player without needing to use lists or tables.

  pu sl 300 bk 300 ra 600
  setfc pick [3 5 8 9] setfs 10
  setbs fillshade setbg fillcolor
  voxel -607 setfs 0 home
  ;create dart room
  
  board
  ;create board
  
  cam:pullout 180
  freeze "board
  
  newmodel "dart1 [dart 1]
  newmodel "dart2 [dart 2]
  ;create dart models
  
  setmodel "dart1
  setmodelscale 0.7
  setanchor [0 0 0]
  
  foreach "i [red blue] [setsurname :i
    rt 180 make "down forwarddir rt 180
    make "updown random 2 make "leftright random 2
    make "dart 0 make "frame 0 make "total 0
    make "oldx mousex make "oldy mousey
  ]
  setsurname "red
  ;define player containers
  
  print word surname |'s turn...|
  showturtle
  
  setposition [0 0 250]

The main game loop moves the dart according to the mouse position. The dart moves up and down and side to side to simulate shaky hands, in an exaggerated fashion to make the game more challenging. The player pushes the left mouse button to launch the dart. The camera follows the dart in a method dictated by a random number.

  forever [
    ;main game loop
    if or :oldx != mousex :oldy != mousey [
      make "oldx mousex make "oldy mousey
      setposition {-100 + 200 * mousex / 100 50 - 100 * mousey / 100 250}
      cam:sety myrtle:ypos
      ;move the dart with the mouse
    ]
    move 1 random 360

    if :updown = 0 [up 1 if and pitch < 350 pitch > 20 [make "updown 1]]
    if :updown = 1 [dn 1 if and pitch < 350 pitch > 20 [make "updown 0]]
    if :leftright = 0 [rl 0.5 if and roll < 350 roll > 10 [make "leftright 1]]
    if :leftright = 1 [rr 0.5 if and roll < 350 roll > 10 [make "leftright 0]]
    ;shaky hands, maybe try drinking herbal tea?

    wait 1
    ;delay

    if buttonp 0 [
      ;if button clicked:

      make "camdir random 4
      ;pick random camera action

      dountil (item 3 extrapolate position vectorsub updir [0 0 0] 35) < 0 [
        ;until the tip of the dart hits the board (basically):

        setpremodel {"lt loopcount}
        ;spin the dart model
        lo 1 dn 0.1 cam:pullin 1
        ;toward the board and down a little

        if :camdir != 3 [cam:sety myrtle:ypos]
        if :camdir = 1 [cam:orbitleft 0.3]
        if :camdir = 2 [cam:orbitright 0.3]
        if :camdir = 3 [cam:orbitup 0.3]
        ;camera actions
      ]

Once the dart reaches the wall / board, we calculate if the dart hit the board or the wall by checking its distance from the center of the dart board [0 0 0]. If it actually hit the board then we 'stamp' the model in place.

      norender
      ;stop rendering graphics while we deal with things

      ht lo 34
      make "vec vectors
      setvectors originvectors
      updatestate
      ;need to update the state for towards to work
      ;with rendering disabled

      make "dir towards [0 0]
      ;where did we land relative to the center?

      if and :dir >= 171 :dir < 189 [make "score 20]
      if and :dir >= 189 :dir < 207 [make "score 1]
      if and :dir >= 207 :dir < 225 [make "score 18]
      if and :dir >= 225 :dir < 243 [make "score 4]
      if and :dir >= 243 :dir < 261 [make "score 13]
      if and :dir >= 261 :dir < 279 [make "score 6]
      if and :dir >= 279 :dir < 297 [make "score 10]
      if and :dir >= 297 :dir < 315 [make "score 15]
      if and :dir >= 315 :dir < 333 [make "score 2]
      if and :dir >= 333 :dir < 351 [make "score 17]
      if or :dir >= 351 :dir < 9 [make "score 3]
      if and :dir >= 9 :dir < 27 [make "score 19]
      if and :dir >= 27 :dir < 45 [make "score 7]
      if and :dir >= 45 :dir < 63 [make "score 16]
      if and :dir >= 63 :dir < 81 [make "score 8]
      if and :dir >= 81 :dir < 99 [make "score 11]
      if and :dir >= 99 :dir < 117 [make "score 14]
      if and :dir >= 117 :dir < 135 [make "score 9]
      if and :dir >= 135 :dir < 153 [make "score 12]
      if and :dir >= 153 :dir < 171 [make "score 5]
      ;calculate dart position and assign score

      make "dist distance extrapolate position vectorsub updir [0 0 0] zpos [0 0 0]
      ;how far away from the center is the tip of the dart?
      if :dist <= 5 [make "score 50]
      ;bullseye!
      if and :dist > 5 :dist < 10 [make "score 25]
      ;half bullseye
      if and :dist >= 35 :dist <= 40 [make "score :score * 3]
      ;triple ring
      if and :dist >= 60 :dist <= 65 [make "score :score * 2]
      ;double ring
      if :dist > 65 [make "score 0]
      ;missed!


We use the towards primitive (an original Apple Logo II primitive!) to determine the number of degrees the landed dart (which is in reality pointed upwards toward the ceiling, the dart 'descending' towards the board along the Z axis) would have to turn to face [0 0]. This, combined with the distance from the center allows us to calculate the dart's score.

      (print |Dart| word :dart + 1 |:| :score) make "frame :frame + :score
      setvectors :vec
      if :dist < 79 [
        ;'stick' a dart to the board using stamp
        playsound "click2
        ra 34
        run premodel
        if surname = "red [stamp "dart1] [stamp "dart2]
        render wait 120
      ]

      else [
        playsound "knock
        pr "Missed! ra 34 st render
        repeat (ypos + 300) / 4 [drift 4 :down cam:lo 4 wait 1]
      ]
      ;drop the dart to the floor

      cam:cs
      ;reset the camera

      setheading 0 setpitch 0 setroll 0
      setvectors originvectors
      ;reset state

      make "updown random 2
      make "leftright random 2
      ;pick random starting wobble directions
      ;for the next dart

      inc "dart
      ;add one to :dart

      if :dart = 3 [
        ;if we've thrown three darts:

        (print |Frame Score: | :frame)
        make "total :total + :frame
        (print word surname |'s Total Score: | :total)
        make "dart 0 make "frame 0
        cam:pushturtle
        cam:run pick [[repeat 30 [orbitleft 1 wait 1]] [repeat 30 [orbitright 1 wait 1]]]
        wait 120 cs cam:popturtle

        if :total > 301 [(print surname "wins!) finish]
        ;the end

        if surname = "red [setsurname "blue setmodel "dart2]
        else [setsurname "red setmodel "dart1]
        print word surname |'s turn...|
        ;switch players
      ]

      setpremodel [] cam:pullout 180 setposition [0 0 250] showturtle
      ;position camera, reset 'spin', show the next dart

    ]
    ;end of throw

  ]
  ;end of main game loop

END

Logo code is like poetry! It's easy to read and describes what the computer is doing in fairly broad terms. This is why it has always been great as a first text-based coding language.

 

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

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…|]