Turtle API reference | Turtletoy (2024)

Introduction

Turtletoy allows you to generate drawings with code, inspired by Python's Turtle graphics API.Programming turtles is done with vanilla JavaScript.

Getting Started

To start a new drawing, click the New turtle link and modify the example in the editor to the right.

In short, all global code is evaluated once, then the walk function is called until it returns false.

Let's first look at the global code of the example:

const turtle = new Turtle();turtle.penup();turtle.goto(-50,-20);turtle.pendown();

To draw lines, we first create a new instance of the Turtle class, then we start giving it commands. In this example, the turtle is moved to position x: -50, y: -20 on the canvas. To prevent a line being drawn from its initial position x: 0, y: 0, penup() is called first. pendown() is then called to re-enable drawing lines when moving the turtle.

Next, we define what happens every time walk is called:

function walk(i) { turtle.forward(100); turtle.right(144); return i < 4;}

In this example, every call, the turtle will move forward by 100 pixels, then turn right by 144 degrees.

The walk function will be called until it returns false. Argument i is zero the first time the function is called and will increase (+1) for each next call. The 5th call, i will be 4, and i < 4 will return false. So in this example our drawing will be complete after 5 lines.

Below is a summary of all commands available for a Turtle.

FAQ

Can I draw lines outside the walk function?
Yes, you could choose to generate the entire drawing in global code. If you write complex turtles this can take quite some time, resulting in a canvas that will initially stay blank. To make it more responsive, it is recommended to use the walk function.
Is the canvas updated every time the walk function is called?
The canvas is updated after every 100 or so calls (~16ms), resulting in a — more or less — animated drawing of your turtle.
Why can I set the pen opacity only once?
Because we want the resulting drawing to be easy to plot with a plotter and by adding (artificial) restrictions we hope to stimulate creativity.

Move and draw

forward(distance) | fd(distance)
Move the turtle forward by the specified distance, in the direction the turtle is headed.
backward(distance) | bk(distance) | back(distance)
Move the turtle backward by distance, opposite to the direction the turtle is headed. Do not change the turtle’s heading.
right(angle) | rt(angle)
Turn turtle right by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.)
left(angle) | lt(angle)
Turn turtle left by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.)
goto(x, y = undefined) | setpos(x, y = undefined) | setposition(x, y = undefined)
If y is undefined, x must be a pair of coordinates [x ,y] (e.g. as returned by pos()).
Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
jump(x, y = undefined) | jmp(x, y = undefined)
If y is undefined, x must be a pair of coordinates [x ,y] (e.g. as returned by pos()).
Move turtle to an absolute position. Do not draw a line. Do not change the turtle’s orientation.
setx(x)
Set the turtle’s first coordinate to x, leave second coordinate unchanged.
sety(y)
Set the turtle’s second coordinate to y, leave first coordinate unchanged.
setheading(to_angle) | seth(to_angle)
Set the orientation of the turtle to to_angle.
home()
Move turtle to the origin – coordinates [0 ,0] – and set its heading to its start-orientation.
circle(radius, extent = undefined, steps = undefined)
Draw a circle with given radius. The center is radius units right of the turtle; extent – an angle – determines which part of the circle is drawn. If extent is not given, draw the entire circle. If extent is not a full circle, one endpoint of the arc is the current pen position. Draw the arc in clockwise direction if radius is positive, otherwise in counterclockwise direction. Finally the direction of the turtle is changed by the amount of extent.
As the circle is approximated by an inscribed regular polygon, steps determines the number of steps to use. If not given, it will be calculated automatically.

Tell Turtle’s state

position() | pos()
Return the turtle’s current location [x ,y].
xcor() | x()
Return the turtle’s x coordinate.
ycor() | y()
Return the turtle’s y coordinate.
heading() | h()
Return the turtle’s current heading.
isdown()
Return true if pen is down, false if it’s up.

Setting and measurement

degrees(fullcircle = 360.0)
Set angle measurement units, i.e. set number of “degrees” for a full circle. Default value is 360 degrees.
radians()
Set the angle measurement units to radians. Equivalent to degrees(2 * Math.PI)

Pen control

pendown() | pd() | down()
Pull the pen down – drawing when moving.
penup() | pu() | up()
Pull the pen up – no drawing when moving.

General

clone()
Creates a clone of the turtle.
Turtle API reference | Turtletoy (2024)

FAQs

How do you make a turtle faster than 0 in Python? ›

speed() method is used to change the speed of the turtle by the value of the argument that it takes. Return or set the turtle's speed. Note: The turtle's speed lies in the range 0-10.

Is turtle an API? ›

The Turtle API is used to work with your Turtles.

Is there a turtle in Java? ›

Turtle is a selfcontained class that will allow students to make beautiful turtle graphics with ease. Makes a default turtle. Makes a default turtle at the specified position.

Why do we need to import turtle in Python script? ›

In short, the Python turtle library helps new programmers get a feel for what programming with Python is like in a fun and interactive way. turtle is mainly used to introduce children to the world of computers. It's a straightforward yet versatile way to understand the concepts of Python.

What is the highest speed turtle in Python? ›

Speed settings can be set between 1 (slowest) to 10 (fastest). But if you set the speed to 0, it has a special meaning — turn off animation and go as fast as possible. A turtle can “stamp” its footprint onto the canvas, and this will remain after the turtle has moved somewhere else.

What is the fastest turtle? ›

The leatherback sea turtle is the largest and fastest turtle on earth. It also: Bears the heaviest clutches of eggs.

Does API Turtle Fix work? ›

Great product! This product exceeded my expectations! it cleans the water so well and is great for my turtles shell! I love this product and the price is perfect!

Can Python call an API? ›

In order to work with APIs in Python, we need tools that will make those requests. The most common library for making requests and working with APIs in Python is the requests library. Since the requests library isn't part of the standard Python library, you'll need to install it to get started.

How often should I use Turtle Fix? ›

Dose: Add 1 ml per 2 U.S. gallons (7.6 L) or 5 ml per 10 U.S. Gallons (38 L) of water. Dose daily for 7 days. After 7 days, perform a 25% water change. Treatment can be continued, if necessary.

Is turtle already in Python? ›

Turtle is a module in Python that allows us to bring shapes, figures, and designs to life on a screen.

Do turtles Despawn Java? ›

If you venture away from your farm further than the despawn range (128 blocks in Java Edition, 44-128 blocks in Bedrock Edition depending on simulation distance), the turtles in the farm despawn if you have not interacted with them by offering them seagrass.

What does turtle () do? ›

Turtle Programming in Python
MethodParameterDescription
Turtle()NoneCreates and returns a new turtle object
forward()amountMoves the turtle forward by the specified amount
backward()amountMoves the turtle backward by the specified amount
right()angleTurns the turtle clockwise
15 more rows
Mar 21, 2024

Why can't i use turtle in Python? ›

turtle is a standard library so there is no need to install it. The only way you couldn't import turtle is if there was another file with the same name. Also, most python packages are not supposed to be in replit. nix .

Is turtle automatically installed in Python? ›

The turtle module is very similar to the Pen functionality that we explored using Scratch! Note that the turtle module is part of the standard Python installation, so you do not need to install it before using it. Turtle graphics, as it is known, is based on a very simple metaphor.

Is turtle a Python library? ›

turtle is a pre-installed Python library that allows users to create pictures and shapes with a provided, virtual canvas. The onscreen pen you use to draw is called the turtle.

How to increase speed in Python? ›

25 Ways to Speed Up Python Code
  1. Embrace Django. ...
  2. Use PyPy Instead of CPython. ...
  3. Use NumPy Arrays Instead of Lists. ...
  4. Use the Built-in “timeit” Module. ...
  5. Apply Generator Expressions Instead of List Comprehensions. ...
  6. Use Multiprocessing in Python Coding. ...
  7. Apply Python Profiling. ...
  8. Optimize Loops with Code Maps.
Aug 17, 2023

How to fast forward turtle in Python? ›

speed property defines the speed of your turtle. Here are how the values for adjusting speed of your turtle. Alternatively, you can use o like this: turtle. speed(0) for the fastest speed.

What is a turtle's top speed? ›

The fastest speed of any reptile was found to be 22 mph (9.8 m/s) in the case of a frightened pacific leatherback turtle. Generally, turtles move faster than tortoises, even on land. Tortoises of the genus Gopherus have been clocked at rates 0.13 to 0.30 mph (0.05 to 0.13 m/s).

How to make the Python turtle instant? ›

You can use the speed function to control how fast the turtle moves, but this has limits. To draw things instantly, you can use the noTrace from turtleBeads before you start drawing, but if you do that, you must use showPicture at the end of your code or Python may randomly decide to skip some of your commands.

Top Articles
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 5812

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.