Нарисуйте сердце, используя графику черепахи на Python

Опубликовано: 3 Апреля, 2022

Turtle - это встроенный модуль в Python. Это обеспечивает:

  1. Рисование с помощью ширмы (картона).
  2. Черепаха (ручка).

Чтобы нарисовать что-то на экране, нам нужно переместить черепаху (ручку) и переместить черепаху, есть некоторые функции, такие как forward (), backward () и т. Д.

Предварительное условие: основы программирования черепах

Нарисуйте сердце, используя графику черепахи

В этом разделе мы обсудим, как нарисовать Сердце с помощью графики черепахи.

Подход:

  1. Import Turtle
  2. Make Turtle Object
  3. Define a method to draw a curve with simple forward and left moves
  4. Define a method to draw the full heart and fill the red color in it
  5. Define a method to display some text by setting position
  6. Call all the methods in main section.

Code:

python3

# Import turtle package
import turtle
  
# Creating a turtle object(pen)
pen = turtle.Turtle()
  
# Defining a method to draw curve
def curve():
    for i in range(200):
  
        # Defining step by step curve motion
        pen.right(1)
        pen.forward(1)
  
# Defining method to draw a full heart
def heart():
  
    # Set the fill color to red
    pen.fillcolor("red")
  
    # Start filling the color
    pen.begin_fill()
  
    # Draw the left line
    pen.left(140)
    pen.forward(113)
  
    # Draw the left curve
    curve()
    pen.left(120)
  
    # Draw the right curve
    curve()
  
    # Draw the right line
    pen.forward(112)
  
    # Ending the filling of the color
    pen.end_fill()
  
# Defining method to write text
def txt():
  
    # Move turtle to air
    pen.up()
  
    # Move turtle to a given position
    pen.setpos(-68, 95)
  
    # Move the turtle to the ground
    pen.down()
  
    # Set the text color to lightgreen
    pen.color("lightgreen")
  
    # Write the specified text in 
    # specified font style and size
    pen.write("GeeksForGeeks", font=(
      "Verdana", 12, "bold"))
  
  
# Draw a heart
heart()
  
# Write text
txt()
  
# To hide turtle
pen.ht()

Выход:

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.