Skip to content

Instantly share code, notes, and snippets.

@mydoghasworms
Last active October 2, 2018 12:55
Show Gist options
  • Save mydoghasworms/bd342a10f2b806f050ed903e5be435a0 to your computer and use it in GitHub Desktop.
Save mydoghasworms/bd342a10f2b806f050ed903e5be435a0 to your computer and use it in GitHub Desktop.
Red [needs: view]
turtle: #()
win: layout [ panel [
tfield: base 500x500 white draw []
origin tfield/offset tlayer: base 500x500 255.255.255.255 draw [] ]
panel [
text "History" return history: text-list 200x350 data [] return
panel [ button "Save" [save request-file history/data]
button "Load" [commands: load request-file foreach cmd commands [process-cmd cmd]] ]
return status: text "" 200x25 ]
return
text "Command" cmdfield: field 300x25 [process-cmd cmdfield/text]
error: text 100x25 ]
home: func[] [
turtle/position: 250x250
turtle/color: black
turtle/direction: 0
turtle/x: 255.0 turtle/y: 255.0
turtle/pen-down: true
]
reset: func[] [
clear history/data
clear tfield/draw
turtle/visible: true
turtle/procs: #()
home
]
turn-by: func[degrees [integer!]] [
turtle/direction: turtle/direction + degrees // 360
]
loc-pair: func[x y] [make pair! compose [(to integer! round x) (to integer! round y)]]
move-by: func[distance [integer!]] [
delta: compose [x ((sine turtle/direction) * distance) y ((cosine turtle/direction) * distance)]
if turtle/pen-down [ append/only tfield/draw
compose [line (translate-loc loc-pair turtle/x turtle/y) (translate-loc loc-pair turtle/x + delta/x turtle/y + delta/y)]
]
turtle/x: turtle/x + delta/x
turtle/y: turtle/y + delta/y
]
translate-loc: func [loc [pair!]] [loc/y: 500 - loc/y loc]
set-color: func [color] [
append/only tfield/draw compose [pen (color)]
]
redraw-turtle: func [] [
clear tlayer/draw
if turtle/visible [
headpos: make pair! compose [(to integer! (sine turtle/direction) * 10) (to integer! (cosine turtle/direction) * 10)]
append/only tlayer/draw compose [fill-pen green circle (translate-loc loc-pair turtle/x turtle/y) 10
circle (translate-loc (loc-pair turtle/x turtle/y) + 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) |
['rt | 'right] set degrees integer! (turn-by degrees) |
['lt | '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) |
'to copy proc to 'end thru 'end (put turtle/procs proc/1 next proc) |
'ht (turtle/visible: false) |
'st (turtle/visible: true) |
'reset (reset) |
'clear (clear tfield/draw) |
'home (home) |
['quit | 'q] (quit) |
set word word! (either blk: select turtle/procs word [parse blk parse-rules]
[error/text: mold/only compose [Unknown Word (word)]]) break
]
]
process-cmd: func[cmd [string!]] [
error/text: ""
parse load/all cmd parse-rules
if empty? error/text [append history/data cmd cmdfield/text: none ]
redraw-turtle
status/text: mold/only compose [Position: (loc-pair turtle/x turtle/y) Direction: (turtle/direction) degrees]
]
process-cmd "reset"
view/no-wait win
@geekyi
Copy link

geekyi commented Aug 21, 2017

Less steps:

reset
repeat 4 [fd 20]

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