Turtle

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.

Basic Controls

Command Example Scratch Equiv. Description
pendown() scratchPenDown.jpg - 11/2015 Puts the pen down so when its moved it WILL draw a line.
penup() scratchPenUp.jpg - 11/2015 Lifts the pen so it can be moved without drawing.
pencolor() scratchPenColor.jpg - 11/2015 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() scratchPenSize.jpg - 11/2015 The width of the pen.
hideturtle
Worth doing for complex shapes where you want to speed up the drawing.
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()
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()
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).

Command Example Scratch Equiv. Description
forward() scratchMove.jpg - 11/2015 Moves forward the given number of pixels.
backward()
scratchMoveBackwards.jpg - 11/2015 Scratch does not have a backwards step so we need to use negative but Turtle has backward. 
left() scratchLeft.jpg - 11/2015 Left turns the pointer clockwise the given number of degrees.
right()
scratchRight.jpg - 11/2015 Right turns the pointer anti-clockwise the given number of degrees.
goto() scratchGoTo.jpg - 11/2015  Goes to the position indicated by the x and y co-ordinate.
home() scratchHome.jpg - 11/2015 Puts the turtle back to the middle of the window
circle() 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() Draw a dot of size 1 unless you provide a bigger size e.g dot(10).

Advanced Colors

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.

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.

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.

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

Canvas Settings

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

Random Colours

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

Alternativly, you could make a random color completly:

Headings

  • 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)

Advanced Usage

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.

This approach allows for multiple drawing turtles at the same time!

 

About CDB 361 Articles
Self-Employed Software Developer, Spark, Property Management, Hobby Forestry, Ex-Teacher, Engineering - Wood, Metal, Electrics & Computers. Outdoors - Walk, Cycle, Kitesurf,

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.