Python Turtle
The Python turtle library consists of all important methods and functions that we will need to create our designs and images. Import the turtle library using the following command. |
import turtle
We can access all methods and functions. First, we need to create a dedicated window where we carry out each drawing command. We can do it by initializing a variable for it.
s=turtle.getscreen()
Example:
import turtle
s=turtle.getscreen() #Creating Turtle Screen
turtle.mainloop() #To stop the screen to display
Output:
Turtle Motion:
The turtle can move forward and backward in direction that it's facing. Let's see the following functions.
- forward(disatnce) or turtle.fd(distance) - It moves the turtle in the forward direction by a certain distance. It takes one parameter distance, which can be an integer or float.
import turtle
s=turtle.Turtle() #Creating Turtle Screen
s.forward(100)
turtle.mainloop() #To stop the screen to display
- back(disatnce) or turtle.bk() or turtle.backward(distance) - This method moves the turtle in the opposite direction the turtle is headed. It doesn't change the turtle heading.
import turtle
s=turtle.Turtle() #Creating Turtle Screen
s.backward(100) #Move turtle in opposite direction
turtle.mainloop() #To stop the screen to display
- right(angle) or turtle.rt(angle) - This method moves the turtle right by angle units.
import turtle
s=turtle.Turtle() #Creating Turtle Screen
s.heading()
s.right(25) #Move turtle in right
s.heading()
turtle.mainloop() #To stop the screen to display
- left(angle) or turtle.lt(angle) - This method turn the turtle left by angle units.
import turtle
s=turtle.Turtle() #Creating Turtle Screen
s.heading()
s.leftt(100) #Move turtle in left
s.heading()
turtle.mainloop() #To stop the screen to display
- goto(x,y=None) or turtle.setpos(x,y=None) or turtle.setposition(x,y=None) - This method is used to move the turtle in the other area on the screen. It takes the two coordinates - x and y.
import turtle
s=turtle.Turtle() #Creating Turtle Screen
s.goto(100,80) #Move turtle with coordinates
turtle.mainloop() #To stop the screen to display
import turtle
s=turtle.Turtle()
s.fd(100)
s.rt(90)
s.fd(100)
s.rt(90)
s.fd(100)
s.rt(90)
s.fd(100)
turtle.mainloop()
Output:
- circle(radius,extent=None,steps=an Integer) - It is used to draw the circle to the screen. It takes three arguments:
2. extent - It can be a number or none.
3. steps - It can be an integer.
The circle is drawn with the given radius. The extent determines which part of circle is drawn and if the extent is not provided or none, then draw the entire circle.
Example Program:
import turtle
s=turtle.Turtle()
s.circle(50)
turtle.mainloop()
Output:
Changing the Screen Color:
By default, the turtle screen is opened with the white background. However, we can modify the background color of the screen using the following function.
Example:
import turtle
import turtle
s=turtle.Turtle()
turtle.bgcolor('red')
turtle.mainloop()
Changing the Screen Title:
By default, it shows the Python tutorial graphics. We can make it personal such as "My First Turtle Program" or "Drawing Shape with Python". We can change the title of the screen using the following function.
turtle.title("Your Title")
Example:
import turtle
import turtle
s=turtle.Turtle()
turtle.title("My Turtle Program")
turtle.mainloop()
Changing the Pen Size:
We can increase or decrease the turtle's size according the requirement. Sometimes, we need thickness in the pen.
Example:
import turtle
import turtle
t=turtle.Turtle()
t.pensize(4)
t.forward(200)
turtle.mainloop()
Changing the Pen Speed:
The speed of the turtle can be changed. Generally, it moves at a moderate sped over the screen but we can increase and decrease its speed.
The turtle speed can vary integer values in the range 0…10. No argument is passed in the speed() function, it returns the current speed. Speed strings are mapped to speed values as follows.
0 - Fastest
10 - Fast
6 - Normal
3 - Slow
1 - Slowest
Example:
turtle.speed(6)
turtle.speed(6)
Customization in one line:
Suppose we want multiple changes within the turtle; we can do it by using just one line. Below are a few characteristics of the turtle.
- The pen color should be red.
- The fill color should be orange.
- The pen size should be 10.
- The pen speed should 7
- The background color should be blue.
Example Program:
import turtle
t=turtle.Turtle()
t.pencolor("red")
t.fillcolor("orange")
t.pensize(10)
t.speed(7)
t.begin_fill()
t.circle(75)
turtle.bgcolor("blue")
t.end_fill()
turtle.mainloop()
Change the Pen Direction:
By default, the turtle points to the right on the screen. Sometimes, we require moving the turtle to the other side of the screen itself. To accomplish this, we can use the penup() method. The pendown() function uses to start drawing again.
Example Program:
import turtle
t=turtle.Turtle()
t.fd(100)
t.rt(90)
t.penup()
t.fd(100)
t.rt(90)
t.pendown()
t.fd(100)
t.rt(90)
t.penup()
t.fd(100)
t.pendown()
turtle.mainloop()
If you have any doubts, feel free to post it in comment Section.
Comments