Every time it comes to various festivals, many friends will encounter such a century-old problem - how to give their favorite girl/girlfriend/wife a unique holiday surprise.
It's not going to Qixi again soon, we can try to use python to express our love to our girlfriends!like this
Let's take a look at the way of confessing in Python, so that girlfriends can feel the romance of IT people.
The power of python lies in its many powerful libraries, among which turtle is a module for interactive painting.It is also a secret magic weapon to enhance the fun of learning python for beginners!
As an artistic idiot, it can be difficult to draw a picture,but with python's turtle library, you can do it with just a few lines of code[1].
turtle
is a library that comes with python, which can be called directly.
from turtle import *
Then we need to understand the basic statements of turtle to lay a solid foundation for drawing.
Now, let's try the commands in the table above to see how the turtle moves.
from turtle import *forward(200) # move 200 from the current brush directionleft(90) # move 90° counterclockwisebackward(200) # Move 200 in the opposite direction of the current brush directionright(90) # move 90° clockwisecircle(200) # Draw a circle with a radius of 200, the center of which is on the left side of the brush
The effect after running is as follows
As you can see, the brush moves as we imagined.Let's start to try to draw a picture by simply imitating it by ourselves
But to save work, we can create some functions, so that we don't have to write a lot of basic code many times.
def heart(x, y, size):go_to(x, y)left(150)begin_fill()forward(51*size)ring(150,size,0.3,'right')ring(210,size,0.786,'right')left(120)ring(210,size,0.786,'right')ring(150,size,0.3,'right')forward(51*size)end_fill()
For example, the above code is to build a function to draw a heart shape, and test the effect separately.
Successfully drawn a heart shape and filled it with color.
The rest we can use straight lines and curved lines to draw the villain's head, arms and body in turn.
#headercolor('black')go_to(-228, 72)pensize(3)left(150)ring(350,1,0.8,'right')#armleft(150)forward(70)left(90)forward(10)ring(200,0.1,0.9,'right')forward(10)left(90)forward(20)ring(200,0.1,0.9,'right')forward(10)left(90)ring(200,0.2,0.9,'right')left(100)leftforward(80)
Of course, in addition to adding hearts, you can also add some text.If you want to customize the text, just change the text 520
in the parameter directly.
# add textgo_to(-39, 69)write("520", align="left", font=("bold", 30, "normal"))
The final result is shown below
If a small partner is interested in the code of this article, you can click ↓
Love Confession CodeTencent Documentation-Online Documentationhttps://docs.qq.com/doc/DT2ZJRVFqYm5oY2lz