Turtle.lua: Turtle Graphics Library for LÖVE

Turtle.lua

Minimalist drawing library is inspired by turtle graphics , written in lua for löve2d.

LÖVE

Prerequisite

How to use

*Create a turtle instance. Give it your commands and call its draw() function in love.draw(), That's it!*

View methods in detail

To draw a triangle:

local triangle = Turtle()

function love.load()
 -- turtle.lua supports chain methods as you can see below
    triangle:forward(60):left(120):forward(60):left(120):forward(60)
end

function love.draw()
   triangle:draw()
end

Result:
tri.gif

Examples

We added some examples to introduce you turtle library. See what we've done in Examples

colorful.gif

 

spidey.gif
logo.gif

 

spores.gif
beauty.gif

 

machine.gif
snake.gif

What is turtle?

As docs.python introduces:

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.

By combining together these and similar commands, intricate shapes and pictures can easily be drawn.

Turtle Methods

1. Turtle motion

  • Move and draw
 forward() | fd()  
 backward() | bk() | back()
 right() | rt()
 left() | lt()
 tl()
 rt()
 circle()
 setheading() | seth()
 home()
 go_to() | go() | setpos() | setposition()
 setx()
 sety()
 undo(c)
 speed()
  • Turtle's state
   position()
   heading()
   xcor()
   ycor()
   distance()
   name()
   nodecount()
   print()

2. Pen control

Drawing state

 pendown() | pd() | down()
 penup() | pu() | up()
 pensize() | width()
 isdown()

Color control

 color(...)
 fillcolor(...)
 turtlecolor()

Filling

 begin_fill()
 end_fill()

Drawing control

 reset()
 clear()

3. Turtle state

  • Visibility
 showturtle() | st()
 hideturtle() | ht()
 isvisible()
  • Color

4. Event

 turtlecolor() | tc()
 ondrawfinish() 

5. Animation

 play()
 pause()
 toggle()

6. Debug

 debugon()
 debugoff()
 drawDebug()

Quick example

local line = Turtle()
local circle = Turtle(100,100,1)
local triangle = Turtle(100,150,1, {1, 1, 1})

function love.load()
    local red = {1, 0, 0}
    line:clear():right(35):forward(100)
    circle:setcolor(1,1,1):penup()
    for i=1, 360 do
        circle:pendown():right(1):forward(2)
    end
    triangle:setcolor(red):left(60):forward(50):right(120):forward(50):left(60):backward(50)
end

function love.draw()
    line:draw()
    circle:draw()
    rectangle:draw()
end

Download Details:

Author: Arthurealike
Source Code: https://github.com/arthurealike/turtle.lua 
License: MIT license

#lua #draw 

Turtle.lua: Turtle Graphics Library for LÖVE
2.15 GEEK