Introduction for Turtle

introduction

We are going to use Turtle graphics to draw line segments, circles, and arcs. Before we start, we need to look at a few introductory topics:

Module

introduction

A module is just a collection of existing Python code that you can import and use. It is nice to have code that is available to use. All you need to do is import it.

the turtle module

introduction

The turtle module contains the Python stuff we need in order to do Turtle graphics. The turtle module needs to be imported so we can use it. Note that the turtle module is spelled with a lower case t.

code
from turtle import *
  
notes

The * means everything you need for this module

You do not see anything happening yet. We just imported the turtle stuff, but have not actually used it yet.

class

introduction

A class provides functions that you can use. They are called member functions or methods. They work a little differently than the functions that are not a member of a class.

the Screen class

introduction

The Screen class is not required to do turtle drawing. In the example the Screen class is used to create a background color. The Screen() constructor funtcion is used to create the screen. The Screen class is spelled with a capital S. We will create a reference to the screen. In this example the name of the reference is screen1, but you can use any name.

code
from turtle import *
screen1 = Screen()
  
notes

This creates an empty screen for the turtles to play in.

the Turtle class

introduction

We will use the Turtle() constructor function to create a turtle. The Turtle class is spelled with a capital T. We will create a reference to the turtle. In this example the name of the reference is turtle1, but you can use any name. A turtle is a relatively slow moving object that you can cause to move around in the screen and draw a line where it goes. You can create more than one Turtle to play in your screen object.

code
from turtle import *
screen1 = Screen()
turtle1 = Turtle()
  
notes

The turtle now shows up in the middle of the screen pointing to the right. It looks like an arrowhead.

set the screen background color

introduction

Now change the background color of the screen to SkyBlue by using the bgcolor member function. Good color names can be found by searching the web for html color names. We will use the bgcolor() function. Put the color name in quotation marks in the parentheses. The bgcolor() function is a member function of the Screen class.

Because our screen1 object was created as a member of the Screen class, it can use member functions belonging to the Screen class. The dot (.) operator means that the following item is a member belonging to the object before the dot. So screen1.bgcolor() means that bgcolor() belongs to the screen1 object. Actually it belongs to the Screen class, but screen1 is permitted to use it because it is an object that belongs to the Screen class.

code
from turtle import *
screen1 = Screen()
turtle1 = Turtle()
screen1.bgcolor("SkyBlue")
    

set turtle color

introduction

Now change the color of the turtle to ForestGreen. The color() function is a member of the Turtle class. This makes the turtle, and the lines it draws, ForestGreen.

code
from turtle import *
screen1 = Screen()
turtle1 = Turtle()
screen1.bgcolor("SkyBlue")
turtle1.color("ForestGreen")
    

setup is completed

note

This ends the introduction and set up. Now we are ready to draw.