Confess your love turtle – draw a heart in a Python

A quick Valentine’s day entry, for those who have a moment to poke around and are in the mood to meet a turtle.

What is a turtle / python turtle?

Turtle this is a python library that is already included in the standard libraries that are available to us as soon as Python 3 is installed. Allows you to draw shapes, create simple images or games, so it is often used for learning programming for children.

Graphics support IS module-based Tkintercode> (python-tk must be installed when you install Python td/tk checkbox or can be reinstalled later pip install tk).

The pointer that moves and draws on the screen is called a turtle.

Turtle has 3 attributes
– Place (location),
– orientation (direction of movement)
– and turtle / pen, I call it pointer or arrow for convenience.

Turtle can move in 4 directions: right, left, straight and backward and such Also we have methods: right(), left, forward(), backward().

Let’s look at the code below. We start, of course, with the import of the library and call subsequent functions.

turtle import

turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.backward(50)
turtle.done()

  • Functions forward() i backward() they take the distance to travel as a numerical value.
  • Functions right() i left() they also take a numerical value, but this is the angle at which the turtle / feather should rotate.

turtle import

turtle.circle(100)
turtle.done()

circle() – allows you to draw the shape of the wheel, the value in parentheses is the radius of the wheel.
Test also:
turtle.circle(100, 180),
turtle.circle(100, 120)
– the first value is the radius, the second is the angle / segment of the wheel that we want to draw.

That’s all the information we need to draw a heart in Python.

Heart using Turtle module

Today we will draw a heart with a turtle.

I don’t want to write every time turtle.nazwa_metody(). therefore, otherwise I will import the library:
from turtle import *.

Let’s start with the following code:

from turtle import *

left(40)
forward(60)
circle(30, 200)
left(240)
circle(30, 200)
forward(60)
done()

heart python turtle contour

Why don’t we color the heart?

from turtle import *

color (“red”, “pink”)
begin_fill()
left(40)
forward(60)
circle(30, 200)
left(240)
circle(30, 200)
forward(60)
end_fill()
done()

python turtle heart filled

Bold lines (pensize()), plus let’s hide the tortoise-pointer (hideturtle()).

from turtle import *

pensize(3)
color (“red”, “pink”)
begin_fill()
left(40)
forward(60)
circle(30, 200)
left(240)
circle(30, 200)
forward(60)
end_fill()
hideturtle()
done()

turtle python's heart is finished

For those interested:

Turtle (just like Python in general) allows us to use programming procedural, as well as object.

Compare:

turtle import

turtle.forward(100)
turtle.done()

All methods from the Turtle library classes are available to us as functions and are called automatically. The default turtle object is created in the same way. However, we can create it openly:

turtle import

t = turtle.Turtle()
t. forward(100)
turtle.done()

Turtle() – he creates and returns us a new turtle object.
In addition, we can change the shape of the turtle to … turtle instructions shape("turtle") (other shapes are e.g. circle, square, arrow )

turtle import
t = turtle.Turtle()
t. shape (‘turtle’)
t. forward(100)
turtle.done()

Unity 3D Development Outsourcing | IT Outsource Support

Ready to see us in action:

More To Explore

IWanta.tech
Logo
Enable registration in settings - general
Have any project in mind?

Contact us:

small_c_popup.png