Monday, December 13, 2021

Python / Turtle: draw a heart


Code:

# importing package:allows us to use the turtles library
import turtle
# make screen object
sc = turtle.Screen()
# and set size
sc.setup(400,400)
#create a turtle named t
t=turtle.Turtle()
#Picks up the turtle’s Pen
t.penup()
#Move the turtle to position x,y
t.goto(0,-100)
#Puts down the turtle’s Pen
t.pendown()
# Changes the color of the turtle’s pen
t.color("red")
# Changes the color of the turtle will use to fill a polygon
#t.fillcolor('red')
#Remember the starting point for a filled polygon
t.begin_fill()
#Turns the turtle counterclockwise
t.left(50)
#tell turtle to move forward by 120 units
t.forward(120)
# draw a semicircle
t.circle(45,200)
#Turns the turtle counterclockwise
t.left(220)
# draw a semicircle
t.circle(45,200)
#tell turtle to move forward by 120 units
t.forward(120)
#Close the polygon and fill with the current fill color
t.end_fill()
# hide the turtle
t.hideturtle()

No comments:

Post a Comment