Basic Movement

Contents
  1. What is Turtle?
  2. Setting up Turtle
  3. Turtle Movement

1. What is 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.


2. Setting up Turtle

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".


3. Turtle Movement

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)