Turtle Stuff

Back to index

This is the first lesson where we get to play around with some turtle graphics. Get the following sample program up and running to find out more about turtle graphics.
#!/usr/bin/python import turtle t=turtle.Pen() t.penup() t.setx(0) t.sety(0) t.pendown() t.speed(1) t.screen.bgcolor("green") t.pencolor("black") t.pensize(3) for x in range(0,360,45): t.seth(x) t.forward(150) t.backward(150) turtle.mainloop()
Turtle Graphics was created at MIT (Massachusetts Institute of Technology) in the late 1960s for the purpose of teaching coding skills to kids. MORE INFO: TURTLE GRAPHICS (AKA, LOGO) at WikiPedia. It was its own programming language, but here it is implemented in the form of Python module. As you can see the module is imported at the beginning of the sample program. The t = turtle.Pen() creates an instance of the turtle class which is used to call the functions defined in the turtle module.

The turtle graphics commands you see here include:

  1. penup - Allows turtle to move without leaving a line
  2. setx - Changes the turtle location left to right
  3. sety - Changes the turtle location up and down
  4. pendown - Puts turtle into draw mode
  5. speed - Controls the speed at which turtle moves
  6. pensize - Sets the size of the line left by the turtle
  7. seth - Short for set-heading. The angle the turtle will move with forward or backward commands
  8. forward - Moves the turtle forward
  9. backward - Moves the turtle backward

The awesome Python trick illustrated in this lesson is in this line of code:

for x in range(0,360,45):
If it were to simply say
for x in range(0,360):
Then the line would iterate from zero to 360 incrementing by one at a time. In other words, the default incrementation interval is one. However, with the third number (in this case 45), x is incremented by 45. Since 45 multiplied by eight equals 360, we wind up with eight lines. Probably the shape produced is best described as an asterisk with eight arms.
ASSIGNMENT:

Feel free to look up additional turtle commands, but the following assignment can be completed with just the commands introduced in this lesson.

Draw the following items:

  1. An asterisk with twelve arms contained inside a square
  2. A triangle topped by an asterisk with ten arms
  3. A basic car with 20-armed asterisks for tires
All three items should be produced on the screen at the same time. They should not overlap. Nor should they be extremely small.