This article is participating in Python Theme Month. See the link to the event for more details

Chinese Valentine’s Day is coming, python to help you love

The Chinese Valentine’s Day is coming, brothers. It is too conventional for programmers to send roses year after year. Let’s take our jobs and give her her own valentine’s Day gift.

How about an arrow through the heart? We use Turtle – Turtle graphics ¶ Check the official documents for yourself. This article will explain the painting process in detail.

First draw the screen size

To draw the entire screen of the computer, the computer desktop right click -> show Settings. Find a computer with a resolution of 1920×1080, so let’s draw the screen at that size. And add a method to exit when clicked.

import turtle
​
if __name__ == '__main__':
    turtle.setup(width=1920, height=1080)
    window = turtle.Screen()
    window.exitonclick()
Copy the code

Look for the right starting value

1. We know that the center of the screen is (0,0), so our starting value should be (0,y), which is more appropriate based on the attempt (0,200).

So we should move the brush from (0,0) to (0,200).

3. The default brush is black and painted on the screen, so we hide the pen, and we don’t need to see how the brush moves.

4, set the pen color, thickness and speed

import turtle
​
if __name__ == '__main__':
    turtle.setup(width=1920, height=1080)
​
    turtle.color('red'."pink")
    turtle.pensize(5)
    turtle.speed(5)
    turtle.up()
    turtle.hideturtle()
    turtle.goto(0.200)
​
    window = turtle.Screen()
    window.exitonclick()
Copy the code

Draw the heart of the upper left part

1. Show the hidden brush

2. Show the path of the brush painting

3. Start the pen at 120° to the left, rotate to the left and draw at the same time.

4. Fill the heart with color after painting, so prepare to fill before painting.

import turtle
​
if __name__ == '__main__':
    turtle.setup(width=1920, height=1080)
​
    turtle.color('red'."pink")
    turtle.pensize(5)
    turtle.speed(5)
    turtle.up()
    turtle.hideturtle()
    turtle.goto(0.200)
​
    turtle.showturtle()
    turtle.down()
    turtle.begin_fill()
    turtle.left(120)
    turtle.down()
    for i in range(200):
        turtle.left(1)
        turtle.forward(2)
​
    window = turtle.Screen()
    window.exitonclick()
Copy the code

The image below

Draw the heart of the upper right part

1. Move to (0,200), which is the same process as above, so encapsulate the method back to the original point.

2. Brush display is also a method of repeated operation encapsulation.

3. The process of drawing the top heart is almost similar.

import turtle
​
​
def returnInitialPoint(x: int, y: int) :
    turtle.up()
    turtle.hideturtle()
    turtle.goto(x, y)
    turtle.showturtle()
    turtle.down()
​
​
def upperHalfHeart(firstDirection: str, parameter: int, rangeParameter: int, direction: str, dParameter: int,
                   forwardParameter: int) :
    turtle.left(parameter) if firstDirection == "left" else turtle.right(parameter)
    for i in range(rangeParameter):
        turtle.left(dParameter) if direction == "left" else turtle.right(dParameter)
        turtle.forward(forwardParameter)
​
if __name__ == '__main__':
    turtle.setup(width=1920, height=1080)
​
    turtle.color('red'."pink")
    turtle.pensize(5)
    turtle.speed(7)
    returnInitialPoint(0.200)
    turtle.begin_fill()
    upperHalfHeart("left".120.200."left".1.2)
    returnInitialPoint(0.200)
    upperHalfHeart("left".100.200."right".1.2)
​
    window = turtle.Screen()
    window.exitonclick()
​
Copy the code

The image below

Let me draw the bottom half of the heart

Draw to the left to the center, then turn the pen 90 degrees to draw the same length.

import turtle
​
​
def returnInitialPoint(x: int, y: int) :
    turtle.up()
    turtle.hideturtle()
    turtle.goto(x, y)
    turtle.showturtle()
    turtle.down()
​
​
def upperHalfHeart(firstDirection: str, parameter: int, rangeParameter: int, direction: str, dParameter: int,
                   forwardParameter: int) :
    turtle.left(parameter) if firstDirection == "left" else turtle.right(parameter)
    for i in range(rangeParameter):
        turtle.left(dParameter) if direction == "left" else turtle.right(dParameter)
        turtle.forward(forwardParameter)
​
​
if __name__ == '__main__':
    turtle.setup(width=1920, height=1080)
​
    turtle.color('red'."pink")
    turtle.pensize(5)
    turtle.speed(7)
    returnInitialPoint(0.200)
    turtle.begin_fill()
    upperHalfHeart("left".120.200."left".1.2)
    returnInitialPoint(0.200)
    upperHalfHeart("left".100.200."right".1.2)
    turtle.left(5)
    turtle.forward(243)
    turtle.right(90)
    turtle.forward(243)
​
    window = turtle.Screen()
    window.exitonclick()
​
Copy the code

The image below

A complete heart is drawn.

Fill color

turtle.end_fill()
Copy the code

Draw the second heart

And the above process is basically similar, no longer repeated.

import turtle
​
​
def returnInitialPoint(x: int, y: int) :
    turtle.up()
    turtle.hideturtle()
    turtle.goto(x, y)
    turtle.showturtle()
    turtle.down()
​
​
def upperHalfHeart(firstDirection: str, parameter: int, rangeParameter: int, direction: str, dParameter: int,
                   forwardParameter: int) :
    turtle.left(parameter) if firstDirection == "left" else turtle.right(parameter)
    for i in range(rangeParameter):
        turtle.left(dParameter) if direction == "left" else turtle.right(dParameter)
        turtle.forward(forwardParameter)
​
​
def arrowHead() :
   turtle.pensize(1)
   turtle.speed(1)
   turtle.color('red'.'red')
   turtle.begin_fill()
   turtle.left(120)
   turtle.forward(20)
   turtle.right(150)
   turtle.forward(35)
   turtle.right(120)
   turtle.forward(35)
   turtle.right(150)
   turtle.forward(20)
​
if __name__ == '__main__':
    turtle.setup(width=1920, height=1080)
​
    turtle.color('red'."pink")
    turtle.pensize(5)
    turtle.speed(7)
    returnInitialPoint(0.200)
    turtle.begin_fill()
    upperHalfHeart("left".120.200."left".1.2)
    returnInitialPoint(0.200)
    upperHalfHeart("left".100.200."right".1.2)
    turtle.left(5)
    turtle.forward(243)
    turtle.right(90)
    turtle.forward(243)
    turtle.end_fill()
    returnInitialPoint(130.90)
    turtle.begin_fill()
    upperHalfHeart("right".30.100."left".2.2)
    returnInitialPoint(130.90)
    upperHalfHeart("left".130.100."right".2.2)
    turtle.right(10)
    turtle.forward(145)
    turtle.right(90)
    turtle.forward(145)
    turtle.end_fill()
    window = turtle.Screen()
    window.exitonclick()
Copy the code

Draw the shaft through the heart

1. Find the right starting position (-400,200)

2. Set the width of the brush, which is the thickness of the shaft. This one is a bit thick, you can reduce it a little.

3. Find the right end position for the shaft.

returnInitialPoint(-400.200)
turtle.pensize(10)
turtle.goto(339, - 50)
Copy the code

Draw the arrow through the heart

First, set the color of the arrow to red

2. Rotate 50° to the right and advance 30 pixels

3, the same below

def arrow() :
    turtle.speed(1)
    turtle.color('red'.'red')
    turtle.begin_fill()
    turtle.right(50)
    turtle.forward(30)
    turtle.right(130)
    turtle.forward(80)
    turtle.right(135)
    turtle.forward(80)
    turtle.right(135)
    turtle.forward(30)
    turtle.end_fill()
Copy the code

Write the names of both people

1. Get in the right place

2. Set the font and color

3, write

returnInitialPoint(0.150)
turtle.color('#CD5C5C'.'pink')
turtle.write("The low-profile girl.", font=('gungsuh'.30,), align="center")
returnInitialPoint(130.50)
turtle.write("Nuggets", font=('gungsuh'.30,), align="center")
Copy the code

Change the value to user dynamic input

name = input('Please enter yourname: ')
girlFrient = input('Please enter your girlfriend's name: ')
Copy the code

The complete code

import turtle
​
​
def returnInitialPoint(x: int, y: int) :
    turtle.up()
    turtle.hideturtle()
    turtle.goto(x, y)
    turtle.showturtle()
    turtle.down()
​
​
def upperHalfHeart(firstDirection: str, parameter: int, rangeParameter: int, direction: str, dParameter: int,
                   forwardParameter: int) :
    turtle.left(parameter) if firstDirection == "left" else turtle.right(parameter)
    for i in range(rangeParameter):
        turtle.left(dParameter) if direction == "left" else turtle.right(dParameter)
        turtle.forward(forwardParameter)
​
​
def arrow() :
    turtle.speed(1)
    turtle.color('red'.'red')
    turtle.begin_fill()
    turtle.right(50)
    turtle.forward(30)
    turtle.right(130)
    turtle.forward(80)
    turtle.right(135)
    turtle.forward(80)
    turtle.right(135)
    turtle.forward(30)
    turtle.end_fill()
​
​
if __name__ == '__main__':
    name = input('Please enter yourname: ')
    girlFrient = input('Please enter your girlfriend's name: ') turtle. Setup (width = 1920, height = 1080) turtle. The color ('red', "pink") turtle.pensize(5) turtle.speed(7) returnInitialPoint(0, 200) turtle.begin_fill() upperHalfHeart("left", 120, 200, "left", 1, 2) returnInitialPoint(0, 200) upperHalfHeart("left", 100, 200, "right", 1, 2) turtle.left(5) turtle.forward(243) turtle.right(90) turtle.forward(243) turtle.end_fill() returnInitialPoint(130, 90) turtle.begin_fill() upperHalfHeart("right", 30, 100, "left", 2, 2) returnInitialPoint(130, 90) upperHalfHeart("left", 130, 100, "right", 2, 2) turtle.right(10) turtle.forward(145) turtle.right(90) turtle.forward(145) turtle.end_fill() returnInitialPoint(-400, 200) turtle.pensize(10) turtle.goto(339, - 50) arrow() returnInitialPoint(0, 150) turtle.color('#CD5C5C', 'pink')
    turtle.write(name, font=('gungsuh'.30,), align="center")
    returnInitialPoint(130.50)
    turtle.write(girlFrient, font=('gungsuh'.30,), align="center")
    window = turtle.Screen()
    window.exitonclick()
Copy the code

Write in the last

Because of time reasons, this example did not carry out accurate calculation, only the crude implementation of it, if you have the time or interest, you can improve it.

I wish all shall be well, jack shall have Jill, and everyone can express their love successfully.