Turtle CoffeeScript Quick Reference

turtle coffeescript website

http://dabbler.org/home/yourname/, then select turtle.html as a template.

turtle basic motion

fd 50 moves forward 50 pixels
bk 30 moves backward 30 pixels
rt 90 turns right 90 degrees
lt 60 turns left 60 degrees

turtle drawing

pen red starts drawing a red line
pen null stops drawing a line
dot green, 30 draws 30 pixel green dot
do cs clears the screen

turtle visibility

do ht hides the turtle
do st shows the turtle
hidden() true if is hidden
shown() true if is visible

turtle absolute motion

moveto 10, 20 10 right, 20 down from topleft
moveto s moves to the location of s
moveto s, 5 moves 5 pixels towards s
turnto 45 turns to 45 degrees right of north
turnto s turns towards the object s
turnto s, 3 turns 3 degrees towards s

turtle location

center() returns {pageX:, pageY:} from topleft
direction() returns degrees from north

turtle collisions

if touches(s) ... true if turtle touches s
if encloses(s) ... if surrounds s

window motion

moveto window centers in window
$(window).moveto(s) scrolls window center to s

printing output

see x + y

loops

for x in [1..4]
  fd 50
  rt 20 * x

functions

myshape = (x) ->
  fd x
  rt 130
  fd x

variables

x = y / 2 + z

tests

if x > 2 and x <= 8
  bk 20
else if x in [1,23,37]
  fd 70
else
  myshape 90

timer events

tick 3, -> fd 10   moves 3 times per sec

mouse events

moveto lastclick
moveto lastmousemove
mousemove -> run when mouse is over

keyboard events

if keydown.which is 37 ... left arrow
if keydown.which is 38 ... up arrow
if keydown.which is 39 ... right arrow
if keydown.which is 40 ... down arrow
if keydown.which is 65 ... A key
if keydown.which is 87 ... W key
if keydown.which is 83 ... S key
if keydown.which is 68 ... D key
if keydown.which is 32 ... space key
if keydown.which is 16 ... shift key

jQuery selectors

$('#x') selects the element with <... id="x">
$('img') selects all <img> elements
$('.c') selects all with <... class="c">
$('a b') selects all <b> inside <a>...</a>

text inside elements

s.text() returns the text inside s
s.text 'hello' sets text inside s