Python Turtle Programming for Kids – Part I

  • Turtle graphics is a popular way for introducing programming to kids.
  • It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.
  • The turtle module provides an environment where turtles move upon a 2-dimensional grid.
  • Turtles have a position, a heading (the direction in which the turtle is facing), and a variety of possible states (turtles can draw lines in a particular colour when they move or leave no trace) and actions (turning left or right; moving forward or backward.

Turtle Methods

  • Turtle Motion
    • Move and Draw
    • Tell Turtle’s State
    • Setting and Measurement
  • Pen Control
    • Drawing State
    • Color Control
    • Filling
    • More Drawing Control
  • Turtle State
    • Visibility
    • Appearance
  • Events
  • Special Turtle Methods
  • Turtle Screen Methods
    • Window Control
    • Animation Control
    • Using Screen Events
    • Settings and Special Methods
    • Methods Sepcific to Screen

Turtle Screen Methods

  • import turtle – Allows us to use the turtle library
  • turtle.Screen() – Creates the graphics window
  • screensize() – If no arguments are given, return current (canvaswidth, canvasheight)
  • bgcolor() – Set or return background color of the TurtleScreen
  • title() – Set title of turtle window to titlestring
  • turtle.clear() – Delete the turtle’s drawings from the screen. Do not move turtle. State and position of the turtle as well as drawings of other turtles are not affected.
  • turtle.reset() – Delete the turtle’s drawings from the screen, re-center the turtle and set variables to the default values.
  • turtle.bye() – Shut the turtlegraphics window.
>>> import turtle
>>> myscreen = turtle.Screen()
>>> myscreen.screensize()
(400, 300)
>>> myscreen.bgcolor()
'white'
>>> myscreen.title("Welcome to Python Turtle Programming")
>>> myscreen.bgcolor("light blue")
>>> turtle.clear()
>>> turtle.reset()
>>> mypen.clear()
>>> myscreen.clear()
>>> myscreen.reset()

Turtle Methods

  • turtle.Turtle() – Creates and returns a new turtle object
  • shape() – Set turtle shape to shape with given name or, if name is not given, return name of current shape. These are the current available polygon shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”.
  • pos() – Return the turtle’s current location (x,y)
  • home() – Move turtle to the origin – coordinates (0,0) – and set its heading to its start-orientation (which depends on the mode.
  • turtle.turtles() – Return the list of turtles on the screen.
  • hideturtle() – Make the turtle invisible.
  • showturtle() – Make the turtle visible.
  • forward() – Move the turtle forward by the specified distance, in the direction the turtle is headed.
  • backward() – Move the turtle backward by distance, opposite to the direction the turtle is headed. Do not change the turtle’s heading.
  • left() – Turn turtle left by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) Angle orientation depends on the turtle mode
  • right() – Turn turtle right by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) Angle orientation depends on the turtle mode
>>> mypen = turtle.Turtle()
>>> mypen.shape()
'classic'
>>> mypen.pos()
(0.00,0.00)
>>> mypen.forward(100)
>>> mypen.left(90)
>>> mypen.forward(100)
>>> mypen.left(90)
>>> mypen.forward(100)
>>> mypen.left(90)
>>> mypen.forward(100)
>>> mypen.forward(100)
>>> mypen.pos()
(-0.00,-100.00)
>>> mypen.home()
>>> mypen.pos()
(0.00,0.00)
>>> turtle.turtles()
[<turtle.Turtle object at 0x035346B0>]
>>> turtle.hideturtle()
>>> turtle.forward(100)
>>> turtle.forward(100)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)
>>> turtle.forward(100)
>>> turtle.showturtle()
>>> turtle.left(45)
>>> turtle.left(180)
>>> turtle.forward(100)
>>> turtle.right(90)
>>> turtle.forward(100)
>>> turtle.backward(100)
>>> turtle.backward(100)
>>> turtle.right(90)
>>> turtle.backward(100)
>>> mypen.forward(100)
>>> mypen.backward(100)
>>> mypen.backward(100)
>>> mypen.backward(100)
>>> mypen.backward(100)
>>> mypen.backward(100)
>>> mypen.backward(100)
>>> mypen.backward(100)
>>> turtle.turtles()
[<turtle.Turtle object at 0x035346B0>, <turtle.Turtle object at 0x03534870>]
>>> turtle.pos()
(158.58,-182.84)
>>> mypen.pos()
(-200.00,0.00)
>>> turtle.shape("square")
>>> turtle.shape("turtle")
>>> turtle.shape("circle")
>>> turtle.shape("arrow")
>>> turtle.shape("triangle")
>>> turtle.shape()
'triangle'

Help – Invoke the built-in help system

  • If no argument is given, the interactive help system starts on the interpreter console.
  • If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console.
  • If the argument is any other kind of object, a help page on the object is generated.
>>> help(turtle.bgcolor)
Help on function bgcolor in module turtle:

bgcolor(*args)
    Set or return backgroundcolor of the TurtleScreen.

    Arguments (if given): a color string or three numbers
    in the range 0..colormode or a 3-tuple of such numbers.

    Example:
    >>> bgcolor("orange")
    >>> bgcolor()
    'orange'
    >>> bgcolor(0.5,0,0.5)
    >>> bgcolor()
    '#800080'

>>> help(turtle.Screen)
Help on function Screen in module turtle:

Screen()
    Return the singleton screen object.
    If none exists at the moment, create a new one and return it,
    else return the existing one.

turtledemo — Demo scripts

The turtledemo package includes a set of demo scripts. These scripts can be run and viewed using the supplied demo viewer as follows: python -m turtledemo

Alternatively, you can run the demo scripts individually. For example,

python -m turtledemo.bytedesign

For more information on demo scripts visit the below URL

  • https://docs.python.org/3/library/turtle.html#module-turtledemo

References

  • https://docs.python.org/2/library/turtle.html#module-turtle
  • https://docs.python.org/3/library/turtle.html#module-turtle
  • https://docs.python.org/3.1/library/turtle.html
  • https://docs.python.org/3.3/library/turtle.html?highlight=turtle#module-turtle

Lear more about Turtle Graphics Methods in the next upcoming Blog Articles.

Happy Learning!