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()
效果展示:
分类:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构