Drawing a heart in Python

How to draw a heart in Python using Turtle framework

Photo by Nicola Fioravanti on Unsplash

Welcome back, fellow developers! 💻

Here in this brief story, I want to share you a little project that could help you to love Python as I do. This isn’t a tutorial for beginners or advanced developers, this is just a project for everyone.

If you want the source code you can find it on Git-Hub at this link.

As always, if you haven’t signed up yet to my Medium profile, well it’s time to do it. In this profile I love to share with you all my love for coding and programming and maybe you can find something useful here.

If you are interested only about how I drawn the heart you can go to directly to the Draw a heart project section.

Now it’s time for this brief story, good reading! 😜

Turtle Graphics Framework

Accordingly to Python’s documentation :

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.

Indeed, thanks to Turtle Graphics and obviously to Turtle module, programmers can create beautiful shapes ny simply using its methods. It’s a very fascinating way to approach to Python and to computer programming in general.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.

By combining together these and similar commands, intricate shapes and pictures can easily be drawn.

The turtle module is an extended reimplementation of the same-named module from the Python standard distribution up to version Python 2.5. It tries to keep the merits of the old turtle module and to be (nearly) 100% compatible with it.

Framework overview

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

The object-oriented interface uses essentially two+two classes:

  1. The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application. The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible. All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-oriented interface.
  2. RawTurtle (alias: RawPen) defines Turtle objects which draw on a TurtleScreen. Its constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the RawTurtle objects know where to draw. Derived from RawTurtle is the subclass Turtle (alias: Pen), which draws on “the” Screen instance which is automatically created, if not already present. All methods of RawTurtle/Turtle also exist as functions, i.e. part of the procedure-oriented interface.

The procedural interface provides functions which are derived from the methods of the classes Screen and Turtle. They have the same names as the corresponding methods. A screen object is automatically created whenever a function derived from a Screen method is called. An (unnamed) turtle object is automatically created whenever any of the functions derived from a Turtle method is called.

To use multiple turtles on a screen one has to use the object-oriented interface.

Drawing using Turtle

To make use of the turtle methods and functionalities, we need to import turtle.”turtle” comes packed with the standard Python package and need not be installed externally. The roadmap for executing a turtle program follows 4 steps:

  1. Import the turtle module
  2. Create a turtle to control
  3. Draw around using the turtle methods
  4. Run turtle.done()

So as stated above, before we can use turtle, we need to import it. We import it as :

After importing the turtle library and making all the turtle functionalities available to us, we need to create a new drawing board (window) and a turtle. Let’s call the window as window and the turtle as my_turtle (because we have a lot of fantasy). So we code as:

Now that we have created the window and the turtle, we need to move the turtle. To move forward 100 pixels in the direction my_turtle is facing, we code:

We have moved my_turtle 100 pixels forward, Awesome! Now we complete the program with the done() function and We’re done!

So, we have created a program that draws a line 100 pixels long. We can draw various shapes and fill different colors using turtle methods. There’s plethora of functions and programs to be coded using the turtle library in python.

Draw a heart project

The project is very easy and very fun to do on your own. It allows you to use a lot of methods from Turtle framwork. There is just one functions that I wrote to effectivly draw the heart on the screen. For the rest, they are all within the framework.

In pratice: the project draw a heart on a black screen and coloring in pink the heart itself.

The reasoning behind the project is very basic. Since the heart is a simmetric shape I’ve decided to split the code in two parts each one divided by the draw() function. In this way I can do the same exact thing, that I've done on the left, on the right.

The code

After importing the turtle framework, I’ve started to give some informations about the speed of drawing, the background color and the size of the mark on the board:

Then I created my loop to give instructions to the turtle pen about where to go and embedded all in function called draw()

In the end I set the color of the pen and the color of the shape

After all of this I’ve just put together all the turtle’s methods that suited best for my homework

Note: As you can notice the draw() function is written two times, this because the heart that I drawn is a symmetric shape and you can do the same thing on both side, the left side and the right side.

Thanks for reading. 🥳🥳🥳

Complete code

Here the complete code




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store