In this Blog article, we will learn how to Create a heart with Turtle, we will see the implementation in Python.
Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!
You can refer to my YouTube video tutorial for better Understanding:
What will be covered in this Blog:
1. Turtle Introduction
2. Creating a heart with Turtle
What is Turtle?
Turtle
is a pre-installed Python library. It 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 as turtle.
The turtle has three attributes: a location, an orientation (or direction), and a pen.
Moving the Turtle Head
The turtle can move in four directions:
- Forward
- Backward
- Left
- Right
If you wish to know more about it, you can refer to Turtle Documentation. Use this link to navigate to the documentation.
Now that you are aware of Turtle basics, we can move forward to the coding section.
Time to code!
You can find all the code at my GitHub Repository. Drop a star if you find it useful.
In order to access the Python library, you need to import it into your Python environment, use the following command to import turtle
it in your python script.
import turtle
Now let's define some properties,
- Let's set the speed as
3
usingspeed
method, that means the heart will not just appear on the screen, the drawing will have some animation. - If you wish to change the background color, you can use the
bgcolor
method, by default it is white. - You can adjust the pen's thickness using
pensize
method, it will be slightly bold.
turtle.speed(3)
#turtle.bgcolor("black")
turtle.pensize(3)
Now let's define a function to define the curve, I am calling it as myfunc
.
def curve():
for i in range(200):
turtle.right(1)
turtle.forward(1)
turtle.color("black", "red")
turtle.begin_fill()
turtle.left(140)
turtle.forward(111.65)
curve()
I will define a loop here for the first curve, we will set the direction right and move forward. Now let's set the turtle
color using the color
method
- the first parameter will be the border color, the pen color, which we are passing as
black
- the second parameter,
red
is the color used to fill inside our heart.
And once we have set the color, we can begin the fill using begin_fill
method.
we call left
and forward
and finally call our function to get the first curve. It will look something like this.
turtle.left(120)
In order to change the pen
's direction, use left
method.
curve()
Now let's call the curve function again.
turtle.forward(111.65)
turtle.end_fill()
Let's make it touch the starting point and complete our heart. We will make use of forward
method here. Once our heart is completed, we can now use end_fill
to fill color in our heart.
If you closely observed you can see the pen head at the end. We will use hideturtle
method to get red of it.
turtle.hideturtle()
turtle.done()
done
method is used to hold the output Turtle screen.
This is all about Creating a heart with Turtle. That's it! simple, isn't it? Hope this tutorial has helped.
You can find all the code at my GitHub Repository. Drop a star if you find it useful.
Thank you for reading, I would love to connect with you at Twitter.
Do share your valuable feedback and suggestions!
You should definitely check out my other Blogs:
- Python 3.9: All You need to know
- The Ultimate Python Resource hub
- GitHub CLI 1.0: All you need to know
- Become a Better Programmer
- How to make your own Google Chrome Extension
- Create your own Audiobook from any pdf with Python
- You are Important & so is your Mental Health!
Resources:
See you in my next Blog article, Take care