DIY -: Using turtle graphics, I drew a heart containing “I love DIYhub community” - Programming tutorials EP09

avatar

Greetings my friends,

Ramadan Kareem to you all, i just broke my fast today and i wanted to share with you another epsiode of my programming with python. About two days ago i shared a post about how you can easily create a contact book where you can add, view and delete a contact. Some people found it very helpful and educational. And I appreciate that. Today i will be doing something completely different. I’ve kindda done something similar.

In python we have something called turtle graphics. Well this allows programmers and coders to visually display the output of something. Like say a crescent moon. You can use a few lines of code to draw a cresent moon. This is made possible by the turtle graphics. So today i will be drawing a heart ❤️ and embed the heart with the phrase ”I love DIYhub Community. Without wasting much of your time lets head on to it.

To begin the code, since we will be using the turtle graphics, we need to import the function turtle. If you are using a very old version of Python, you might not have the turtle function inbuilt in your device. You would have to pip install that function. I guess i would have to teach you how to pip install any function in our next episode. Now back to the matter, after calling the turtle function, we go ahead to define two functions t and s. Where t = turtle.Turtle() and s = turtle. Screen ().

The next step is to fill the whole background as black. How do we do this?, well from our defiend functions above, we can set s which is screen to color black. t.goto contains some numbers that is instructing the line to start drawing from the y axis because the x is set to 0.

Now we have all but laid down the foundation of the code. Its now time to draw the main code. Which is the heart. First we need to determine the size, color, and many other features we will be using to draw the heart.

turtle.pensize is set to 3, a quite moderate size if you ask me. Depending on how big you want it, you can change the number. Next up is the color, t.color(red) means that we want our heart to be red which is quite obvious. Now t.begin_fill() will start filling the color red while drawing from the left moving forward and Will kinda repeat that for the other side. This is because we used t.left, t.forward and t.circle to instruct it on what to do.

Now at thid stage when we run the code, this is the output we get;

So basically from the above code, you can see that the heart is ready. If i was just drawing a heart alone we would have left the code at that. But now we have to engrave the words I love DIYhub community. To do that, i want to write these words in the middle so i will use the t.goto() to achieve this.

Now i can decide on the color i want the writing to be. I choose white and then i will set t.color to white. And now use t.write(I love DIY community) added the font arial to size 10. And ended the code with turtle.done().

We run the code again and we get;

As you can see a heart containing i love diyhub community. Thats al for now guys. See you on the next episode.



0
0
0.000
7 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000
avatar

Coool, thanks for loving DIYhub community.

0
0
0.000
avatar

Thank you for your support.

0
0
0.000
avatar

You're welcome. Keep up the great work 😊

0
0
0.000