python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python生成动画

利用python生成一个终极复杂的跳动小红心动画

作者:水月清辉

这篇文章主要为大家详细介绍了一个使用Python内置turtle模块实现的跳动爱心动画特效,无需安装任何第三方库,感兴趣的小伙伴可以跟随小编一起学习一下

跳动 + 光晕拖影 + 粒子飘散 + 颜色渐变 + 呼吸闪烁 + 上下漂浮

全是流畅动画,复制就能跑,Python 自带 turtle 实现,不用装任何库!

终极复杂动画:跳动小红心 

import turtle
import math
import random
import time  # 【修复1】补充导入 time 模块
# ================= 配置参数 =================
WIDTH, HEIGHT = 800, 800
PARTICLE_COUNT = 100  # 粒子数量,形成浪漫满天星效果
HEART_STEP = 3  # 【优化2】增加绘制步长,减少计算量 (原1,即360个点)
# 窗口设置
screen = turtle.Screen()
screen.title("❤️ 终极特效跳动爱心 (优化版) ❤️")
screen.bgcolor("black")
screen.setup(WIDTH, HEIGHT)
screen.tracer(0)  # 极速流畅动画
# 主爱心画笔
heart = turtle.Turtle()
heart.hideturtle()
heart.speed(0)
# 粒子系统(浪漫满天星特效)
particles = []
class Particle:
    def __init__(self):
        self.t = turtle.Turtle()
        self.t.hideturtle()
        self.t.speed(0)
        self.t.penup()
        self.reset()
    def reset(self):
        # 扩大粒子分布范围,形成满天星效果
        x = random.randint(-350, 350)
        y = random.randint(-350, 350)
        self.t.goto(x, y)
        self.size = random.randint(1, 4)  # 更小的粒子,更精致
        self.speed_x = random.uniform(-1, 1)  # 减慢速度,更浪漫
        self.speed_y = random.uniform(-1, 1)
        self.life = random.randint(40, 80)  # 增加生命周期
        # 丰富的彩色粒子:粉色、紫色、蓝色、白色、金色
        self.color = random.choice([
            "#ff0033", "#ff3366", "#ff6699", "#ff99cc",  # 粉色系
            "#9900ff", "#cc66ff", "#ffccff",  # 紫色系
            "#0066ff", "#66ccff", "#ccffff",  # 蓝色系
            "#ffffff", "#ffff99", "#ffcc00",  # 白色和金色
            "#ff6666", "#ffcc66"  # 暖色系
        ])
    def update(self):
        self.life -= 1
        if self.life <= 0:
            self.reset()
            return
        x, y = self.t.position()
        self.t.goto(x + self.speed_x, y + self.speed_y)
        self.t.clear()
        # 添加闪烁效果
        if random.random() < 0.1:  # 10%的概率改变透明度(通过大小模拟)
            current_size = self.size if random.random() > 0.5 else self.size * 0.5
        else:
            current_size = self.size
        self.t.color(self.color)
        self.t.dot(current_size)
# 创建粒子
for _ in range(PARTICLE_COUNT):
    particles.append(Particle())
# 数学公式画标准爱心(增加旋转逻辑)
def draw_heart(size, angle, color):
    heart.penup()
    heart.goto(0, 0)
    heart.pendown()
    heart.color(color)
    heart.begin_fill()
    # 预计算旋转参数
    rad_angle = math.radians(angle)
    cos_a = math.cos(rad_angle)
    sin_a = math.sin(rad_angle)
    # 【优化3】使用 range(0, 360, HEART_STEP) 减少循环次数
    for i in range(0, 360, HEART_STEP):
        rad = math.radians(i)
        # 爱心标准公式
        x = 16 * math.sin(rad) ** 3
        y = 13 * math.cos(rad) - 5 * math.cos(2 * rad) - 2 * math.cos(3 * rad) - math.cos(4 * rad)
        # 【修复2】应用旋转矩阵公式
        # x_new = x * cos(θ) - y * sin(θ)
        # y_new = x * sin(θ) + y * cos(θ)
        x_rot = x * cos_a - y * sin_a
        y_rot = x * sin_a + y * cos_a
        heart.goto(x_rot * size, y_rot * size)
    heart.end_fill()
# 动画主循环
frame = 0
try:
    while True:
        frame += 1
        heart.clear()
        # 更新所有粒子
        for p in particles:
            p.update()
        # ===================== 动画参数计算 =====================
        # 1. 爱心大小:流畅跳动
        beat = math.sin(frame / 4)
        size = 12 + 4 * beat
        # 2. 取消旋转,保持爱心正面朝向
        rotate = 0
        # 3. 上下漂浮
        y_float = 8 * math.sin(frame / 6)
        # 4. 颜色渐变
        r = 255
        g = int(40 + 80 * abs(math.sin(frame / 7)))
        b = int(60 + 90 * abs(math.sin(frame / 9)))
        main_color = f'#{r:02x}{g:02x}{b:02x}'
        # 5. 绘制光晕和主心(取消旋转,增强立体感)
        # 通过多层叠加和颜色渐变营造立体效果
        # 外层光晕(深红色,最大)
        heart.penup()
        heart.goto(0, y_float)  # 应用漂浮
        heart.pendown()
        draw_heart(size * 1.6, 0, "#330000")
        # 中层发光(中等红色,中等大小)
        heart.penup()
        heart.goto(0, y_float)
        heart.pendown()
        draw_heart(size * 1.2, 0, "#660022")
        # 主爱心(渐变色,核心)
        heart.penup()
        heart.goto(0, y_float)
        heart.pendown()
        draw_heart(size, 0, main_color)
        screen.update()
        time.sleep(0.02)  # 【修复3】使用 time.sleep
except turtle.Terminator:
    print("程序已关闭")

效果预览

这个终极版将呈现:

展示效果:

到此这篇关于利用python生成一个终极复杂的跳动小红心动画的文章就介绍到这了,更多相关python生成动画内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文