Programming
with Snakes (part 4)
and Turtles

The Logo Turtle
The Logo programming language was created in 1967 at the Massachusetts Institute of Technology (MIT)
One aspect of the language that is still used is the way it could be used to draw lines using a robot called a "turtle".
About Logo Turtle
The "turtle" is a drawing robot that has three attributes:
-
location: an x - y position
-
orientation: the direction it is facing
- pen: the thing that does drawing
The pen has attributes as well such as colour, width, and up versus down.
The Yellow Turtle

Python Turtle
In Python we can simulate the actions of the turtle using commands that are very similar to the original Logo commands.
Sample commands:
forward / backward
right / left / setheading
penup / pendown
goto / setx / sety
First Turtle Program
# This gives our program access to all of the turtle commands from turtle import * # The default start position is the centre (0, 0) and pointing to the right forward(200) # Move the turtle forward 200 pixels (dots on the screen) # This leaves the screen visible until it is clicked Screen().exitonclick()
Comments
A good program contains comments. These is extra information about the commands that helps the reader understand what you doing and why you did it.
In Python, anything that occurs in a line after the '#' symbol is ignored (unless it is between quotation marks)
Command Reference
A reference to the commands that are available to the Python turtle commands we can use can be found at
Lets look at a few commands ...
Moving the Turtle
forward / backward e.g. forward(100)
-
Move the turtle forward /backward a number of units.
-
Units are typically pixels (dots on the screen)
-
The movement is in the direction that the turtle is pointed.
setx / sety e.g. sety(-90)
-
Move the turtle from the current position to the location with the x / y position set to the input value
goto e.g. goto(150, -200)
-
Move to the turtle to the specified position
Turtle Direction
left / right e.g. left(90)
-
Changes the direction the turtle faces by rotating counterclockwise / clockwise by a number of degrees
-
Rotation starts from the current direction
setheading e.g. setheading(90)
-
Sets the direction to an absolute direction
0 - to the right90 - up180 - to the left270 - down
Pen Drawing
penup / pendown e.g. penup()
-
Lifts / drops the pen for drawing
-
Lines are drawn when the pen is down
pensize e.g. pensize(20)
-
Sets the width of the line drawn by the pen (0 is thinnest line)
- Width is in pixels
pencolor e.g. pencolor(0.0, 0.0, 1.0)
-
Sets the colour of the line drawn
-
Can use colour names or RGB values from 0.0 to 1.0
Circles
circle e.g. circle(100, 180, 6)
-
Draw all or part of a circle or polygon
-
First parameter specifies the radius of the circle
-
Second parameter specifies the number of degrees to draw (default to 360)
-
Third parameter specifies the number of lines to use to draw the shape (defaults to making it look like a curve)
-
Only the first parameter is required.
Polygons
circle(200, None, 6)

Polygons
circle(200, 180, 6)

Assignment
Draw a happy face using turtle.
-
It must have a round head, two eyes and a smiling mouth.
-
The head must be centred in the window.
-
You must submit a program file that draws the face.
Programming in Python - Part 4
By gordie
Programming in Python - Part 4
- 1,209