Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Red [needs: view]
turtle: #()
win: layout [ tfield: base 500x500 white draw []
panel [
text "History" return history: text-list 180x350 data [] return
panel [ button "Save" [save request-file history/data]
button "Load" [commands: load request-file foreach cmd commands [process-cmd cmd]] ]
text "Command" cmdfield: field 300x25 [process-cmd cmdfield/text]
error: text 100x25 ]
home: func[] [
turtle/position: 250x250
turtle/color: black
turtle/direction: 0
turtle/pen-down: true
reset: func[] [
clear history/data
clear tfield/draw
turn-by: func[degrees [integer!]] [
turtle/direction: turtle/direction + degrees // 360
move-by: func[distance [integer!]] [
delta: make pair! compose [(to integer! (sine turtle/direction) * distance) (to integer! (cosine turtle/direction) * distance)]
if turtle/pen-down [append/only tfield/draw compose [line (translate-loc turtle/position) (translate-loc turtle/position + delta)]]
turtle/position: turtle/position + delta
translate-loc: func [loc [pair!]] [loc/y: 500 - loc/y loc]
set-color: func [color] [
append/only tfield/draw compose [pen (color)]
draw-turtle: func[] [
headpos: make pair! compose [(to integer! (sine turtle/direction) * 10) (to integer! (cosine turtle/direction) * 10)]
append/only tfield/draw compose [fill-pen green circle (translate-loc turtle/position) 10
circle (translate-loc turtle/position + headpos) 3 fill-pen off]
turtle-repeat: func [cnt rblk] [
repeat none! cnt [parse rblk parse-rules]
parse-rules: [
some [
['fd | 'forward] set distance integer! (move-by distance) |
['bk | 'back] set distance integer! (move-by distance * -1) |
'right set degrees integer! (turn-by degrees) |
'left set degrees integer! (turn-by degrees * -1) |
['pu | 'penup] (turtle/pen-down: false) |
['pd | 'pendown] (turtle/pen-down: true) |
'color set value word! (set-color value) |
'repeat set value integer! set blk block! (turtle-repeat value blk) |
'reset (reset) |
'home (home) |
'quit (quit) |
set word word! (error/text: mold/only compose [Unknown Word (word)]) break
process-cmd: func[cmd [string!]] [
remove back tail tfield/draw
error/text: ""
parse load/all cmd parse-rules
if empty? error/text [append history/data cmd cmdfield/text: none ]
process-cmd "home"
view/no-wait win
Copy link

meijeru commented Apr 28, 2016

I think that in turtle-repeat, the code repeat none! can be replaced by loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment