LUA TURTLE

Commands

Use ↑ ↓ to select a command, to jump between sections.

Movement

  • forward(n)Move forward n pixels. Alias: fd
  • back(n)Move backward n pixels. Alias: bk
  • left(degrees)Turn left. Alias: lt
  • right(degrees)Turn right. Alias: rt
  • setpos(x, y)Move to coordinates (x, y)
  • setheading(angle)Face a direction (0=east, 90=north). Alias: seth
  • home()Return to center, face east
  • setx(x)Set x coordinate, keep y
  • sety(y)Set y coordinate, keep x
  • teleport(x, y)Jump to position instantly, no drawing

Pen

  • penup()Stop drawing. Alias: pu
  • pendown()Start drawing. Alias: pd
  • pensize(n)Set line width
  • pencolor(r, g, b)Set color with RGB (0–1 or 0–255)
  • pencolor("name")Set color by name: "red", "blue", "gold", etc.
  • color("red")Set both pen and fill color
  • color("red", "blue")Set pen and fill color independently

Shapes

  • circle(radius)Draw a full circle
  • circle(radius, extent)Arc: extent in degrees

Fill

  • begin_fill()Start recording a filled shape
  • end_fill()Fill the shape drawn since begin_fill
  • filling()Returns true if currently filling
  • fillcolor(r, g, b)Set fill color
  • dot(size)Draw a filled circle at turtle's position
  • dot(size, "red")Draw a dot with a specific color

Text

  • write(text)Write text at turtle's position
  • write(text, move, align, font)align: "left", "center", "right"; font: {family, size}

Canvas

  • bgcolor(r, g, b)Set background color
  • bgcolor("name")Set background color by name
  • screen_width()Returns canvas width in pixels
  • screen_height()Returns canvas height in pixels
  • clear()Erase drawing, keep turtle position
  • reset()Erase drawing and reset turtle to start
  • undo()Undo the last command
  • setundobuffer(n)Set max undo history length
  • speed(n)0=instant, 1=slowest, 10=fastest

Graphing

  • graph(left, bottom, right, top)Use graph coordinates: this rectangle, one unit the same length both ways
  • graph(left, bottom, right, top, "stretch")Graph coordinates stretched to fill the canvas
  • graph(left, bottom, right, top, box)A graph in a box: {x, y, width, height, units}; the rest of the canvas keeps its coordinates
  • setworldcoordinates(left, bottom, right, top)Make the canvas edges these coordinates, as in Python
  • axes()Draw graph paper: grid, axes and numbers
  • axes(xstep, ystep, xname, yname)Grid lines every xstep and ystep; name the axes
  • plot(f, color)Draw the curve y = f(x) across the graph
  • mark(x, y, color, label)Draw a dot at (x, y), with a label
  • g:plot(f, color)Draw on graph g even after a newer graph(); g:axes() and g:mark(x, y) too
  • g:tocanvas(x, y)The canvas point at (x, y) on graph g, for the turtle: setpos(g:tocanvas(2, 3))
  • g:tograph(x, y)The point on graph g at canvas point (x, y), such as a click

Interaction

  • onclick(f)Call f(x, y) when the canvas is clicked
  • ondrag(f)Call f(x, y) as the pointer drags across the canvas
  • onrelease(f)Call f(x, y) when the pointer lets go of the canvas
  • onkeypress(f, key)Call f when key is pressed; leave out key for any key
  • onkeyrelease(f, key)Call f when key is let go. Alias: onkey
  • done()Keep answering clicks, keys and controls until Stop; code after it waits. Alias: mainloop
  • slider(options)A slider: {label, min, max, value, step, x, y, width, onchange}; read s.value
  • button(options)A button: {label, x, y, width, onclick}
  • toggle(options)An on/off switch: {label, value, x, y, onchange}; read t.value
  • radio(options)Radio buttons: {label, options, value, x, y, onchange}; read r.value
  • textfield(options)A text box: {label, value, x, y, width, onchange, onsubmit}; read f.value
  • control.valueRead or set a control's value as the program runs; control.label likewise
  • control:remove()Take a control off the canvas

Turtle

  • hideturtle()Hide the turtle. Alias: ht
  • showturtle()Show the turtle. Alias: st
  • stamp()Stamp turtle shape; returns id
  • clearstamp(id)Remove a stamp
  • clearstamps(n)Remove last n stamps; omit n to clear all
  • Turtle()Create a second turtle with all the same methods

State Queries

  • position()Returns x, y coordinates
  • xcor() / ycor()Returns x or y coordinate
  • heading()Returns current angle in degrees
  • isdown()Returns true if pen is down
  • isvisible()Returns true if turtle is visible
  • distance(x, y)Returns distance to point
  • towards(x, y)Returns angle towards point

Turn on Demos above to see this command in action.


              
100%

Drag or scroll to pan
Pinch or Ctrl/⌘+scroll to zoom
Drag to pan
Pinch to zoom

Terminal