Python+Matplotlib实现动态旋转爱心粒子动画(附完整源码)

 爱心代码,快拿去给心爱的女孩子看吧。

2025最新!Python+Matplotlib实现1200粒子爱心动画(不卡顿/附源码)

import numpy as np
import matplotlib
matplotlib.use('TkAgg')  # 强制使用 TkAgg 后端
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# 粒子数量和角度 - 增加粒子数量使轮廓更清晰
num_particles = 1200  # 增加粒子数量
t = np.linspace(0, 2 * np.pi, num_particles)  # 均匀分布的角度
r = 0.9 + 0.1 * np.random.rand(num_particles)  # 减小随机扰动,使轮廓更清晰

# 爱心轮廓公式
x = 16 * np.sin(t)**3 * r
y = (13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)) * r

# 粒子旋转参数 - 调整旋转速度和偏移
rotation_speeds = np.random.uniform(0.2, 1.0, num_particles)  # 减慢旋转速度
phases = np.random.uniform(0, 2 * np.pi, num_particles)  # 每个粒子的初始相位
offsets = np.random.uniform(-0.1, 0.1, num_particles)  # 减小轨道偏移范围

# 初始化图像
fig, ax = plt.subplots(figsize=(10, 9))  # 调整图像大小
fig.patch.set_facecolor('black')
ax.set_facecolor('black')

# 创建渐变颜色效果
colors = np.zeros((num_particles, 4))
colors[:, 0] = 1.0  # 红色分量
colors[:, 1] = 0.2 + 0.3 * np.random.rand(num_particles)  # 绿色分量变化
colors[:, 2] = 0.4 + 0.2 * np.random.rand(num_particles)  # 蓝色分量变化
colors[:, 3] = 0.8 + 0.2 * np.random.rand(num_particles)  # 透明度变化

# 不同位置的粒子大小不同
sizes = np.random.uniform(3, 7, num_particles)  # 粒子大小变化

sc = ax.scatter(x, y, s=sizes, color=colors)  # 使用不同大小和颜色
ax.set_xlim(-18, 18)
ax.set_ylim(-18, 18)
ax.set_aspect('equal')
plt.axis('off')

# 动画函数
def update(frame):
    # 心跳效果 - 调整脉动幅度
    scale = 1 + 0.04 * np.sin(np.radians(frame * 2))  # 微调脉动缩放

    # 旋转效果
    angles = t + phases + frame * rotation_speeds * 0.008  # 减慢旋转速度
    x_rotated = 16 * np.sin(angles)**3 * (r + offsets * np.sin(frame * 0.01))  # 动态轨道偏移
    y_rotated = (13 * np.cos(angles) - 5 * np.cos(2 * angles) - 2 * np.cos(3 * angles) - np.cos(4 * angles)) * (r + offsets)

    # 使用缩放
    x_final = x_rotated * scale
    y_final = y_rotated * scale

    # 更新粒子位置
    sc.set_offsets(np.c_[x_final, y_final])
    return sc,

# 运行动画
ani = FuncAnimation(fig, update, frames=360, interval=30, blit=True)
plt.show()
如果觉得本文有帮助,欢迎点赞⭐收藏✨关注❤️,三连支持!在评论区留言#获取源码#,我会发送完整项目文件~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值