Python Turtle Graphics The Complete Guide
Python Turtle Graphics The Complete Guide
coolpythoncodes.com /python-turtle/
Python turtle graphics is one of the cool ways to implement your knowledge in Python before you start writing
complex programs to solve problems in life.
In this tutorial, I will make Python more practical for you and also fun.
I know that before you came across this tutorial, you were looking for fun things to do with Python.
When you run this program, you will get this result on your screen.
1/47
Python Turtle Graphics Window
You can see an arrow head at the middle point of the window pointing to the east which is the default position. This
location is called home.
The position of the turtle is specified with the two-dimensional coordinate system, (x,y).
The default position of the turtle(arrow head) is (0,0) which is the center of the window.
I will have to talk about some things that are important and it is for those that will use Python IDE (pycharm, spyder
etc) for this tutorial.
When you run the code snippets of this tutorial in any Python IDE you will notice that the turtle window will open and
close immediately.
To allow the turtle window stay so you can see the output of your program, include t.screen.mainloop() in your
program. It should be the last statement of your program.
To make the turtle window disappear after viewing the turtle screen when you click on the turtle canvas, use the
method screen.exitonclick() .
2/47
The following code snippet shows you how you can use them.
When you run the code snippet above in any Python IDE such as Pycharm or Spyder, the turtle screen won’t
disappear immediately unless you click on the turtle canvas.
To change the color of the turtle window background, please open a new file editor window and type the following
code snippet. Save it as Turtle_window_background.py
When you run this program, you will get this result on your screen.
3/47
Python Turtle Graphics Window
The background color of the turtle window is black but there is no turtle because the default color of the turtle is
black.
Note :
The screen.bgcolor() method accepts one argument which is the color of the turtle window as a string.
The default color of Python turtle window is white. You can change it using the method screen.bgcolor()
Type the following code snippet into a new file editor and save it as any name you like.
Python
4/47
1 from turtle import Turtle
2 t=Turtle()
3 t.screen.bgcolor("black")
4 t.color("blue")
When you run this program, you will get this result on your screen.
5/47
1 from turtle import Turtle
2
3 t=Turtle()
4
5 t.hideturtle()
6
7 t.screen.bgpic("night-owl-75.gif")
Note:
t.setheading(angle)
You can set the turtle to face any angle of your choice.
You can change the direction of the turtle using the following syntax
t.left(angle)
t.right(angle)
The above is used to rotate the turtle either to the left or to the right at the angle specified.
To change the shape of the turtle you use the shape() method.
The shape() method accepts a string as an argument. The string is the name of a valid shape in the TurtleScreen’s
shape dictionary.
Arrow
Circle
Square
Triangle
Turtle
Classic
Like I said earlier, the default shape of the turtle is known as a classic.
I will show you my favorite turtle shape which you will love as well.
7/47
Open a new file editor window and type the following code snippet. Save it as turtle_shape.py
turtle shape
Python
Turtle Shape-“Turtle”
From the above screenshot, the turtle is small. To increase the size of the turtle, use the shapesize() method.
8/47
1 from turtle import Turtle
2 t=Turtle()
3 t.shape("turtle")
4 t.shapesize(20)
t.fd(distance)
or
t.goto(x,y)
Note:
The Cartesian system is the coordinate system for Turtle. So x and y represent values in the x and y axis
respectively.
Open a new file editor window and type the following code snippet. Save it as draw_line.py
Draw a line
Python
9/47
1 from turtle import Turtle
2 t=Turtle()
3 t.fd(100)
The above program moves the turtle 100 pixels to the east.
Open a new file editor window and type the following code snippet. Save it as turtle_go_to.py
turtle go to
Open a new file editor and save the following code snippet as turtle_line_thickness.py
1 t=Turtle()
2 t.screen.bgcolor("black")
3 t.color("blue")
4 t.pensize(10)
5 t.fd(100)
when you run the code snippet above, you will get:
11/47
How to Hide the Turtle(Arrow Head)
You may not need the turtle to appear in your drawing as you just need a line. To achieve this open a new file editor
and save the following code snippet as draw_line_without_turtle.py
Hiding turtle
Python
Project 1:
Write a program to draw a square on the computer screen and save it as turtle_square.py
12/47
Let the background screen color be black and color of the line drawn red.
Solution 1: for a beginner that doesn’t know Python function and for loop
Drawing a Square
Python
The above program first changes the window background to black and the turtle to red using
t.screen.bgcolor(“black”) and t.color(“red”).
The turtle is then made invisible using this line of code t.hideturtle()
The turtle moves 100 pixels to the east (t.fd(100)) and draws a horizontal line.
It then faces the north by rotating 90degrees(t.left(90)) to the left and moves 100 pixels to the north, drawing a
vertical line.
t.left(90) makes the turtle face the west by rotating it 90 degrees left. It then moves 100 pixels to the west, drawing
a horizontal line.
t.left(90) makes the turtle face the south by rotating it 90 degrees left. It then moves 100 pixels to the south, drawing
a vertical line.
The turtle turns 90 degrees left to face the east, it then moves 100 pixels to the east which is the default position of
the turtle.
Solution 2:
Let’s make our code more readable and maintainable with function and a for loop. Save this as turtle_square1.py
13/47
1 from turtle import Turtle
2 t=Turtle()
3 t.screen.bgcolor("black")
4 t.color("red")
5 t.hideturtle()
6
7 def square(length):
8 for steps in range(4):
9 t.fd(length)
10 t.left(90)
11
12 square(100)
A square
Note:
Basically, the for loop runs through the code below four times.
t.fd(length)
14/47
t.left(90)
But Python made it fun as I could write some bunch of codes and my computer would do the drawing.
Project 2:
Write a program to draw a rectangle inclined to 30 degrees on the computer screen and save it as
turtle_rectangle.py
Let the screen background color be black and color of the line drawn red.
Solution 1: for a beginner that doesn’t know Python function and for loop
Inclined Rectangle
Python
Solution 2:
Open a new file editor window and type the following code snippet. Save it as turtle_rectangle1.py
Inclined Rectangle
Python
15/47
1 from turtle import Turtle
2 t=Turtle()
3 t.screen.bgcolor("black")
4 t.hideturtle()
5 t.color("red")
6
7 def slanted_rectangle(length,width,angle):
8 t.setheading(angle)
9 for steps in range(2):
10 t.fd(width)
11 t.left(90)
12 t.fd(length)
13 t.left(90)
14
15 slanted_rectangle(length=200,angle=45,width=100)
An Inclined Rectangle
16/47
Yes, of course.
Project 3:
Write a program to draw a triangle on the computer screen and save it as turtle_triangle.py
Let the screen background color be black and color of the line drawn red.
Solution:
Draw a Triangle
Python
Don’t be over excited yet, there’s a ton of room for improvement in our programs.
1. radius
2. extent
3. steps
The most important is radius and extent. Though, extent and steps are optional(i.e a circle can still be drawn without
including them.)
The extent accepts an integer which is an angle. To cut short the technical explanation, it determines the part of the
circle that will be drawn.
If you assign 360, a full circle will be drawn, 180 will draw a semi-circle. (Do you now understand why I said extent
represents an angle?)
Let’s do some coding. The code snippet below will draw a semi-circle.
Open a new file editor window and type the following code snippet. Save it as turtle_semi_circle.py
Semi circle
17/47
Python
Semi circle
18/47
Semi Circle
Open a new file editor window and type the following code snippet. Save it as semi_circle1.py
Python
19/47
The only thing strange about this code snippet is t.setx().
The method setx() positions the turtle on the x-axis leaving the y-axis unchanged.
In addition, the method sety() positions the turtle on the y-axis leaving the x-axis unchanged.
To do a similar action with the turtle, you use this syntax below,
t.up()
To put back the turtle on the canvas, use this syntax below,
t.down()
Note:
up()
down()
goto(x,y)
circle(radius)
I want you to observe the following code snippet and the diagram.
Code snippet 1:
Open a new file editor window and type the following code snippet. Save it as code_snippet_1.py
Python
20/47
code snippet 1 screenshot
From the diagram above you can observe that the turtle moved to an absolute position ( t.goto(100,50))
The next code snippet will remove the line in the diagram above.
Code snippet 2:
Open a new file editor window and type the following code snippet. Save it as code_snippet_2.py
Python
21/47
1 from turtle import Turtle
2 t=Turtle()
3 t.screen.bgcolor("black")
4 t.color("red")
5 t.up()
6 t.goto(100,50)
7 t.down()
8 t.circle(50)
9 t.hideturtle()
t.up() – raises the turtle up and no drawing is done when the turtle is up.
The setposition() method accepts two arguments which are the coordinates x and y.
This method moves the turtle to an absolute position. If the turtle pen is down, a line will be drawn to the specified
position.
t.begin_fill() is called before drawing a shape to be filled with the color of the turtle.
t.end_fill() fills the shape drawn with the color of the turtle after t.begin_fill() has been called.
Open a new file editor window and type the following code snippet. Save it as red_circle.py
red circle
Python
23/47
Red circle
t.fillcolor()
This meth0d changes the color of the turtle only, when the turtle moves, the color path remains the default color
(black) .
t.fillcolor(“blue”)
24/47
1 from turtle import Turtle
2 import random
3
4 t=Turtle()
5 t.screen.bgcolor("black")
6
7 def random_drawing(turns,distance):
8 for x in range(turns):
9 right=t.right(random.randint(0,360))
10 left=t.left(random.randint(0,360))
11 t.color(random.choice(["blue","red","green"]))
12 random.choice([right,left])
13 t.fd(distance)
14
15
16
17 random_drawing(100,50)
Random drawing
Note:
Why?
Random drawing
How then?
t.speed() accepts an integer as an argument to set the speed of the turtle. The value 0(zero) is the fastest speed of
the turtle
So to increase the speed of the program (turtle_random_walk.py) include t.speed(0) before defining the function
randomwalk.
26/47
A dot
So we have been drawing shapes on our computer screen, and am getting really bored of it.
Project 4:
Open a new file editor and save the following code snippet as write_program.py
28/47
Cool Python Codes
The cool font I used is “Freestyle Script” and I got it from Microsoft word application.
I want to do some logo designs for my blog, and I don’t want to use Photoshop.
Guess what?
I will do it in Python.
Here are some code snippets to the logo design for my blog.
Logo design 1:
Open a new file editor and save the following code snippet as Cool_Python_Codes_Logo1.py
logo snippet 1
Python
29/47
1 from turtle import Turtle
2 t=Turtle()
3 t.screen.bgcolor("black")
4 t.color("white")
5
6 def square(length):
7 for steps in range(4):
8 t.fd(length)
9 t.left(90)
10
11 def draw_square(x,y,length):
12 t.hideturtle()
13 t.up()
14 t.goto(x,y)
15 t.down()
16 t.begin_fill()
17 square(length)
18 t.end_fill()
19
20 def rectangle(length,width):
21 for steps in range(2):
22 t.fd(width)
23 t.left(90)
24 t.fd(length)
25 t.left(90)
26
27 def draw_rectangle(length,width,x,y):
28 t.hideturtle()
29 t.up()
30 t.goto(x,y)
31 t.down()
32 t.begin_fill()
33 rectangle(length,width)
34 t.end_fill()
35
36 t.write("Cool Python Codes", move=True, align='center',
37 font=('Cambria', 18, 'normal'))
38
39 draw_square(-120,-20,20)
40 draw_square(-120,30,20)
41 draw_square(-140,0,20)
30/47
Code Python Codes logo design 1.
Logo design 2:
Open a new file editor and save the following code snippet as Cool_Python_Codes_Logo_2.py
31/47
1 from turtle import Turtle
2
3 t=Turtle()
4 t.screen.bgcolor("black")
5 t.color("blue")
6
7 def square(length):
8 for steps in range(4):
9 t.fd(length)
10 t.left(90)
11
12 def draw_square(x,y,length):
13 t.hideturtle()
14 t.up()
15 t.goto(x,y)
16 t.down()
17 t.begin_fill()
18 square(length)
19 t.end_fill()
20
21 def rectangle(length,width):
22 for steps in range(2):
23 t.fd(width)
24 t.left(90)
25 t.fd(length)
26 t.left(90)
27
28 def draw_rectangle(length,width,x,y):
29 t.hideturtle()
30 t.up()
31 t.goto(x,y)
32 t.down()
33 t.begin_fill()
34 rectangle(length,width)
35 t.end_fill()
36
37
38
39 t.write("Cool Python Codes", move=True,
40 align='center',
41 font=('Cambria',18, 'normal'))
42
43 draw_square(-120,-20,20)
44 draw_square(-120,30,20)
45 draw_rectangle(30,10,-130,0)
32/47
Cool Python Codes logo design 2
Logo design 3:
Open a new file editor and save the following code snippet as Cool_Python_Codes_Logo_3.py
33/47
1 from turtle import Turtle
2
3 t=Turtle()
4 t.screen.bgcolor("black")
5 t.color("magenta")
6
7 def square(length):
8 for steps in range(4):
9 t.fd(length)
10 t.left(90)
11
12 def draw_square(x,y,length):
13 t.hideturtle()
14 t.up()
15 t.goto(x,y)
16 t.down()
17 t.begin_fill()
18 square(length)
19 t.end_fill()
20
21 def rectangle(length,width):
22 for steps in range(2):
23 t.fd(width)
24 t.left(90)
25 t.fd(length)
26 t.left(90)
27
28 def draw_rectangle(length,width,x,y):
29 t.hideturtle()
30 t.up()
31 t.goto(x,y)
32 t.down()
33 t.begin_fill()
34 rectangle(length,width)
35 t.end_fill()
36
37 t.write("Cool Python Codes", move=True, align='center',
38 font=('Cambria', 18, 'normal'))
39
40 draw_square(-120,-20,20)
41 draw_square(-120,30,20)
42 draw_rectangle(30,40,-140,0)
43 draw_rectangle(70,10,-170,-20)
44 draw_rectangle(10,70,-170,-30)
45 draw_rectangle(10,70,-170,50)
34/47
Cool Python Codes logo design 3
35/47
Cool Python Codes logo
Yes, it can.
onclick() method
This method calls a function whenever the mouse is used to click on a particular coordinate.
fun– this is a function with two arguments, to which will be assigned the coordinates(x,y) of the clicked point
on the canvas.
btn– this argument is assigned to one by default, which means you click the left mouse button to call a
function. If you assign it to two, you will have to click both the left and right mouse button simultaneously to
call a function.
add– True or False. If True, a new binding will be added, otherwise, it will replace a former binding.
I usually use one argument which is the function I want to call when I click my mouse.
Here is a program that draws a circle when you click on the turtle. Save the code snippet as
click_to_draw_circle.py
Note:
The function name “circle” has two arguments, x and y which are the coordinates you will have to click to draw a
circle.
ondrag() method
This method is basically used when you what to click on the turtle and move it. It accepts three arguments like the
onclick() method.
Python
37/47
I drew a cup.
onrelease() method
This method is also similar to onclick and ondrag method. To understand what this method does,
I wrote a program to draw a circle when you left click on the turtle and when you remove your hand from the mouse,
the circle will disappear.
38/47
Note:
You have to leave your hand on the left-hand button of the mouse until the circle is drawn on the screen.
When the circle has been drawn, you can then remove your hand from your mouse. Once you remove your hand
from the mouse, the circle disappears.
This circle disappears because of the clear() method. The clear() method deletes the turtle’s drawing on the screen.
1. onkey()
2. listen()
onkey()
This method is similar to onclick() method. The only differences is that the onclick() method responds to clicks on the
mouse while the onkey() method responds to commands from your computer keyboard.
listen()
I will avoid using any technical word because I’m as well bored with them. Basically, without including this method,
the turtle won’t obey your commands.
The listen() method is like a cane you use on a child to obey your instruction.
Project 6:
Write a program to control the turtle to move up, down, left and right using the computer keyboard. The turtle should
move constantly to any pixels of your choice.
Open a new file editor and save the following code snippet as turtle_keyboard_control.py
39/47
1 from turtle import Turtle
2 t=Turtle()
3
4 def up():
5 if not(t.heading() == 90):
6 t.setheading(90)
7 t.fd(50)
8 else:
9 t.fd(50)
10
11 def down():
12 if not(t.heading() == 270):
13 t.setheading(270)
14 t.fd(50)
15 else:
16 t.fd(50)
17
18 def right():
19 if not (t.heading() == 0):
20 t.setheading(0)
21 t.fd(50)
22 else:
23 t.fd(50)
24
25 def left():
26 if not (t.heading() ==180):
27 t.setheading(180)
28 t.fd(50)
29 else:
30 t.fd(50)
31
32 def undo_button():
33 t.undo()
34
35 def keyboard_commands():
36 t.screen.onkey(up,"Up")
37 t.screen.onkey(down,"Down")
38 t.screen.onkey(right,"Right")
39 t.screen.onkey(left,"Left")
40 t.screen.onkey(undo_button,"End")
41 t.screen.listen()
42
43 keyboard_commands()
44 t.screen.mainloop()
40/47
Angle 360 or 0 set turtle to face the East
Alright!!!…
Now the “if”, “ else “ and “not” tells the turtle that if it’s not facing the desired direction, that it should change the
direction.
The key commands are “Up”, “Down”, “Left”, “Right” and “End” which are your direction keys and end key on the
keyboard. Note that they all start with capital letters.
stamp() method
This method basically pastes the turtle shape on the canvas on the current turtle’s position.
To illustrate this, open a new file editor and save the following code snippet as turtle_stamp.py
stamp method()
Python
Observing the above screenshot, you will see that the turtle shape appeared 4 times. This makes the concept of for
loop more practical. For every 50pixels to the east, a turtle shape will appear.
position() method:
This method returns the position of the turtle, both the coordinates of the x and y axis is returned.
xcor() method:
41/47
ycor() method:
It will be fun if your name or your friend’s name is on it. Your friends will know that you’re a Pro programmer.
To change the title of the turtle screen, use the method screen.title(). This method only accepts a string.
To illustrate this method, open a new file editor and save the following code snippet as turtle_screen_title.py
Python
42/47
How to change the title of Python turtle.
Here are some amazing drawings I did with Python Turtle including the code snippet.
Try and study the code snippet and do a lot of experiments to come up with new designs.
Circle spiral
Python
43/47
1 from turtle import Turtle
2
3 t=Turtle()
4 t.screen.bgcolor("black")
5 colors=["red","yellow","purple"]
6 t.screen.tracer(0,0)
7
8 for x in range(100):
9 t.circle(x)
10 t.color(colors[x%3])
11 t.left(60)
12
13 t.screen.exitonclick()
14 t.screen.mainloop()
Beautiful Square
Python
44/47
1 from turtle import Turtle
2
3 t=Turtle()
4 t.screen.bgcolor("black")
5 colors=["blue","purple","red","yellow"]
6 t.screen.tracer(0,0)
7
8 for x in range(300):
9 t.color(colors[x%4])
10 t.fd(x)
11 t.left(90)
12
13 t.screen.exitonclick()
14 t.screen.mainloop()
Beautiful Square
Python
45/47
1 from turtle import Turtle
2
3 t=Turtle()
4 t.screen.bgcolor("black")
5 colors=["blue","purple","red","yellow","orange","brown"]
6 t.screen.tracer(0,0)
7
8 for x in range(300):
9 t.color(colors[x%6])
10 t.fd(x)
11 t.left(59)
12
13 t.screen.exitonclick()
14 t.screen.mainloop()
beautiful spiral
46/47
Please if you have any questions or contributions, please leave it in the comment section.
Thanks for reading. If you enjoyed this tutorial, hit the share button. It would mean a lot to me and it helps other
people to see the tutorial.
47/47