- Python Turtle Programming:
- python turtle programming methods and their working.
- change the title and screen color:
- Draw a line with turtle:
- Draw line in backward direction:
- Rotate the turtle:
- Draw a Square with turtle:
- change the position of turtle:
- Draw a star with turtle:
- Draw a polygon with turtle:
- Draw a circle with turtle:
- Draw a dot with turtle:
- Add background image to the screen:
- change the screen size:
- change the pen size:
- increase the turtle size:
- change the pen color:
- Change the Pen Speed:
- Leaving a Stamp:
- Designs using Python Turtle Programming:
- Circle Spiro graph:
- Draw Heart with turtle:
- Related
- Related posts:
Python Turtle Programming:
To work with graphics python provides us with many libraries, one of them is the turtle. Python Turtle Programming lets us control a turtle object which will be used to draw anything on screen as we draw with a pen on the whiteboard. In this case, the turtle object will act as a pen and the screen will act as a drawing board. Now we will use the turtle library to draw different pictures, images, sketches and games graphics.
The turtle is a built-in library so we don’t need to install it separately. We just need to import the library into our code.
After importing the library we can use all functionalities of this library like turtle.forward(…) and turtle.right(…) which can move the turtle around. Following are the most important method of turtle library.
python turtle programming methods and their working.
Method | Parameter | Working |
---|---|---|
Turtle() | None | Creates and returns a new turtle object |
forward() | amount in pixels | Moves the turtle forward by the specified amount in pixels |
backward() | amount in pixels | Moves the turtle backward by the specified amount |
right() | angle | Turns the turtle clockwise |
left() | angle | Turns the turtle counterclockwise |
penup() | None | Picks up the turtle’s Pen |
pendown() | None | Puts down the turtle’s Pen |
up() | None | Picks up the turtle’s Pen |
down() | None | Puts down the turtle’s Pen |
color() | Color name | Changes the color of the turtle’s pen |
fillcolor() | Color name | Changes the color of the turtle will use to fill a polygon |
heading() | None | Returns the current heading |
position() | None | Returns the current position |
goto() | x, y | Move the turtle to position x,y |
begin_fill() | None | Remember the starting point for a filled polygon |
end_fill() | None | Close the polygon and fill with the current fill color |
dot() | None | Leave the dot at the current position |
stamp() | None | Leaves an impression of a turtle shape at the current location |
shape() | shapename | Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’ |
To draw any object on screen first we need to create a screen or window(drawing board) then we run all drawing commands on this window. To create and get the screen object use turtle.Screen(). At the end turtle.done() method is used to complete all work.
import turtle screen = turtle.Screen() turtle.done()
After running the above codes we will get a blank screen.
A little arrow in the middle of the screen is a turtle object. we will control this turtle for drawing. We can change the properties of this screen like color, title etc. further properties will discuss as we go.
change the title and screen color:
screen.title() method is used to define the title of screen and screen.bgcolor(“color name”) method is used to change the background color of the screen.
import turtle screen = turtle.Screen() screen.title("Drawing Board") screen.bgcolor("green") turtle.done()
Draw a line with turtle:
we have learned to create the screen, now we will draw a very simple line on this screen. For this, we will move our turtle(pen) in a forwarding direction or a backward direction etc.
turtle.Turtle() method is used to get the object of a turtle then we will use this object to draw something on the screen. turtle.forward(amount) method is used to move the turtle in the forwarding direction.
import turtle #create the screen screen = turtle.Screen() #specify the title of screen screen.title("Drawing Board") #change the color of screen screen.bgcolor("green") #get the turtle object t=turtle.Turtle() #move turtle forward t.forward(100) turtle.done()
We can also use turtle.fd() method in place of turtle.forward() method. The following code is the same as above.
import turtle #create the screen screen = turtle.Screen() #specify the title of screen screen.title("Drawing Board") #change the color of screen screen.bgcolor("green") #get the turtle object t=turtle.Turtle() #move turtle forward t.fd(100) turtle.done()
Draw line in backward direction:
we can use turtle.backward(distance) or turtle.bk() method to move the turtle in backward direction.
import turtle #create the screen screen = turtle.Screen() #get the turtle object t=turtle.Turtle() #move turtle forward t.backward(100) turtle.done()
Rotate the turtle:
We can rotate the pen(turtle) by right() or left() method.
import turtle t = turtle.Turtle() t.right(90) turtle.done()
Draw a Square with turtle:
We will use turtle.forward() and turtle.right() method to draw a square. Look at the following code.
import turtle t = turtle.Turtle() #Draw 4 sides for squar for i in range(4): t.forward(100) t.right(90) turtle.done()
We have used the for loop in the above code. You can read more about for loop in python here.
change the position of turtle:
We can also change the position of the turtle by goto(x, y=None) or turtle.setpos(x, y=None) turtle.setposition(x, y=None). After changing the position all drawings will start from that position.
import turtle #Create first pen t = turtle.Turtle() # Move turtle with coordinates t.goto(50, 50) turtle.done()
import turtle #Create first pen t = turtle.Turtle() # Move turtle with coordinates t.setpos(50, 50) turtle.done()
import turtle #Create first pen t = turtle.Turtle() # Move turtle with coordinates t.setposition(50, 50) turtle.done()
Draw a star with turtle:
import turtle star = turtle.Turtle() star.right(75) star.forward(100) for i in range(4): star.right(144) star.forward(100) turtle.done()
Draw a polygon with turtle:
#. Draw a polygon with turtle. import turtle #create turtle object t=turtle.Turtle() #decide the number of side Num_of_sides=8 #draw sides by forward() and right() method for i in range(Num_of_sides): angle=360/Num_of_sides t.forward(100) t.right(angle) turtle.done()
Draw a circle with turtle:
We have used movement methods to draw the different shapes with turtles but the turtle library has some predefined shapes like a circle. we will use turtle.circle(radius, extent = None, steps = an integer).
The circle is drawn with the given radius. The extent determines which part of the circle is drawn and if the extent is not provided or none, then draw the entire circle.
import turtle t = turtle.Turtle() #Draw circle with radius=60 t.circle(60) turtle.done()
Draw a dot with turtle:
Just like circle() method we have a turtle.dot() method to draw a filled dot.
import turtle t = turtle.Turtle() #Draw a dot t.dot(50) turtle.done()
Add background image to the screen:
We have already changed the color of the screen, now we will change the background of the screen with an image. For this, we will use turtle.bgpic() method. This method takes the path of the image.
import turtle t = turtle.Screen() #change the background t.bgpic('E:\ExampleImage.png') turtle.done()
change the screen size:
We can also change the screen size for drawing by using turtle.screensize(canvaswidth = None, canvasheight = None, bg = None)
import turtle # Creating turtle object t = turtle.Turtle() turtle.screensize() #change the screen size turtle.screensize(50,2000) turtle.screensize() turtle.mainloop()
change the pen size:
we can change the pen size by using turtle.pensize(new size) method.
import turtle t = turtle.Turtle() #change the pen size t.pensize(5) #move forward by 100 pixels t.forward(100) turtle.done()
increase the turtle size:
turtle.shapezie(x,y) method is used to change the size of turtle.
import turtle t = turtle.Turtle() #change the turtle size t.shapesize(20,20) turtle.done()
change the pen color:
we can change the color of pen and the color of the pen’s outline.
- turtle.fillcolor(color name) method is used to change the color of turtle.
- turtle.pencolor(color name) method is used to chnage the color of pen’s outline.
import turtle t = turtle.Turtle() # Increase the turtle size t.shapesize(5,5) # fill the color t.fillcolor("blue") # Change the pen color t.pencolor("yellow") turtle.done()
Change the Pen Speed:
you can also control the speed of the turtle by turtle.speed() method.
import turtle t = turtle.Turtle() t.speed(2) t.forward(100) t.speed(5) t.forward(100) turtle.done()
Leaving a Stamp:
We can leave the stamp of the turtle on the screen. The stamp is nothing but an imprint of the turtle.
import turtle t = turtle.Turtle() #Get the stamp object t.stamp() #move forward t.fd(200) t.stamp() t.fd(100) turtle.done()
Designs using Python Turtle Programming:
Circle Spiro graph:
import turtle # Creating turtle object t = turtle.Turtle() turtle.bgcolor("black") turtle.pensize(2) turtle.speed(0) while (True): for i in range(6): for colors in ["red", "blue", "magenta", "green", "yellow", "white"]: turtle.color(colors) turtle.circle(100) turtle.left(10) turtle.hideturtle() turtle.mainloop()
Draw Heart with turtle:
import turtle # Creating turtle t = turtle.Turtle() s = turtle.Screen() s.bgcolor("black") turtle.pensize(2) # To design curve def curve(): for i in range(200): t.right(1) t.forward(1) t. speed(3) t.color("red", "pink") t.begin_fill() t.left(140) t.forward(111.65) curve() t.left(120) curve() t.forward(111.65) t.end_fill() t.hideturtle() turtle.mainloop()