Lover's robbery, teach you to use python to draw dynamic love confession

d22f2947b7c2e1e585b87512a88c1816.png

It's Valentine's Day soon, and it's also a reason to give a small gift to confess

Source code acquisition:

1 Click the card below

2. Reply keyword: love

Pay attention to the public number: python private land

Beginner drawing heart

Learning Python, I feel that all of you are very complicated, so let me make a simple one. I directly regard the heart shape as a square + two semicircles:

26990125d0c81b99208065251cf201bd.png

So this is very simple, ten lines of code to solve:

import turtle as t
t.pensize(2)
# 笔大小2像素
t.pencolor("red")
# 颜色为红色
t.left(45)
# 45度
t.fd(200)
# 向前200直线
t.circle(100, 180)
# 画一圆半径100 弧度180
t.right(90)
# 向右90度
t.circle(100, 180)
# 再画一个圆半径100 弧度180
t.fd(200)
# 直线向前直线200
t.done()
# 绘制完成

524847fbe2d38a01b66e141a80b8d618.png

A line of code to draw a heart

original code

print('\n'.join([''.join([('Love'[(x-y) % len('Love')] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 <= 0 else ' ') for x in range(-30, 30)]) for y in range(30, -30, -1)]))


067f122979bf7730da11dabc676ca03d.png

After dismantling this long string of [one line of code], the following multi-line code is obtained, and the actual running effect is the same. The code is as follows:

import time
words = input('请输出想要表达的文字:')
#例子:words = "Dear lili, Happy Valentine's Day! Lyon Will Always Love You Till The End! ♥ Forever!  ♥"
for item in words.split():
    #要想实现打印出字符间的空格效果,此处添加:item = item+' '
    letterlist = []#letterlist是所有打印字符的总list,里面包含y条子列表list_X
    for y in range(12, -12, -1):
        list_X = []#list_X是X轴上的打印字符列表,里面装着一个String类的letters
        letters = ''#letters即为list_X内的字符串,实际是本行要打印的所有字符
        for x in range(-30, 30):#*是乘法,**是幂次方
            expression = ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3
            if expression <= 0:
                letters += item[(x-y) % len(item)]
            else:
                letters += ' '
        list_X.append(letters)
        letterlist += list_X
    print('\n'.join(letterlist))
    time.sleep(1.5);

However, it's a bit too monotonous. Come on, let me roll up my sleeves and do something, and simply modify the code to achieve the effect of dynamically outputting a heart-shaped sentence:

After the makeover, it looks great! The effect is as follows:

The code is as follows, if you like it, please like it, thank you ❤️! :

import time
words = input('请输出想要表达的文字:')
for item in words.split():
    print('\n'.join([''.join([(item[(x-y) % len(item)] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 <= 0 else ' ') for x in range(-30, 30)]) for y in range(12, -12, -1)]))
    time.sleep(1.5)

In fact, emoji like ❤️ can actually be output, but the format is not a standard heart shape, you need to adjust it yourself, you can try it yourself ^_^!

Reference: Lyon https://zhuanlan.zhihu.com/p/33816013

Source code acquisition:

1. Scan the QR code below

2. Reply keyword: love

b66911a91feefb05657dbb3299425ae5.png

Press and hold the QR code above for 2 seconds 

Reply to " love " to get the source code

Pay attention to the public number: python private land

推荐阅读  点击标题可跳转用python代码打包app
全军覆没,韩国人破防了
一个随时随地写Python代码的神器
滴滴程序员被亲戚鄙视不如二本教书的……

I'm Lao Zeng (personal WeChat:  tuling030  )  remarks that 888 has established a WeChat group to answer questions with each other, share selected e-books, and show you various benefits for free. Students in need can add me to WeChat into the group.

Guess you like

Origin blog.csdn.net/bigzql/article/details/122904714
Recommended