Python Turtle is based on the Logo Programming Language that has been used as a popular way of teaching children programming for many years.

HEALTH WARNING:  Do NOT save your python file as turtle.py – this will break it!

### Getting started

You will need to import the Turtle library into the script.

`from turtle import *`

### Basic Controls

CommandExampleScratch Equiv.Description
pendown()
` pendown()`
Puts the pen down so when its moved it WILL draw a line.
penup()
` penup()`
Lifts the pen so it can be moved without drawing.
pencolor()
` pencolor("red")`
you can set the color of the pen and its fill using any of the Tkinter Colors.  For more information about colour and fills see below.
pensize()
` pensize(5)`
The width of the pen.
hideturtle
` hideturtle()`
Worth doing for complex shapes where you want to speed up the drawing.
showturtle
` showturtle()`
Shows the turtle if it has been hidden.  Also note that shape(“Turtle”) will change the pointer – you can pick from: arrow, turtle, circle, square, triangle, classic.
speed()
`speed(0)`
Makes the turtle move faster, which is good for complex shapes.  Give a number between 0 and 10 where 6 is the standard speed.  0 is no animation, 1 is slowest and 10 is fastest.
clear()
` clear()`
Delete the turtle’s drawings from the screen. Do not move turtle.  Use reset() if you want to move the turtle as well.

### Turtle Motion

The turtle can be made to draw using the following simple commands.  It starts from 0,0, which is the centre of the window (aka origin or home position).

CommandExampleScratch Equiv.Description
forward()
` forward(100)`
Moves forward the given number of pixels.
backward()
` backward(100)`
Scratch does not have a backwards step so we need to use negative but Turtle has backward.
left()
` left(45)`
Left turns the pointer clockwise the given number of degrees.
right()
` right(90)`
Right turns the pointer anti-clockwise the given number of degrees.
goto()
` goto(160,50)`
Goes to the position indicated by the x and y co-ordinate.
home()
` home()`
Puts the turtle back to the middle of the window
circle()
` circle(100,360,6)`
Draws a circle with: a radius (e.g. 100); for an angle (e.g. 360 is full circle and 180 would be half); in the number of steps given (e.g. 8 for hexagon).
dot()
` dot()`
Draw a dot of size 1 unless you provide a bigger size e.g dot(10).

As shown above you can set the line color of the pen.  You can also use HTML colours e.g. “#33BB55” or RGB colours represented by a tuple e.g. color(100,73,45).  If you are using RGB you need to ensure that the colormode() is set to 255 e.g.

`colormode(255)`
`color(200,34,13)`

#### Filling Shapes

By default the turtle can draw a line or a circle with a transparent fill.  To fill an object you should first set the fillcolor.

`fillcolor("yellow")`

The include begin_fill and end_fill statements to tell Python when to start filling a shape and when to stop.  This example will create a circle with a black outline and red fill.

```color("black", "red")
begin_fill()
circle(80)
end_fill()```

This approach will also work with multiple lines enclosing a shape e.g. a square with a blue outline and a yellow fill.

```color("blue", "yellow")
begin_fill()
for x in range (0,4)  forward(100)  right(90)
end_fill()```

### Canvas Settings

You might want to change the size or background color of the window that opens.

`setup(1000,400)`
`bgcolor("black")`

### Random Colours

One method for picking involves making a choice from a predetermined list of colours:

```from random import choices
colors = ["red", "blue", "green"]
color(choices(colors))```

Alternativly, you could make a random color completly:

```from random import randint
randomColor = (randint(0,255),randint(0,255),randint(0,255))
color(randomColor)
```

• heading() – returns the current direction of the turtle based on 0 being East.
• towards() – gets the bearing to the coordinates given..

### Fun Equations

Calculate the XY coordinates  of a circle given its radius r, origin (cx,cy) and current angle (a).  Place this in a forloop from 0 to 360 to draw a circle. x = cx + r * cos(a) y = cy + r * sin(a)

```terry = Turtle()
{"ft_lang_label":"__label__en","ft_lang_prob":0.8108323,"math_prob":0.9343096,"size":4375,"snap":"2021-43-2021-49","text_gpt3_token_len":1121,"char_repetition_ratio":0.110043466,"word_repetition_ratio":0.005479452,"special_character_ratio":0.27085716,"punctuation_ratio":0.11587486,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97685325,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-23T15:06:34Z\",\"WARC-Record-ID\":\"<urn:uuid:e0386e6f-4b23-4e01-933c-62c218d4148b>\",\"Content-Length\":\"104639\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:33df2d92-0ac8-4d51-a44c-0c39e496419e>\",\"WARC-Concurrent-To\":\"<urn:uuid:94f4919c-4584-4327-810c-c7dfffb40334>\",\"WARC-IP-Address\":\"35.214.84.59\",\"WARC-Target-URI\":\"https://byteinsight.co.uk/2017/06/turtle/\",\"WARC-Payload-Digest\":\"sha1:SBZUERBRQXSAQPSP5J5GTF3J7DPWLUGY\",\"WARC-Block-Digest\":\"sha1:4Z5H43P2I25PKKDFMRWZJEX3WOHAWQL5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585696.21_warc_CC-MAIN-20211023130922-20211023160922-00160.warc.gz\"}"}