Skip to content

Latest commit

 

History

History
195 lines (166 loc) · 4.89 KB

README.md

File metadata and controls

195 lines (166 loc) · 4.89 KB

turtle.lua MIT License

LÖVE

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

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:


Examples

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

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
 turtlecolor() | tc()

4. Event

 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

License

Copyright (c) 2020 Erkam Badın

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.