Basics of Python Turtle module5th September 2019
- Python is less complicated than some other popular text based programming languages like Java or C++. Yet, Python is powerful enough that it is used by companies such as Google, Yahoo, Red Hat, and more. Many universities use Python in their first computing course.
- Python have so many modules are available and Turtle is one of among that module and this Python Turtle is built in module in python.
- Python turtle is basically 2D designing module, by using this module we can make so many different types of designs.
- It is used to create basic 2D shapes and drawings and is extremely beginner friendly.
- The main advantages of turtle is that it is extremely simple and makes it very easy to draw things to the screen. That being said the turtle module does offer support for some more advanced functionality too.
- Imagine that it is a robotic turtle starting at (0, 0) in the x-y plane.
- Meaning is that your turtle is center of your screen and now you can move your turtle as you want in your design.
- To move this turtle you need to programe that turtle that at which time in which direction how many distance it need to move.
- If you knowing some basic maths like Trigonometry and Geometry it will be easy for you to design your model.
Starting with Python Turtle module
- First you need to import the turtle in your python programe.
1. Methods of Turtle
- After importing turtle module you can define a variable to give name your turtle, here i am giving pen to my turtle
pen = turtle.Turtle()
- Now i can use turtle as pen in my whole programe.
Move Forward or Backward
- At starting of your programe your turtle is situated as (0,0) coordinates and his head is directed towards 0 degree.
- Now let’s see how to move your pen to draw a line so for that turtle has function forward() or fd(). (you can use any one).
- So basically to move the turtle forward by the specified distance forward(distance)
- Here distance is in form of pixels
- I am going to move my pen to 100 pixels in forward directioon
- Now to move backward you can use backward(), bk() or back()
- Rest of things for backward is smae as forward function.
Angle of turtle
- Now your turtle is directing towards 0 degree in (x,y) plan, you can see it in below picture.
- Now, you can change the angle of the turtle in (x,y) plan. To do that there is a command in python turtle that is setheading(angle)
- Here i am using angle 90 you can choose any angle you want from 0 ,1,2,3…..degree to 359 degree (because 360 degree again it consider as 0 degree) in (x,y) plan.
- 0 degree will direct your turtle in east direction, 90 degree will direct your turtle in north direction, 180 degree will direct your turtle in west direction and 270 degree will direct your turtle in south direction (accordingly you can manage your turtle from 1,2,3,….degree to 359 degree).
- Besides that Python Turtle has one more command to change the angle of turtle that is rt(angle) or right(angle) to turn your turtle angle in right side and lt(angle) and left(angle) to turn the angle of turtle in left direction.
- This command is same as previous command that is setting of head of turtle. You can choose any of one.
- Now, let’s take one example to clear all above things.
- Here we are going to draw a square using Python Turtle
pen = turtle.Turtle()
for i in range(4): pen.fd(100) pen.rt(90)
- As you will write above code, this will able to draw a square.
- Here first we define out turtle as pen and than we first move our pen in forward direction with the distance of 100 pixel and than we turn our turtle head 90 degree in right direction.
- We will doing same thing repeatedly for 4 times to make a square.
No drawing when moving the turtle
- In many cases you want to move turtle from one point to another point at that time this function will helpful to you
- To pll the pen up python turtle has function called penup() or up() or pu() and pendown() or down() or pd()
- When you want to move turtle without drawing you nedd to write pen.penup() or pen.up() and after turtle reaching at particular place you nedd to write pen.down() or pen.pendown()
#Move your turtle where ever you want
- Let’s take an example.
pen = turtle.Turtle()
- In above example, we penup out turtle and move 250 pixel in forward direction and then we pendown our turtle.
- So between (0,0) to (250,0) there in no drawing
Color of turtle
- At the starting by default the color of the turtle is black, so you can change the color of your turtle. There is command to change color of turtle that is color(“color name”) or color(*argu)
- NOTE: before using color function you need to write
- Here in color function you can write either number of color, color name, or color(r,g,b) mixer or you can write hex value of color as shown below.
color(100, 11, 190) or color('white') or color('red', 'green') or
color('green' 'blue') or color('#285078')
- By using any of above you change the color of turtle.
Move turtle to an absolute position
- If in your code you want to set your turtle at a particular position, say you want to set your turtle at (30,50) in (x,y) plan.
- So doing it by forward and angle function it is time consuming so python turtle has function called goto(x,y) or setposition() to move your turtle any where in x-y- plan
pen.goto(30,50) #This will set your turtle at x=30 and =50
- Even you can use negative sign to move turtle in backward direction.
pen.goto(-30, -50) #This will set your turtle at x = -30 and y =
Fill color in object or your design
- Now question is if we are using turtle.color() function, it just change the color of our drawing line or say outer line of our design or object, so how to color up inside the our design ?
- So for that python turtle has function begin_fill() & end_fill() this both function used jointly means we can not use any of above function separately.
- Before using this function we need to define color which we want to fill in our design using color(“blue”).
#Your object or design code
- Let’s take example to make square and fill it with yellow color for better understanding.
import turtle pen = turtle.turtle() pen.color("yellow") begin_fill() for i in range(4): pen.fd(100) pen.rt(90) end_fill()
- Here in above example we take example 1 and fill it with yellow color.
Increase the width of drawing line(turtle pen)
- In python turtle we can also varies the width of the drawing line or turtle pen width with using function pensize()
- Here as shown above i increase my pen width with 5 pixel
Increase the drawing speed of turtle
- It may possible that your design is too complicated and large size at that time you might want to increase the drawing speed of turtle.
- You can increase the speed of turtle by using function speed()
pen.speed(0) #For fastest speed, pen.speed(1) #For lowest speed, pen.speed(3) #For slow speed, pen.speed(6) #For Normal speed, pen.speed(10) #For fast speed
Hide turtle or Show turtle
- Now after completing your design or anytime in your programe if you want to hide your turtle there is function in python called hideturtle() or ht()
- In the same way you can aging see your turtle by function called st() or showturtle()
2. Methods of TurtleScreen/Screen
- A screen object is automatically created whenever a function derived from a Screen method is called. An turtle object is automatically created whenever any of the functions derived from a Turtle method is called. To use multiple turtles an ascreen one has to use the object-oriented interface.
- It return the singleton screen object
- You can define your screen as we defined our turtle earlier
wn = turtle.Screen()
- Here i defined my turtle screen as wn, you can choose other name too.
Change the background color
- If you wish to change the background color of your turtle screnn you can do it by using function bgcolor()
- Here i changed my background color by using color name from white to orange, you can choose your color as we seen in color method of turtle earlier like writing hex number of color or by writing R,G,B color.
Add background picture
- Now instead of changing background color, you want to add background picture so you can add by calling function bgpic()
- Picture that you want to add as background picture in your programe that must be in same folder where you saved python programe.
- Picture that you want to add must be .gif file.
- If your picture is not in .gif format you can simply open that picture with paint and save as it in .gif format.
- Now if you want to remove that background picture write.
- It will remove your background picture
Delete all drawing
- If you want to clear your drawing or you can simply say you can delete your drawing by calling the function called clear() or clearscreen()
Set your screen size or window size
- Now you can set your windown size by call function setup()
wn.setup(width=200, height=300) #I set width = 200 and height =
300 of my output screen
Give name to your screen
- By calling function title() you can simply give the name of your output window
Here we saw some basic concept of Python turtle, python turtle is not just this, it is a world of drawing if you have logic you can make so many different type of designs even you can make games.
If you are good in Basic maths you can easily makes thousands of design
We have some python turtle examples to understand better all things
Real time analog clock :