Giter Site home page Giter Site logo

homerooliveira / turtlebuilder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zonble/turtlebuilder

0.0 1.0 0.0 161 KB

Turtle graphics library built on Swift function builders

Home Page: https://zonble.github.io/TurtleBuilder/

License: MIT License

Swift 100.00%

turtlebuilder's Introduction

TurtleBuilder

Actions Status Build GitHub Release GitHub Top Language License Twitter

TurtleBuilder is a turtle graphics made on the top of Swift's function builder. It allows you to use a Logo-like syntax to create and draw lines in your Swift project.

TurtleBuilder Example

Why?

Because it is fun!

Requirement

  • Xcode 11 or above
  • Swift 5.1 or above

Installation

You can install TurtleBuilder into your project via Swift Package Manager.

Usage

Build a Turtle

You can use a build block to build a turtle. For example:

let turtle = Turtle {
        penDown()
        loop(9) {
            left(140)
            forward(30)
            left(-100)
            forward(30)
        }
        penUp()
}

Then you can get lines from the turtle, by calling turtle.lines. If you render it, it would look like

screenshot.png

Commands

TurtleBuilder provides following commands to let you control your turtle:

  • pass: The command does nothing.
  • center: Move the turtle to the center of the canvas.
  • resetHeading: Reset the direction of the turtle. The turtle is facing to the top by default.
  • setHeading: Set the direction of the turtle..
  • penUp: After the command is called, the turtle moves without drawing a line.
  • penDown: After the command is called, the turtle draw a line when it is moving.
  • left | lt: Turn the turtle to left with a given degree.
  • right | rt: Turn the turtle to right with a given degree.
  • forward | fd: Ask the turtle to move forward.
  • loop | repeat: Repeat running a set of commands.
  • setMacro: Set a set of commands as a macro with a given name.
  • playMacro: Play a macro. The macro needs to be set before.

Draw In Views

TurtleBuilder provides two views where you can let the turtle to draw graphics. One is TurtleView, it renders the still lines with Quartz 2D, while another is AnimatedTurtleView, which creates animating layers using Core Animation.

You can use AnimatedTurtleView as following:

let turtleView = AnimatedTurtleView(frame: self.view.bounds) {
    penDown()
    loop(10) {
        left(10)
        forward(10)
    }
    penUp()
}

Then call turtleView.animate() to start the animation.

Known Issues

If you use variables in the commands within the loop command, we only take the values once. For example, if you have code as following:

loop(5) {
   left(Int(arc4random() % 50))
   forward(Int(arc4random() % 50))
}

We do not generate new randrom numbers in each run of the loop, and the values sent to the commands remain identical.

Patches are welcome. Enjoy! ๐Ÿข

turtlebuilder's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.