rem cursor's current position
global cx = 320
global cy = 240
global angle = 180
global length = 10
global isPenDown = 0
 
dim args$(2)
 
restore hello
read z$
while z$ <> "end"
   parseCommand(z$)
   gosub turtle
   read z$
endwhile
 
 
 
 
 
 
wait key
 
 
turtle:
   select args$(1)
      case "cls"     : cls                      : endcase
      case "reset"   : resetVars()              : endcase
      case "penup"   : isPenDown = 0            : endcase
      case "pendown" : isPenDown = 1            : endcase
      case "forward" : forward(val(args$(2)))   : endcase
      case "color"   : setColor(args$(2))       : endcase
      case "left"    : left(args$(2))           : endcase
      case "right"   : right(args$(2))          : endcase
   endselect
RETURN
 
 
 
REM
function parseCommand(cmd$)
   arg = 0
   s = 0
   for i = 1 to len(cmd$)
      if mid$(cmd$,i) = " "
         inc arg
         args$(arg) = left$(cmd$,i-1)
         args$(arg) = right$(args$(arg),len(args$(arg))-s)
         s = i
      endif
   next i
   if s <> len(cmd$)
      inc arg
      args$(arg) = right$(cmd$,len(cmd$)-s)
   endif
   for i = arg+1 to array count(args$())
      args$(i) = ""
   next
endfunction
 
 
 
REM
function right(degrees$)
   angle = wrapvalue(angle-val(degrees$))
endfunction
 
 
 
REM
function left(degrees$)
   angle = wrapvalue(angle+val(degrees$))
endfunction
 
 
 
REM
function forward(flength)
   if flength = 0 then flength = length
   x = sin(angle)*flength
   y = cos(angle)*flength
   if isPenDown = 1 then line cx,cy,cx+x,cy+y
   cx = cx+x
   cy = cy+y
endfunction
 
 
 
REM
function setColor(c$)
   local color as dword
   select c$
      case "random"  : color = rgb(rnd(255),rnd(255),rnd(255)) : endcase
      case "yellow"  : color = rgb(255,255,0)   : endcase
      case "blue"    : color = rgb(0,0,255)     : endcase
      case "red"     : color = rgb(255,0,0)     : endcase
      case "green"   : color = rgb(0,255,0)     : endcase
      case "white"   : color = rgb(255,255,255) : endcase
      case "purple"  : color = rgb(255,0,255)   : endcase
      case "orange"  : color = rgb(255,128,0)   : endcase
   endselect
   ink color, 0
endfunction
 
 
 
REM
function resetVars()
   cx = 120
   cy = 240
   angle = 180
   length = 10
   isPenDown = 0
endfunction
 
 
 
hello:
data "reset","pendown","color yellow","forward 100","right 180","forward 50","left 90"
data "forward 50","left 90","forward 50","right 180","forward 100","left 90"
data "penup","forward 25","pendown","forward 50","left 180","forward 50"
data "right 90","forward 50","right 90","forward 50","left 180","forward 50"
data "right 90","forward 50","right 90","forward 50","penup","forward 25"
data "right 90","pendown","forward 100","left 90","forward 50","penup"
data "forward 25","pendown","forward 50","left 180","forward 50","right 90"
data "forward 100","penup","right 90","forward 75","pendown","forward 50"
data "right 90","forward 100","right 90","forward 50","right 90","forward 100"
data "end"