News

One-a-Day: Space Tube Ride using TUBE and TUBEARC

Myrtle creates a 3D space tube ride for her to fly through…

Sometimes Myrtle needs to take a break and have a little fun! One of the things she can do is make a space tube and fly through it, using the TUBE and TUBEARC primitives:

TUBE makes a straight tube, and takes the parameters <radius> <depth> and <sides>. So, tube 20 10 10 makes a tube with a radius of 20 turtle units, a depth of 10 turtle units and 10 sides.

TUBEARC creates a curved tube, and takes the parameters <thickness> (the radius of the tube itself) <radius> (the radius of the arc itself) <sides> <totalsegments> and <arcsegments> – the last two are the total segments that would make up a complete torus, or donut, and the number of segments we actually want to create. So values of 20 and 10 will create a half-donut: tubearc 10 20 20 20 10

There are four procedures to our space tube program: createcourse, drawcourse, followcourse and spacetube.

createcourse generates the data that represents the tube’s course. It creates a course made up of 100 segments, represented by a word made up of a series of numbers between 0 and 4, where 0 represents a straight section of tube, and 1 to 4 represent four directions of turn. We use dountil to ensure we don’t repeat the same direction of turn more than once and end up looping back on ourself. Although, createcourse is not perfect and it is still possible for the course to cross over itself – but it’s a good thing tube walls aren’t solid!

drawcourse creates the graphical representation of the contents of the :course container we generated using createcourse. We use the twosided primitive to generate reflective “normals” on both sides of the shapes we generate from that point forward, and we use randfc to set a random ‘fill color’ or shape color for each segment as we create them.

We use foreach to step through each segment listed in the :course container, creating either a tube or a tubearc where appropriate.

While tubes are created directly under Myrtle, tubearcs are created around her, and so this means we need to do a bit of calisthenics to position the tubearcs where they need to go. Follow them through one command at a time to see how they work! We wait 2 / 60ths of a second between each segment creation so we can follow it.

followcourse causes Myrtle to fly through the created course, once again iterating through the :course container using foreach. fluid causes Myrtle to move forward smoothly, and to turn through the arcs we use a repeat. sleep is another form of wait, which takes milliseconds as a parameter instead of wait‘s 60ths of a second.

setpremodel allows us to ‘prepend’ commands to the turtle’s model, allowing us to make Myrtle look like she’s turning the appropriate way as she moves through the course.

Finally, spacetube puts it all together and is the procedure we execute to create the maze and fly through it. spacetube performs a reset, turns off the text layer, tells Snappy (the camera turtle) to pull away from Myrtle 1000 turtle units, tells Snappy to ‘follow’ Myrtle (turn to face her when she disappears from his view), then executes the createcourse procedure, the drawcourse procedure, sets the view (camera) turtle to Myrtle, turns Myrtle’s ‘light’ on (so we can see properly inside the tubes), turns Snappy’s light off, sets the camera ‘view point’ of Myrtle a bit up and behind her, and then executes followcourse.

TO spacetube
  reset
  fullscreen
  snappy:pullout 1000
  snappy:follow "myrtle
  createcourse
  drawcourse
  setview "myrtle
  setlight 1
  snappy:setlight 0
  setviewpoint [0 10 -30]
  followcourse
END

TO createcourse
  make "course empty
  make "last 5
  repeat 100 [
    
    dountil :last != :segment [make "segment random 5]
    
    make "course word :course :segment
    if :segment != 0 [make "last :segment]
  ]
END

TO drawcourse
  nofluid
  cs
  pu twosided
  foreach "segment :course [
    randfc
    if :segment = 0 [tube 50 200 20 lo 200]
    else [rt :segment * 90
      dn 90 rr 90 sl 100 tubearc 50 100 20 20 5 fd 100 rr 90 lt 180 + :segment * 90]
    wait 2
  ]
  home
END

TO followcourse
  dn 90
  fluid
  foreach "segment :course [
    if :segment = 0 [fd 200]
    if :segment = 1 [setpremodel [rt 22.5] repeat 90 [fd 1.75 rt 1 sleep 1] setpremodel []]
    if :segment = 2 [setpremodel [dn 22.5] repeat 90 [fd 1.75 dn 1 sleep 1] setpremodel []]
    if :segment = 3 [setpremodel [lt 22.5] repeat 90 [fd 1.75 lt 1 sleep 1] setpremodel []]
    if :segment = 4 [setpremodel [up 22.5] repeat 90 [fd 1.75 up 1 sleep 1] setpremodel []]
  ]
END

Release Notes: New SKEW* and TRAPE* shape primitives + SAVEPNG high-res export

We’ve added a number of new 3D shape primitives in the latest release:

skewfiso
skewiso
skewpyramid
skewpyramoid
skewquad
skewrect
skewtraperect
skewtrapevoxeloid
skewtrapezoid
skewvoxeloid
traperect
trapevoxeloid
trapezoid

These allow you to “skew” (or slant) the created shape and / or compress or expand one “end” of it (trape, or trapezoidal). This allows for a large increase in the number of possible shapes you can create in turtleSpaces.

Also, you can export an arbitrarily-sized render of the current scene using the new SAVEPNG primitive:

SAVEPNG [width height] “filename.png

Note that if you specify a really large size (> 10000 pixels per side) you can cause the application and even your operating system to crash! This is because the rasterisation needs to be done in memory.

The image is saved in the current “working directory”, usually a turtleSpace TSP folder inside of your user folder (inside of the turtleSpaces folder in your home directory).

We’ve also created a lot of short-hand primitives for shapes. You can find them listed in the shapes and graphics reference.

One-a-Day: Amiga Ball Bounce


The Commodore Amiga computer had a famous ‘bouncing ball’ demonstration program, a checkered bouncing ball. It was a bit different than this, but this is certainly reminiscent of it.

We love the Amiga demo so much that we created a special checkered ball primitive, AMIGABALL, and a second ‘oid’ primitive, AMIGABALLOID, which is used to create the ‘compressed’ ball.

Rather than turning the turtle into a ball (one method), this method ‘cleans’ the ‘turtle track’ after rendering each frame of the animation (which we wait for using the NEXTFRAME primitive).

The background voxel is actually an ‘inverted’ voxel, a voxel that is created using a negative size value. Due to the nature of OpenGL, only the insides of negative voxels are visible, which is why we can see inside it, and not see the face directly in front of us. Because we clean the ball each frame, we need to use a HATCHLING to create and hold the voxel in place. The hatchling just sleeps while the program runs.

The CAM: prefix directs the current view turtle (usually ‘Snappy’) to do things. You could also use SNAPPY: in this case but CAM: is shorter.

DOUNTIL PENCOLOR != FILLCOLOR [RANDFC] ensures that the ball has two different colors. The Amigaball(oid) uses both the pen and the fill colors in its construction, the only 3D primitive to do so.

To stop the ball from bouncing, press the Escape key.

TO bounce
  reset fs
  penup lower 120 slideright 120 fd 120
  
  hatch [randomfillcolor voxel -240 forever [wait 10]]
  ;create the cube, using a hatchling so it persists
  
  cam:run [oup 10 ra 5]
  
  home
  setspheroidaxis 1
  ;stretch the spheroid on the vertical, default is horizontal
  dn 90 rr 10
  randpc dountil pencolor != fillcolor [randfc]
  ;set random pen and fill colors
  
  forever [
    
    repeat 14 [
      amigaball 50 10 20
      setx xpos - 1 lo 4 lt 2
      nextframe clean
    ]
    ;move down
    
    repeat 11 [
      amigaballoid 50 10 20 1 - repcount / 20
      lo 2 lt 2 nextframe clean
    ]
    ;compress ball
    
    randpc dountil pencolor != fillcolor [randfc]
    ;change colors
    
    repeat 11 [
      amigaballoid 50 10 20 0.375 + repcount / 20
      ra 2 lt 2 nextframe clean
    ]
    ;expand ball
    
    repeat 28 [
      amigaball 50 10 20
      setx xpos + 1 ra 4 lt 2
      nextframe clean
    ]
    ;move up
    
    repeat 11 [
      amigaballoid 50 10 20 1 - repcount / 20
      ra 2 lt 2 nextframe clean
    ]
    ;compress ball
    
    randpc dountil pencolor != fillcolor [randfc]
    ;change colors
    
    repeat 11 [
      amigaballoid 50 10 20 0.375 + repcount / 20
      lo 2 lt 2 nextframe clean
    ]
    ;expand ball
    
    repeat 14 [
      amigaball 50 10 20 setx xpos - 1
      lo 4 lt 2 nextframe clean
    ]
    ;move back to center
  ]
END

 

 

One-a-Day: FRAG

The FRAG primitive creates a filled shape out of the current turtle position and her last two positions. For example:

FD 100 RT 90 FD 100 FRAG

will create a triangle.

While turtleSpaces has a variety of shape primitives, sometimes you need to create an arbitrary shape, and FRAG aids you in this.

Take this example, which draws a star:

TO star
  repeat 4 [
    forward 100
    right 170
    forward 86
    frag
    left 70
    forward 86
    right 170
    forward 100
    frag
    right 180
  ]

We can change the number of sides the star has by changing the number of repeats and fiddling with the values a bit:

TO star2
  repeat 5 [
    fd 100 rt 170
    fd 86 frag
    lt 88 fd 86
    rt 170 fd 100
    frag rt 180
  ]
END

First, let’s change our star procedures so they can take a :size parameter, like so:

TO star1 :size
  repeat 4 [
    fd 100 * :size
    rt 170
    fd 86 * :size
    frag
    lt 70
    fd 86 * :size
    rt 170
    fd 100 * :size
    frag
    rt 180
  ]
END

In this case, :size is a ratio, that is, 0.5 will make a star half the size, and 2 will make a star twice the size of the default.

You can change the color of the star using the SETFILLCOLOR primitive, or set a random fill color with RANDFC.

The following procedures create a sky full of stars:

TO star1 :size
  repeat 4 [
    fd 100 * :size
    rt 170
    fd 86 * :size
    frag
    lt 70
    fd 86 * :size
    rt 170
    fd 100 * :size
    frag
    rt 180
  ]
END

TO star2 :size
  repeat 5 [fd 100 * :size rt 170 fd 86 * :size frag lt 88 fd 86 * :size rt 170 fd 100 * :size frag rt 180]
END

TO star3 :size
  repeat 6 [fd 100 * :size rt 170 fd 86 * :size frag lt 100 fd 86 * :size rt 170 fd 100 * :size frag rt 180]
END

TO star4 :size
  repeat 7 [fd 100 * :size rt 170 fd 86 * :size frag lt 108.625 fd 86 * :size rt 170 fd 100 * :size frag rt 180]
END

TO star5 :size
  repeat 8 [fd 100 * :size rt 170 fd 86 * :size frag lt 115 fd 86 * :size rt 170 fd 100 * :size frag rt 180]
END

TO stars
  reset cam:setposition [0 0 0]
  cam:fixate [0 0 0]
  cam:setviewpoint [0 0 0]
  cam:newworker [forever [up 0.1 lt 0.1 rr 0.1 wait 1]]
  repeat 200 [
    pu home randori fd 400 + random 1000 up 90
    lt random 60 pd randpc randfc randfs randps
    make "size (10 + random 90) / 100
    run {pick [star1 star2 star3 star4 star5] :size}
  ]
  
END

Type STARS and press Enter to see the stars!

FRAG’s sister, SHARD creates a three-dimensional FRAG with depth beneath it. This depth is supplied as a parameter, in turtle-units, eg. SHARD 5. Try replacing FRAG with SHARD 5 in one of your star procedures and see what happens! (You’ll need to drag the camera around to see the sides of the star)

Myrtlebot: A turtleSpaces Twitter Bot

Don’t want to download the turtleSpaces client just yet? Well, you can take turtleSpaces for a bit of a spin using Myrtlebot, our turtleSpaces Twitter Bot! Myrtlebot executes whatever Logo code (not procedures yet) you tweet at her, and then returns an image of the results. In the future, we plan to allow for the recording of movies, but for now it’s just a still image. If your code runs for more than 60 seconds, it will be cut off at the 60 second mark.

Head on over to Twitter and check it out!

We’ve created a number of ‘short codes’ for various graphics primitives so that you can put more into a tweet. Here are the new ‘short codes’ (along with the typical logo rt, lt, fd, bk, up, dn etc.) See the documentation for a fuller description of each primitive:

AB = AMIGABALL
ABO = AMIGABALLOID
CAM = CURRENT CAMERA TURTLE
CB = CUBE
CBO = CUBOID
CC = CUTCONE
CCO = CUTCONOID
CCOS = CUTCONOIDSLICE
CCS = CUTCONESLICE
CF = CUTFUNNEL
CFO = CUTFUNNELOID
CFOS = CUTFUNNELOIDSLICE
CFS = CUTFUNNELSLICE
CIR = CIRCLE
CN = CONE
CNO = CONOID
COSL = CONOIDSLICE
CSL = CONESLICE
CSO = CUTSPHEROID
CSOS = CUTSPHEROIDSLICE
CSP = CUTSPHERE
CSS = CUTSPHERESLICE
CY = CYLINDER
CYA = CYLINDERARC
CYAS = CYLINDERARCSLICE
CYO = CYLINDROID
CYOA = CYLINDROIDARC
CYOAS = CYLINDROIDARCSLICE
CYOS = CYLINDROIDSLICE
CYS = CYLINDERSLICE
DCC = DUOCUTCONOID
DCFO = DUOCUTFUNNELOID
DCO = DUOCYLINDROID
DIVP = DIVISORP
DM = DOME
DMO = DOMOID
DOD = DODECAHEDRON
DODO = DODECAHEDROID
DTO = DUOTUBOID
ELL = ELLIPSE
FIX = FIXATE
FU = FUNNEL
FUO = FUNNELOID
FUOS = FUNNELOIDSLICE
FUS = FUNNELSLICE
HD = HEADING
ICD = ICOSPHEROID
IT = ITEM
NTWS = NOTWOSIDED
OCT = OCTAHEDRON
OCTO = OCTAHEDROID
PK = PICK
POPT = POPTURTLE
PRI = PRISM
PS = POLYSPOT
PT = PITCH
PUSHT = PUSHTURTLE
PY = PYRAMID
PYO = PYRAMOID
QD = QUAD
QUO = QUOTIENT
RBG = RANDBG
RBS = RANDBS
RCT = REPCOUNT
RD = RANDOM
RE = REMAINDER
RFC = RANDFC
RFS = RANDFS
RO = ROLL
RP = ROPE
RPA = REPABOVE
RPC = RANDPC
RPS = RANDPS
RPT = REPEAT
SBDS = SETBOUNDS
SBG = SETBG
SBS = SETBS
SFC = SETFC
SFS = SETFS
SHD = SETHEADING
SISI = SETICOSPHEREITERATIONS
SKF = SKEWFISO
SKI = SKEWISO
SKP = SKEWPYRAMID
SKPO = SKEWPYRAMOID
SKQ = SKEWQUAD
SKR = SKEWRECT
SKT = SKEWTRAPEZOID
SKTR = SKEWTRAPERECT
SKV = SKEWVOXELOID
SKVO = SKEWTRAPEVOXELOID
SMW = SETMARKERWIDTH
SP = SPOT
SPC = SETPC
SPD = SPHEROID
SPDS = SPHEROIDSLICE
SPH = SPHERE
SPHS = SPHERESLICE
SPI = SETPITCH
SPN = SETPOSITION
SPO = SETPOS
SPS = SETPS
SPW = SETPENWIDTH
SSA = SETSPHEROIDAXIS
SQ = SQUARE
SRO = SETROLL
SSPD = SETSPEED
STD = SETTYPEDEPTH
STDE = SETTYPEDELAY
STF = SETTYPEFONT
STFI = SETTYPEFILLED
STR = SETTYPESTRETCH
STS = SETTYPESIZE
TB = TUBE
TBA = TUBEARC
TBAS = TUBEARCLICE
TBO = TUBOID
TBOA = TUBOIDARC
TBOAS = TUBOIDARCSLICE
TBOS = TUBOIDSLICE
TBS = TUBESLICE
TH = TETRAHEDRON
THO = TETRAHEDROID
TR = TORUS
TRE = TRAPERECT
TRI = TRIANGLE
TRO = TOROID
TROS = TOROIDSLICE
TRS = TORUSSLICE
TVO = TRAPEVOXELOID
TWS = TWOSIDED
TZ = TRAPEZOID
VX = VOXEL
VXO = VOXELOID

Export STL format 3D models created in turtleSpaces 3D Logo using the SAVESTL primitive

turtleSpaces has a new primitive, SAVESTL, which can be used to export 3D models of the current turtleSpace.

savestl "modelname

creates a new folder in the current project folder (TSP) with the extension MDL (eg modelname.mdl), inside which contains STL format files, one for each color used in the space. These files can be used with other 3D applications (such as the pictured viewstl.com website), and can be 3D printed (although the sides of objects may need to be extruded using another application first, if the desire is for the objects to be hollow).

Another primitive, SETSTLLINETHICKNESS, is used to set the radius of the ropes used to represent lines in exported STL files. It takes a number.

setstllinethickness 10

Happy 3D printing!

 

Quick Play: Warped Logo Spirals

One of the best things about turtleSpaces is its capacity for discovery through play.

Today I was playing with spirals:

I started off by creating a simple spiral using the RARC primitive and a simple REPEAT loop. This loop uses REPCOUNT (the current repeat loop iteration) to define the radius of the arc, resulting in an ever-growing spiral.

I clicked and dragged on the window to rotate the camera around the spiral. But let’s make the spiral itself three-dimensional:

I’ve ‘warped’ the spiral by placing three rarcs inside of the repeat loop, along with a ROLLLEFT (rl) and a ROLLRIGHT (rr). With the twisting and turning, the turtle ends up roughly halfway around a somewhat distorted circle, which you can see if you type:

cs rarc 90 20 rr 90 rarc 90 20 rl 90 rarc 90 20

cs repeat 2 [rarc 90 20 rr 90 rarc 90 20 rl 90 rarc 90 20]

If we orbit the camera (by clicking and dragging) around our spiral, we can see just how warped it is!

What else can we do with it?

Whoa! What happened there? I added a dn (down) 10 to the end of the loop. Let’s take a look from another vantage point:

That’s pretty distorted. Let’s try a smaller value, say 2:

And what about 5?

What about down (dn) instead of up?

Okay, pretty neat. How about if at the end of each loop we also roll left (rl) a little?

Another angle:

Let’s play around with the values of down (dn) and rollleft (rl) a bit:

What if we do a right turn (rt) instead of a roll right (rr)?

And what if we add the roll back in?

Let’s play around with the values a bit:

All right, that’s enough from me! But you can play around with the different values and see what happens. That’s all part of the fun of turtleSpaces!

 

Make it Rain!

TO rain

  reset

  penup hideturtle
  fullscreen
  setbackgroundcolor blue setbackgroundshade 12
  make "count 0

  forever [

    inc "count
    setposition {-300 + random 600 300 -300 + random 600}
    make "size 0.25 + ((1 + random 20) / 20)
    
    begintag "teardrop
    setfillcolor pick [2 3 6 7 14]
    setfillshade -10 + random 20
    
    up 90
    cutsphere 10 * :size 20 20 1 13
    lower 4.5 * :size
    cone 8.9 * :size 18 * :size 20
    down 90
    ;raindrop
        
    endtag
    
    newmodel :count "teardrop
    clean
    
    hatch [
      setmodel :count
      showturtle
      make "speed (5 + random 10) / 10
      while ypos > -300 [
        back :speed rollright 0.25
      ]
    ]
    
    wait 5 + random 25
  ]

END

Control turtleSpaces from other applications with its API

You can communicate with turtleSpaces over a socket (telnet) connection to port 1967.

First you need to issue the API command inside of turtleSpaces to enable API connections, or use the -enable-api flag when starting up.

Then connect to turtleSpaces from an external application or terminal. You can issue instructions to affect turtles and the environment just like you would do inside the turtleSpaces application.

The API connection executes as its own worker. You can also redirect input and output to the API instance by using setread “api and setwrite “api – but these must be issued by the API worker itself to redirect to that worker (if you use another API connection, reading and writing will be redirected to that API instance, for example).

Using the API, you can use turtleSpaces as a visualiser for another application, and have it act as a light organ, or create charts and graphs.

 

‘Snake’ written in turtleSpaces Logo

TO snake
  
    
  ;setup:
  
  reset
  ;empties (erases) all containers (variables),
  ;returns the turtles to their default positions,
  ;restores their states to defaults
  
  release
  ;sets all the turtles, except for the default ones
  ;(myrtle, snappy, libby) 'free' - erasing them
  
  penup
  ;don't draw
  
  setbackgroundcolor lightblue
  
  hideturtle
  ;don't show the executing turtle (typically myrtle)
  
  stealth
  ;don't show up in output from the near primitive
  
  noaudiowait
  ;don't delay when playing sounds (the default)
  
  print |Use keys J and K to turn snake, eat apples!|
  ;print instructions to the screen
  
  ;create the 'ground'
  setfillcolor brown
  setpos [-185 -115]
  lower 5
  quad 185 * 2 115 * 2
  
  make "score 0
  make "length 8
  ;the default snake length, in segments
  
  make "queue empty
  ;this snake implementation uses a push / pull queue
  ;to keep track of the snake's turns
  
  repeat :length [push "fd "queue]
  ;all segments start moving forward
  
  newmodel "ball [setfillcolor 12 icosphere 5]
  newmodel "head [setfillcolor 4 icosphere 6]
  newmodel "apple [setfillcolor red icosphere 4]
  ;create turtle models, where ball is a non-head segment
  
  ;create the turtles that make up the starting snake:
  repeat :length [
    nameturtle repcount
    ;create a new turtle
    ;each segment has the number of its segment as its name
    
    control repcount {"wrap "penup "back repcount "* 10 "showturtle}
    ;initialize the new turtle and move it into position
    ;{} denotes a 'softlist', a list whose contents are evaluated
    ;at execution
    
    control repcount [
      setbounds [-185 -115 185 115]
    ]
    ;sets the bounds used for wrapping. [] denotes a 'hardlist', a
    ;list whose contents are fixed and not evaluated
    
    if repcount = 1 [control 1 [setmodel "head]]
    ;if this is the first segment, give it the 'head' model
    else [control repcount [setmodel "ball]]
    ;otherwise, give it the 'ball' (segment) model
  ]
  
  ;set up the 'apple' turtle:
  nameturtle "apple
  control "apple [
    penup setmodel "apple showturtle
    
    dountil (count near position 5) = 1 [
      setpos {-180 + (10 * random 36) -100 + (10 * random 20)}
    ]
  ]
  ;set a random position until such times as the apple is not near
  ;any snake segments (turtles)
  
  ;leading parameters used in comparison operators (=, >, < etc)
  ;must be enclosed in {}'s if more than a single value or primitive
  ;because the parser evaluates right to left
  
  ;the main loop:
  
  forever [
    norender
    ;don't render while we shuffle the snake forward
    
    for {"i 1 :length} [
      make "move item :i :queue
      ;set the move variable to the :i item in the :queue
      
      switch "move
      case "fd [ask :i [forward 10]]
      case "rt [ask :i [right 90 forward 10]]
      case "lt [ask :i [left 90 forward 10]]
      ;switch / case statements execute instructions based on the
      ;contents of the container (variable) provided to switch
      
      ;ask creates a new worker that executes the commands using
      ;the specified turtle (the turtle named by :i)
      ;so, in this for loop we are iterating through the :queue,
      ;moving the turtle segments based on the item present in
      ;each space in the queue (fd, rt or lt)
    ]
    while workers != [] [sleep 1]
    ;sleep execution until the ask workers finish moving their
    ;turtles (the snake). This way, when we enable the render,
    ;we won't catch the snake mid-move.
    
    render
    ;enable rendering again
    
    wait 40 - :score
    ;wait units are 60ths of a second. The higher the score, the
    ;shorter the wait will be
    
    ;user control:
    if keyp [
      ;did the user press a key? If so, do this stuff:
      
      make "input readchar
      ;take the output from the readchar primitive and place it
      ;into the input container. keyp indicates if there is a character
      ;already in the keyboard buffer. If readchar is called when there
      ;is no character in the buffer, it waits.
      
      switch "input
      case "j [push "lt "queue]
      ;if the user pressed the j key, push lt to the front of the queue
      case "k [push "rt "queue]
      ;if the user pressed the k key, push rt to the front of the queue
      otherwise [push "fd "queue]
      ;if no case has been satisfied since switch (the user pressed a key
      ;other than j or k), push fd to the front of the queue
    ]
    else [push "fd "queue]
    ;similarly, if the user hasn't pressed any key, push fd to the front
    ;of the queue
    
    ignore dequeue "queue
    ;ignore (drop) the last value off the end of the :queue
    ;dequeue would ordinarily return this value, ignore sends it into
    ;the ether
    
    ;is there something occupying the same space as the head of the snake?
    if (count (near 1:position 5)) > 1 [
      ;near returns the names of the turtles within the specified distance (5)
      ;count returns the count of the number of turtles returned by near
      ;if there is more than one (the head itself), we will execute the following:
      
      if memberp "apple near 1:position 5 [
        ;is it an apple?
        
        playsound "chomp
        inc "score
        ;increment the value in the score container by one
        
        (print |Yum! Score:| :score)
        ;to pass more than the default number of parameters to a primitive, wrap
        ;it in parentheses (round brackets)
        
        dountil (count near apple:position 5) = 1 [
          control "apple [setpos {-180 + (10 * random 36) -100 + (10 * random 20)}]
        ]
        ;place a new apple
        
        repeat 2 + int (:score / 10) [
          
          queue "pa "queue
          ;a pa or 'pass' will cause these new segments not to move on the next
          ;iteration, until movement commands are 'shuffled' on to their places
          ;in the queue
          
          inc "length
          ;increment the value of the length container by one
          
          nameturtle :length
          ;create a new turtle with the name contained in :length (the new length)
          
          (control :length {"hideturtle "setmodel ""ball "wrap "penup "setpos
          "query :length - 1 leftbracket "position rightbracket "setvectors
          "query :length - 1 leftbracket "vectors rightbracket})
          ;set up the new turtle and make sure it's positioned next to the
          ;previously last turtle, and is facing in the same direction
          
          control :length [setbounds [-185 -115 185 115] showturtle]
          ;set the bounds of the new turtle and show it
          
          wait 10
        ]
        ;add new segment to the snake
        
      ]
      
      ;and if it's not an apple?
      else [
        print "crash! playsound "crash finish
      ]
      ;game over, man! game over!
    ]
  ]
END