Python Beginner Tutorials: Introduction To Turtle Graphics

What Is Turtle Graphics

Turtle graphics is a built in library in Python, which is easy and fun to use.

We can create cool animations while getting the hang of coding syntax.

Turtle graphics is ideal for beginners and is suitable for all ages.

To get started working on turtle, we have to open our text editor.

To use turtle we need to import it using the import command;

This command tells the computer we will be using the turtle graphics library.

We always end our turtle program with the turtle.done() command.

Example

The turtle.done() command keeps our graphics window open so we can see what we have designed.

To create our first turtle we will use the tumi = turtle.Pen() command.

This command tells the computer we have named our turtle tumi and assigned him with a pen to start drawing.

You can name your turtle anything, I have named mine tumi.

Let’s command our turtle Tumi to move forward a certain distance using this command.

We have told the computer move tumi the turtle 250 pixels forward.

Why pixels and not centimeters?

tumi the turtle lives inside the computer he doesn’t know centimeters and millimeters, he only knows pixels.

Pixels are the little dots that you see on the screen of your computer or mobile phone, they make up display, and thus our turtle tumi only moves using pixels.

Let’s run our first command.

Our turtle has moved 250 pixels forward.

Now let’s change the direction of tumi.

To do that we will use the left or right command.

We are telling the computer change the direction of tumi the turtle and inside the parentheses add the angle value.

Direction is measured in degrees.

So let’s tell the computer to move tumi the turtle forward 250 pixels then change his direction 45 degrees and move him again 50 pixels.

Drawing Our Shapes

Let’s practice programming by drawing shapes.

We have learned a few commands let’s use them to draw squares

Now let’s add some color to our square, let’s tell the computer to draw the square in pink by using the color command.

With this command we are telling the computer, tell tumi the turtle to draw our square using the color pink.

Let’s run the program and see what we get;

Adding Fill And Drawing With Special Colors

We can also add special colors which are defined using hex values.

There are a lot of websites that show the special colors and their hex values (#).

To draw with our special colors we just need to replace the color value pink with our hex value, I will be using #3C9118

We can also fill in our square, using the begin_fill() and end_fill() command.

Let’s take it a step further and have two different colors; one as the outline color and the other as the fill color.

I will choose black and pink, black as the outline color and pink as the fill color.

To execute this we will edit the tumi.color() command, and inside the parentheses add two values as black and pink.

The first value is the outline color and the second value is the fill color.

Summary

1. When starting out with programming it is best you use a fun and practical library like turtle graphics.

2. We can draw multiple shapes with turtle graphics and use loops and functions, which will be covered in other lessons

3. Practice, play around with turtle graphics and get the hang of coding and syntax.

Software Engineer, Mathematics, Artificial Intelligence