Python Programming (Turtle)

Explore Python Turtle, an engaging and interactive way to learn programming through drawing and graphics. Discover the basics of Turtle graphics, including how to create shapes and patterns.


Contents
  1. What is Turtle?
  2. Setting up Turtle
  3. Turtle Movement
  4. Turtle with loops
  5. Turtle and Subroutines
  6. Using Colour in Turtle

Turtle graphics is a feature of the Logo programming language from the 1960's. Turtle is now a feature of many programming languges including Python.

Turtle graphics makes use of x & y cooridinates and orientation to draw features on the screen. 

It has become a popular method of learning the basics of programming in a more visual way.

KS3 Computing

In order to use Turtle in Python we must first import the Turtle library. By default we don't load all of the features Python has, this reduces the amount of memory our programs use. If we forget to include the library then our programs will crash.

import turtle

We must import Turtle before we use any of the functions included in the library, however most people will simply import it at the top of their Python file. 

As soon as we have imported Turtle we can start programming but there are a few set-up options we can work with. 

Screen size

We can set up our drawing environment by adjusting the screen size. This is useful if you need more space for your drawings or want to customize the drawing area to fit a specific size.

screen = turtle.Screen()
screen.setup(width=800, height=600)

In this example, the screen is set to 800 pixels wide and 600 pixels high. We can adjust these values if needs be.

Turtle attributes

We can also use variables to set the turtle's attributes, such as its shape and color. This makes our code more flexible and easier to modify.

my_turtle = turtle.Turtle()
my_turtle.shape("turtle")
my_turtle.color("red")

In this example, the turtle's shape is set to "turtle", and its color is set to blue. You can choose from several shapes such as "arrow", "circle", "square", "triangle", and "classic".

KS3 Computing

By default, the turtle starts facing east, which means to the right side of the screen. We can move the turtle in different directions using various functions.

Moving the Turtle Forward and Backward

To move the turtle forward, we use the forward() method. This method requires a distance parameter, which tells the turtle how many pixels to move.

# Move the turtle forward by 100 pixels
turtle.forward(100)

Similarly, to move the turtle backward, we use the backward() method. This method also requires a distance parameter.

# Move the turtle backward by 50 pixels
turtle.backward(50)

Turning the Turtle

We can change the direction the turtle is facing by using the right() and left() methods. These methods require an angle parameter, which tells the turtle how many degrees to turn.

To turn the turtle to the right (clockwise), we use the right() method.

# Turn the turtle right by 90 degrees
turtle.right(90)

To turn the turtle to the left (counterclockwise), we use the left() method.

# Turn the turtle left by 45 degrees
turtle.left(45)

By combining these movement and turning commands, we can create complex drawings. Here is an example of using these commands together:

import turtle

turtle.forward(300)
turtle.right(90)
turtle.forward(300)
turtle.right(90)
turtle.forward(300)
turtle.right(90)
turtle.forward(300)

KS3 Computing

Loops allow us to run blocks of code more than once (it allows us to be lazy). We can run the same code exactly or we can vary how it runs each time. We tend to use loops when our programs get longer or have lots of repetition.

The example below created a pentagon

import turtle

turtle.forward(100)
turtle.right(72)
turtle.forward(100)
turtle.right(72)
turtle.forward(100)
turtle.right(72)
turtle.forward(100)
turtle.right(72)
turtle.forward(100)

The code that makes up the pentagon contains a lot of repetition. We can optimise the code using a for loop:

import turtle

for i in range(5):
    turtle.forward(100)
    turtle.right(72)

Here we have told it to repeat 5 times. Indented code gets repeated. Each time it loops it draws a line and then turns right 72 degrees.

We can change the number inside the brackets in order to change the number of times the indented code repeats. We can even place other for loops inside for loops to create even more complicated graphics. 

 

KS3 Computing

When we are writing a lot of code that is very similar, and we can simplify this by using subroutines. We may use subroutines to optimise the repetition when the repeated is not contigious.  

We start all subroutines off with def followed by a name. Ensure they end with a set of brackets and a colon. All the code indented inside of the subroutine is what is run when it is called. Whenever we call the name of the subroutine with a set of brackets, it runs the indented code. We can call the subroutine as many times as we like, and it runs the same way every time. Remember, it does not run the code until it is called.

import turtle

def draw_square():
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)
    turtle.right(90)
    turtle.forward(100)

draw_square()

turtle.penup()
turtle.right(90)
turtle.forward(200)
turtle.pendown()

draw_square()

turtle.penup()
turtle.forward(100)
turtle.pendown()

draw_square()
turtle.penup()
turtle.forward(100)
turtle.pendown()

draw_square()

The above code defines how the program should draw a square. The square is not drawn until the user writes draw_square().

KS3 Computing

We can add colour to our Turtle graphics by altering the line colour and the fill colour.

Pen Colour

We can change the line colour that gets drawn in turtle with the following line.

turtle.pencolor(“Red”)

The above code would make any lines drawn after be coloured red. We can change the line colour as many times as we like in our programs Please note the americanised spelling!

import turtle

turtle.pencolor(“Green”)

turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)

The above code created a square with a green pen colour.

Fill Colour

We can also fill the shapes we draw with colour. Firstly we use this line of code to state which colour to fill shapes with:

turtle.fillcolor(“Red”)

After this we need to tell python when to begin the fill and when to end it.

turtle.begin_fill()

turtle.end_fill()

It will not work unless we end the fill and the lines of shape fully join up.

KS3 Computing