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()
效果展示: