Python情人节表白小程序

Python练手,情人节小程序,心跳+爱心

复制代码
import numpy as np
import matplotlib.pyplot as plt
import time

def heart(x, bound='up'):
    if bound == 'up':
        return np.sqrt(2 * np.abs(x) - x ** 2)
    if bound == 'low':
        return np.arcsin(np.abs(x) - 1) - np.pi / 2
    else:
        raise ValueError('bound abould be up or low')

def heard_curve(x, a, b=0):
    y = np.cos(a * 2 * np.pi * x + b)
    up_index = np.where(y > 0)
    low_index = np.where(y <= 0)
    y[up_index] = heart(x[up_index], 'up') * y[up_index]
    y[low_index] = heart(x[low_index], 'low') * np.abs(y[low_index])
    return y

if __name__ == '__main__':
    a = np.linspace(0, 1, 150)
    a = (a ** 3) * 4
    b = np.linspace(0, 20, 150)
    x = np.linspace(-2, 2, 500)
    plt.ion()
    plt.figure(figsize=(8, 8))
    plt.figure(1)
    for i in range(a.size):
        plt.axis('off')
        plt.fill_between(x, heart(x, 'low'), heart(x, 'up'),
                         color='r', alpha=0.4 * (i / a.size) ** 3)
        plt.plot(x, heard_curve(x, a[i], b[i]), 'r', linewidth=2)
        plt.rcParams['font.sans-serif'] = ['SimHei']
        plt.rcParams['axes.unicode_minus'] = False
        plt.text(0, -1, "LOVE YOU", fontsize=30, alpha=0.9 * (i / a.size) ** 5,
                 color='r', verticalalignment="center", horizontalalignment="center")
        if i != a.size:
            plt.draw()
            plt.pause(0.03)
            plt.cla()
    time.sleep(2)
    plt.show()
复制代码

效果展示:

 

 

posted on   明.Sir  阅读(538)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示