Python Graphics !!
In this article we will discuss about introducing graphics in python with the help of turtle module.
Introduction :
With the help of graphics module turtle we can able to make any design or any geometry.
Turtle Module :
This module is specially destined to introduce graphics in the programming so user can perform graphical programs it includes all the functions of the drawing and so user can make desired graphical design .
Basic Functions :
Draw and Move Functions :
- forward() or fd()
It is used for moving the turtle forward.
Ex- forward(50) , fd(50)
- backward() or bk() or back()
It is used for moving the turtle backward.
Ex- backward(50) , bk(50), back(50)
- right() or rt()
It is used to turn the turtle right.
Ex- right(90) , rt(90)
It is used to turn the turtle left.
- left() or lt()
It is used to turn the turtle left.
Ex- left(90) , lt(90)
It is used to set the position of the turtle.
we have to give two arguments to the function.
(x,y) value of both x and y,
Ex- setposition(10,20) , setpos(20,-10)
- setposition() or setpos()
It is used to set the position of the turtle.
we have to give two arguments to the function.
(x,y) value of both x and y,
Ex- setposition(10,20) , setpos(20,-10)
- goto()
It is used to send the turtle to the given coordinates.
we have to pass the value of X and Y.
Ex- goto(10,5)
- setx()
It is used to set the position of the x.
Ex- setx(4)
- sety()
It is used to set the position of the y.
Ex- sety(8)
- home()
It is used to send the turtle to the origin(0,0).
Ex - home()
- dot()
It is used to create the dot of given arguments.
we have to give two argument size and color of the dot.
Ex-dot(10,"red")
- speed()
it is used for setting the speed of the turtle.
0- highest speed of the turtle.
10 - high speed of the turtle.
1- slowest speed of the turtle.
Ex- speed(0)
Tell State Of Turtle :
- posittion() or pos()
It returns the coordinates of the turtle.
Ex- position() or pos()
>>(1,2,1).
- xcor()
It returns the value of x coordinate.
Ex- xcor()
>>10.
- ycor()
It returns the value of the y coordinate.
Ex-ycor()
>>5.
- distance()
It returns the distance of the coordinate from the origin.
Ex- distance(6,8)
>>10
- xcor()
It returns the value of x coordinate.
- ycor()
It returns the value of the y coordinate.
- distance()
It returns the distance of the coordinate from the origin.
Sample Programs Using Basic Functions
- Program to draw a square
import turtle
for i in range (4):
turtle.fd(100)
turtle.rt(90)
turtle.mainloop()
- Output
- Program to make a circle
import turtle
for i in range (360):
turtle.fd(2)
turtle.rt(1)
turtle.mainloop()
- Output
- Program to make polygon of n side
import turtle
n=int(input("enter no of side "))
a =((n-2)*180)/n
for i in range (n):
turtle.fd(50)
turtle.lt(180-a)
turtle.mainloop()
- Outputs
With input of 3
With input of 5
With input of 8
- Program to draw the circle of desired radius with circle
import turtle
n=int(input("enter the radius "))
turtle.circle(n) #circle with radius n
turtle.pu() #pen up
turtle.setpos(turtle.xcor(),turtle.ycor()+n) #to set the center of the circle
turtle.pd() #pen up
turtle.dot(20,"blue") #for center
turtle.mainloop()
- Output