-
-
Save dockimbel/92c319ed02238c729c03c2b671ed42b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
do-events/no-wait | |
wait .1 | |
] | |
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] [ | |
loop 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did some of my own tweaks to Logo_Turtle (not changed on github)
-Running on Win10
Line 10: increased status: text area from 200x25 to 200x100 (there wasn't enough room for text)
Line 13: increased error: text area from 100x25 to 300x25 (there wasn't enough room for text)
Line 97: Added the status of Pen Down (see below)
status/text: mold/only compose [Position: (loc-pair turtle/x turtle/y) Direction: (turtle/direction) degrees Pen Down: (turtle/pen-down)]
Bugs:
-Every second line does not draw (FD or BK at any angle)
-Does not appear to be due to toggling Pen Down. That is why I modified line 97.