Chinese Valentine's Day is coming, let's use Python to confess your love biubiubiu

Every time there are various festivals, many friends will encounter such a century-old problem-how to give the girl/girlfriend/wife they like a unique holiday surprise.

Isn't it going to be Qixi Festival again soon, we can try to express our love to our girlfriends with python! like this

Let's take everyone to appreciate the way of expressing love in Python, so that girlfriends can feel the romance of IT people.

Turtle basic parameters

The power of python is that it has many powerful libraries, and turtle is a module that can interactively paint. It is also a secret magic weapon to enhance the fun of learning python for beginners!

As an art idiot, it may be difficult to draw a picture, but with python's turtle library, you can achieve painting with only a few lines of code [1] .

turtleIt is a library that comes with python, just call it directly.

from turtle import *

Then we need to understand the basic statements of turtle to lay a good foundation for drawing.

Now, let's try to use the commands in the table above to see how the turtle moves.

from turtle import *

forward(200) # 从当前画笔方向移动200
left(90) # 逆时针移动90°
backward(200) # 在当前画笔方向的反方向移动200
right(90) # 顺时针移动90°
circle(200) # 画一个半径为200的圆,圆心在画笔左边

The effect after running is as follows

As you can see, the brush moves as we expected. Now let's try to draw pictures by simply imitating them.

turtle drawing

But in order 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.

A heart shape was successfully drawn and filled with color.

For the rest, we can use straight lines and curves to draw the villain's head, arms and body in turn.

#头部
color('black')
go_to(-228, 72)
pensize(3)
left(150)
ring(350,1,0.8,'right')

#手臂
left(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)
left
forward(80)

 

Of course, in addition to adding a heart shape, you can also add some text. If you want to customize the text, just change the text in the parameters directly 520.

# 添加文字
go_to(-39, 69)
write("520", align="left", font=("黑体", 30, "normal"))

The final result is shown in the figure below

code download

If you are interested in the code of this article, please click ↓

Love Confession Code Tencent Documentation - Online Documentation https://docs.qq.com/doc/DT2ZJRVFqYm5oY2lz

Guess you like

Origin blog.csdn.net/m0_59485658/article/details/126072965#comments_27493106