Learn to Code via Tutorials on Repl.it!

← Back to all posts
A Turtle tutorial
yuhuiguo (5)
  1. Movement
    The from turtle import * is an instruction needed at the beginning of all of
    your turtle programs. It imports the turtle module so you can do the turtle
    instructions.

from turtle import *

Next instructions in your program tell the "turtle" how to move. The turtle
draws a line behind it as it moves. This program draws a square. The steps
given to the program are:

  1. Move forward 100 steps. (In the beginning, the turtle is facing to the
    right.)
  2. Turn 90 degrees to the left.
  3. Move forward 100 steps.
  4. Turn 90 degrees to the left.
  5. Move forward 100 steps.
  6. Turn 90 degrees to the left.
  7. Move forward 100 steps. The turtle has ended up where it started.

forward ( 100 )
left ( 90 )
forward ( 100 )
left ( 90 )
forward ( 100 )
left ( 90 )
forward ( 100 )

By calling these functions, the turtle can be made to move around the
screen. Imagine the turtle holding a pen down on the ground and drawing a
line as it moves around.
The turtle's position is two numbers: the X coordinate and Y coordinate.

The goto() function will immediately move the turtle to the given x and y
coordinates. If the pen is down (see pendown() and penup()) a line will be
drawn from the previous coordinates to the new coordinates.
This example moves the to several x and y coordinates while drawing a line
behind it:

goto ( 50 , 50 )
goto ( -50 , 50 )
goto ( 100 , -50 )
goto ( -50 , -50 )

The home() function will move the turtle to it's original position at the
coordinates (0, 0) and set it's direction to 0 degrees. Calling home() is the
same as calling goto(0, 0) and setheading(0). If the pen is down (see
pendown() and penup()) a line will be drawn as the turtle moves back
home.

forward ( 100 )
right ( 90 )
forward ( 100 )
home ()

The clear() function will erase all the line drawings on the screen. This
function does not move the turtle.

The reset()) function will erase all the line drawings on the screen and
return the turtle to the (0, 0) coordinate and facing 0 degrees. This function
does the same thing as calling the clear() and home() function.

clear ()
reset()

  1. Drawing
    The pendown() function will cause the turtle to draw as it moves around.
    The line it draws can be set with the pencolor() and pensize() functions.
    The penup() function will cause the turtle to draw as it moves around. The
    line it draws can be set with the pencolor() and pensize() functions.
    The pensize() function sets the width of the line that the turtle draws as it
    moves.

The pencolor() function sets the color of the line that the turtle draws. The
pencolor() function can be passed a string of the color, such as 'red' or
'black'. Or, the pencolor() function can be passed an "RGB color tuple"
pensize ( 20 )
pencolor ( 'red' )
forward ( 50 )
pencolor ( 0 , 1.0 , 0 )
forward ( 50 )
pencolor (( 0 , 0.5 , 0.5 ))
forward ( 50 )
pensize ( 10 )
goto ( -400 , 50 )
3. Coloring
The turtle can draw the outline of a shape and then fill it in with color using
the fill functions. The filling process starts when the begin_fill() function is
called. The turtle can move around as normal. When the end_fill() function
is called, the shape the turtle was drawing will be filled with the fill color.
The fill color is separate from the pen color.

fillcolor ( 'purple' )
pensize ( 10 )
pencolor ( 'black' )
forward ( 100 )
begin_fill ()
forward ( 100 )
left ( 90 )
forward ( 100 )
left ( 90 )
forward ( 100 )
left ( 90 )
forward ( 100 )
left ( 90 )
end_fill ()