用Python绘制随心跳跳动的星空爱心(附完整代码)

Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

效果预览

在这里插入图片描述
在这里插入图片描述

本程序将呈现以下视觉特效:

  • 渐变彩虹爱心​:多层次的爱心结构配合HSV色彩空间变换
  • ​星空背景​:200颗随机分布的闪烁星星,部分带有十字星光特效
  • 粒子特效​:60个随机发散的金色粒子,带有生命周期衰减效果
  • 呼吸光晕​:10层动态光晕包裹爱心,随心跳节奏律动

代码解析

导入所需库

import turtle    # 核心绘图库
import math      # 数学计算
import colorsys  # 颜色空间转换
import random    # 随机数生成

画布初始化

screen = turtle.Screen()
screen.bgcolor('black')          # 星空背景
screen.setup(width=800, height=600)
screen.title("Glowing Heart with Stardust")
screen.tracer(0)                 # 关闭自动刷新

爱心参数方程

x = scale * (16 * math.sin(angle)**3)
y = scale * (13 * math.cos(angle) - 5 * math.cos(2*angle) 
           - 2 * math.cos(3*angle) - math.cos(4*angle))

该方程通过傅里叶级数叠加,生成更圆润的心形轮廓。

彩虹渐变算法

# HSV颜色空间转换(色相、饱和度、明度)
rainbow_hue = (hue + i/steps) % 1.0  
saturation = 0.9 + (i/steps) * 0.1   
value = 1 - (i/steps) * 0.2
pen.color(colorsys.hsv_to_rgb(rainbow_hue, saturation, value))

粒子系统设计

{
    "pos": (x, y),            # 当前位置
    "vector": (vx, vy),       # 速度矢量
    "life": 3.0,              # 剩余寿命
    "max_life": 3.0,          # 最大寿命
    "size": 5.0,              # 粒子尺寸
    "color": (1, 0.7, 0.3),   # RGB颜色
    "rotation": 45            # 旋转角度
}

心跳动画循环

while True:
    # 动态参数更新
    heart_beat += 0.3  # 心跳速度
    hue = (hue + 0.01) % 1.0  # 色相偏移
    
    # 绘制多层爱心
    for i in range(glow_layers):
        current_scale = heart_scale + (i * glow_step)/100 
                        + math.sin(heart_beat) * 0.1  # 呼吸效果
        draw_heart(current_scale, layer_hue)
    
    # 粒子系统更新
    if random.random() < 0.2:
        init_particles()
    
    screen.update()
    turtle.delay(2)  # 控制帧率

星光闪烁算法

# 正弦波控制尺寸变化
brightness = (math.sin(star["twinkle"]) + 1) / 2
star["size"] = base_size * (0.7 + 0.5*math.sin(star["twinkle"]))

完整代码

import turtle
import math
import colorsys
import random

# 设置画布
screen = turtle.Screen()
screen.bgcolor('black')
screen.setup(width=800, height=600)
screen.title("Glowing Heart with Stardust")
screen.tracer(0)

# 创建画笔
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.pensize(3)

star_pen = turtle.Turtle()
star_pen.hideturtle()
star_pen.speed(0)
star_pen.penup()

glow_pen = turtle.Turtle()
glow_pen.hideturtle()
glow_pen.speed(0)
glow_pen.penup()

# 参数配置
glow_layers = 10      # 增加发光层数
glow_step = 10       # 缩放步长
heart_scale = 2.0    # 基础缩放系数
stars = []           # 存储星空粒子
particles = []       # 存储发散粒子
particle_colors = [
    (1, 0.7, 0.3),   # 金色
    (1, 0.5, 0.5),   # 粉色
    (0.7, 0.3, 1),   # 紫色
    (0.3, 0.7, 1),   # 蓝色
    (1, 0.3, 0.3)    # 红色
]

def init_stars():
    for _ in range(200):  # 增加星星数量
        x = random.randint(-400, 400)
        y = random.randint(-300, 300)
        size = random.choice([1.5, 2, 2.5, 3, 3.5])  # 更多尺寸选择
        twinkle_speed = random.uniform(0.02, 0.08)
        stars.append({
            "pos": (x, y),
            "size": size,
            "base_size": size,
            "twinkle": random.uniform(0, 2*math.pi),  # 随机初始相位
            "speed": twinkle_speed,
            "color": random.uniform(0.5, 1.0)  # 随机亮度
        })

def init_particles():
    for _ in range(60):  # 增加粒子数量
        angle = random.uniform(0, 2*math.pi)
        speed = random.uniform(2, 6)  # 增加速度范围
        life = random.uniform(1.5, 3.5)  # 增加生命周期范围
        particles.append({
            "pos": (0, 0),
            "vector": (math.cos(angle)*speed, math.sin(angle)*speed),
            "life": life,
            "max_life": life,
            "size": random.uniform(2, 5),
            "color": random.choice(particle_colors),
            "rotation": random.uniform(0, 360)
        })

def draw_heart(scale, hue):
    # 使用更丰富的彩虹渐变填充
    steps = 60  
    for i in range(steps):
        # 创建彩虹渐变效果
        rainbow_hue = (hue + i/steps) % 1.0  # 随层次变化色相
        saturation = 0.9 + (i/steps) * 0.1   # 增加饱和度
        value = 1 - (i/steps) * 0.2          # 调整明度渐变
        pen.color(colorsys.hsv_to_rgb(rainbow_hue, saturation, value))
        pen.begin_fill()
        for t in range(0, 360, 2):
            angle = math.radians(t)
            x = scale * (16 * math.sin(angle)**3)
            y = scale * (13 * math.cos(angle) - 5 * math.cos(2*angle) - 
                        2 * math.cos(3*angle) - math.cos(4*angle))
            if t == 0:
                pen.penup()
                pen.goto(x, y)
                pen.pendown()
            else:
                pen.goto(x, y)
        pen.end_fill()
        scale -= 0.015

def draw_glow():
    # 绘制心形周围的光晕效果
    glow_pen.clear()
    for angle in range(0, 360, 15):
        glow_pen.penup()
        glow_pen.goto(0, 0)
        glow_pen.setheading(angle)
        color = colorsys.hsv_to_rgb(hue, 0.5, 0.3)
        for i in range(3):
            glow_pen.pensize(3 - i)
            glow_pen.color(color)
            glow_pen.forward(50 + i * 20)
            glow_pen.dot(5 - i)

def draw_stars():
    star_pen.clear()
    for star in stars:
        star_pen.goto(star["pos"][0], star["pos"][1])
        brightness = (math.sin(star["twinkle"]) + 1) / 2
        color = (star["color"], star["color"], 1)  # 使用蓝白色调
        star_pen.dot(star["size"], color)
        # 添加十字星光效果
        if star["size"] > 2.5:
            for angle in [0, 90]:
                star_pen.setheading(angle)
                star_pen.penup()
                star_pen.forward(star["size"])
                star_pen.dot(star["size"]/2, color)
                star_pen.backward(star["size"] * 2)
                star_pen.dot(star["size"]/2, color)

def update_stars():
    for star in stars:
        star["twinkle"] = (star["twinkle"] + star["speed"]) % (2*math.pi)
        star["size"] = star["base_size"] * (0.7 + 0.5*math.sin(star["twinkle"]))

def update_particles():
    new_particles = []
    for p in particles:
        if p["life"] > 0:
            x = p["pos"][0] + p["vector"][0]
            y = p["pos"][1] + p["vector"][1]
            p["life"] -= 0.05
            new_particles.append({
                "pos": (x, y),
                "vector": p["vector"],
                "life": p["life"],
                "max_life": p["max_life"],
                "size": p["size"] * (p["life"] / p["max_life"]),
                "color": p["color"],
                "rotation": p["rotation"]
            })
    particles[:] = new_particles

def draw_particles():
    for p in particles:
        star_pen.goto(p["pos"][0], p["pos"][1])
        alpha = p["life"] / p["max_life"]
        color = tuple(c * alpha for c in p["color"])
        star_pen.dot(p["size"], color)

# 初始化效果
init_stars()
init_particles()

# 动画循环
hue = 0.0
heart_beat = 0
while True:
    # 更新元素
    update_stars()
    update_particles()
    
    # 更新心跳效果
    heart_beat += 0.3  # 加快心跳速度,原值0.1
    
    # 清空画布并重绘所有元素
    pen.clear()
    glow_pen.clear()
    
    # 绘制背景效果
    draw_stars()
    draw_glow()
    draw_particles()
    
    # 绘制心形
    for i in range(glow_layers):
        current_scale = heart_scale + (i * glow_step)/100 + math.sin(heart_beat) * 0.1
        pen.pensize(2 + i*0.3)
        # 添加更丰富的颜色变化
        layer_hue = (hue + i*0.1 + math.sin(heart_beat*0.5)) % 1.0
        draw_heart(current_scale, layer_hue)
    
    # 生成新粒子
    if random.random() < 0.2:
        init_particles()
    
    # 更新屏幕
    screen.update()
    
    # 更新色相
    hue = (hue + 0.01) % 1.0  # 加快颜色变换速度
    
    # 控制动画速度
    turtle.delay(2)  # 减小延迟时间,原值10

turtle.done()

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

像素艺术家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值