Monday, March 18, 2024

Draw a Graphical Heart Program in Python

 Below is a Python code using the turtle module to draw a heart shape graphically:


import turtle


# Set up the screen

screen = turtle.Screen()

screen.setup(width=600, height=600)

screen.bgcolor("white")


# Create a turtle object

heart = turtle.Turtle()

heart.speed(0)

heart.color("red")

heart.fillcolor("red")

heart.begin_fill()


# Draw the heart shape

heart.left(140)

heart.forward(180)

for i in range(200):

    heart.right(1)

    heart.forward(2)

heart.left(120)

for i in range(200):

    heart.right(1)

    heart.forward(2)

heart.forward(180)


# Fill the heart with color

heart.end_fill()


# Hide the turtle

heart.hideturtle()


# Keep the window open

turtle.done()



===========
When you run this code, a window will appear displaying a graphical representation of a heart shape drawn using the turtle graphics library in Python. You can adjust the window size and heart shape by modifying the parameters in the screen.setup() function and the heart drawing commands.

No comments:

Post a Comment