Header Ads

How do you draw a turtle heart in Python?

We will draw a heart using python using turtle library. Turtle is a pre-installed Python library that enables users to create pictures and shapes by providing them with a virtual canvas. The onscreen pen that you use for drawing is called the turtle and this is what gives the library its name. Let's start the program by importing turtle.

Code :


from turtle import *
color("red")
begin_fill()
pensize(3)
left(50)
forward(133)
circle(50,200)
right(140)
circle(50,200)
forward(133)
end_fill()
done()


Output :




Explanation :

First import turtle module
Then implement the following functions :

color()
Return the current pencolor and the current fillcolor as a pair of color specification strings or tuples as returned by pencolor() and fillcolor().

begin_fill()
To be called just before drawing a shape to be filled.

pensize()
Set the line thickness to width or return it. If resizemode is set to “auto” and turtleshape is a polygon, that polygon is drawn with the same line thickness. If no argument is given, the current pensize is returned.

left()
move the turtle left

forward()
Move the turtle forward by the specified distance, in the direction the turtle is headed.

circle()
Draw a circle with given radius. The center is radius units left of the turtle; extent – an angle – determines which part of the circle is drawn. If extent is not given, draw the entire circle. If extent is not a full circle, one endpoint of the arc is the current pen position. Draw the arc in counterclockwise direction if radius is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of extent.

right()
moves the turtle towards right

end_fill()
It completes the filling process

done()
Starts event loop - calling Tkinter’s mainloop function. Must be the last statement in a turtle graphics program.



Post a Comment

0 Comments