HEALTH WARNING: Do NOT save your python file as turtle.py – this will break it!
You will need to import the Turtle library into the script.
from turtle import *
|Puts the pen down so when its moved it WILL draw a line.|
|Lifts the pen so it can be moved without drawing.|
|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.|
|The width of the pen.|
|Worth doing for complex shapes where you want to speed up the drawing.|
|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.|
|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.|
|Delete the turtle’s drawings from the screen. Do not move turtle. Use reset() if you want to move the turtle as well.|
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).
|Moves forward the given number of pixels.|
|Scratch does not have a backwards step so we need to use negative but Turtle has backward.|
|Left turns the pointer clockwise the given number of degrees.|
|Right turns the pointer anti-clockwise the given number of degrees.|
|Goes to the position indicated by the x and y co-ordinate.|
|Puts the turtle back to the middle of the window|
|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).|
|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.
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.
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)
You might want to change the size or background color of the window that opens.
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..
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)
Once the library has been imported it is possible to either use the functions immediatly e.g. forward() but you can create an instance of Turtle and use it e.g.
terry = Turtle() terry.forward()
This approach allows for multiple drawing turtles at the same time!