用Python实现两种爱心效果❤️
文章目录
- 一:粒子爱心
- 1.效果展示
- 2.完整代码
- 3.代码分析
- (1)模块导入
- (2)全局常量定义
- (3)爱心粒子系统
- (4)主动画控制
- 二:跳动爱心
- 1.效果展示
- 2.完整代码
- 3.代码分析
- (1)导入模块和全局变量设置
- (2)Heart类 - 核心动画逻辑
一:粒子爱心
1.效果展示
将数学爱心曲线与粒子系统结合,通过预计算和帧动画实现流畅的视觉效果,同时添加了精美的文字特效
2.完整代码
import tkinter as tk
import random
from math import sin, cos, pi, log
import numpy as npnp.set_printoptions(suppress=True)WIDTH, HEIGHT = 800, 600
HEART_CENTER_X, HEART_CENTER_Y = WIDTH // 2, HEIGHT // 2
HEART_SIDE = 11
FPS = 60
ANIMATION_SPEED = 1.5 HEART_COLORS = ["#FFB6C1", "#FFC0CB", "#FFD1DC", "#FFE4E1", "#FFF0F5","#FFB3BA", "#FFDFBA", "#FFFFBA", "#BAFFC9", "#BAE1FF","#FFB6C1", "#FF69B4", "#DB7093", "#FF1493", "#C71585"
]
BACKGROUND_COLOR = "#000000"
TEXT = "我喜欢你" class Heart:def __init__(self, generate_frames=60):self.points = set() self.edge_points = set() self.center_points = set() self.all_points = {} self.frame_count = generate_framesself.build(3000) self.setup_animation()def build(self, point_count):for _ in range(point_count):t = random.uniform(0, 2 * pi)x, y = self.heart_function(t)self.points.add((x, y))for x, y in list(self.points):for _ in range(3):nx, ny = self.scatter_inside(x, y, 0.05)self.edge_points.add((nx, ny))point_list = list(self.points)for _ in range(5000): x, y = random.choice(point_list)nx, ny = self.scatter_inside(x, y, 0.2)self.center_points.add((nx, ny))def heart_function(self, t, shrink_ratio=HEART_SIDE):x = 16 * (sin(t) ** 3)y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))x *= shrink_ratioy *= shrink_ratiox += HEART_CENTER_Xy += HEART_CENTER_Yreturn int(x), int(y)def scatter_inside(self, x, y, beta=0.15):ratio_x = -beta * log(random.random())ratio_y = -beta * log(random.random())dx = ratio_x * (x - HEART_CENTER_X)dy = ratio_y * (y - HEART_CENTER_Y)return x - dx, y - dydef setup_animation(self):for frame in range(self.frame_count):self.calculate_frame(frame)def calculate_frame(self, frame):ratio = 10 * self.animation_curve(frame / self.frame_count * 2 * pi)halo_radius = int(4 + 6 * (1 + self.animation_curve(frame / self.frame_count * 2 * pi)))halo_count = int(4000 + 6000 * abs(self.animation_curve(frame / self.frame_count * 2 * pi) ** 2))halo_points = set()frame_points = []for _ in range(halo_count):t = random.uniform(0, 2 * pi)x, y = self.heart_function(t, shrink_ratio=HEART_SIDE * 0.9)x, y = self.shrink(x, y, halo_radius)if (x, y) not in halo_points:halo_points.add((x, y))x += random.randint(-10, 10)y += random.randint(-10, 10)size = random.choice([1, 2, 3])color = random.choice(HEART_COLORS)frame_points.append((x, y, size, color))for x, y in self.points:nx, ny = self.calculate_position(x, y, ratio)size = random.randint(1, 3)color = self.get_color_gradient(x, y)frame_points.append((nx, ny, size, color))for x, y in self.edge_points:nx, ny = self.calculate_position(x, y, ratio)size = random.randint(1, 2)color = self.get_color_gradient(x, y, edge=True)frame_points.append((nx, ny, size, color))for x, y in self.center_points:nx, ny = self.calculate_position(x, y, ratio)size = random.randint(1, 2)color = self.get_color_gradient(x, y, center=True)frame_points.append((nx, ny, size, color))self.all_points[frame] = frame_pointsdef calculate_position(self, x, y, ratio):dx = ratio * self.force_function(x, y) * (x - HEART_CENTER_X) + random.randint(-1, 1)dy = ratio * self.force_function(x, y) * (y - HEART_CENTER_Y) + random.randint(-1, 1)return x - dx, y - dydef force_function(self, x, y):return 1 / (((x - HEART_CENTER_X) ** 2 + (y - HEART_CENTER_Y) ** 2) ** 0.52)def animation_curve(self, p):return 0.5 * (1 + sin(p))def shrink(self, x, y, ratio):force = -1 / (((x - HEART_CENTER_X) ** 2 + (y - HEART_CENTER_Y) ** 2) ** 0.6)dx = ratio * force * (x - HEART_CENTER_X)dy = ratio * force * (y - HEART_CENTER_Y)return x - dx, y - dydef get_color_gradient(self, x, y, edge=False, center=False):dist = ((x - HEART_CENTER_X) ** 2 + (y - HEART_CENTER_Y) ** 2) ** 0.5max_dist = ((WIDTH / 2) ** 2 + (HEIGHT / 2) ** 2) ** 0.5ratio = min(1.0, dist / max_dist)if center:return HEART_COLORS[int(ratio * len(HEART_COLORS) / 3)]elif edge:return HEART_COLORS[int(len(HEART_COLORS) / 2 + ratio * len(HEART_COLORS) / 3)]else:angle = (x - HEART_CENTER_X) / max_distreturn HEART_COLORS[int((angle + 1) / 2 * len(HEART_COLORS)) % len(HEART_COLORS)]class HeartAnimation:def __init__(self, root):self.root = rootself.root.title("爱心代码")self.root.geometry(f"{WIDTH}x{HEIGHT}")self.root.resizable(False, False)self.root.configure(bg=BACKGROUND_COLOR)self.canvas = tk.Canvas(self.root, width=WIDTH, height=HEIGHT,bg=BACKGROUND_COLOR, highlightthickness=0)self.canvas.pack()self.heart = Heart()self.current_frame = 0self.create_text_effect()self.animate()def create_text_effect(self):self.text_ids = []shadow_offset = 2self.shadow_id = self.canvas.create_text(HEART_CENTER_X + shadow_offset, HEART_CENTER_Y + shadow_offset,text=TEXT, font=("SimHei", 36, "bold"),fill="#4A4A6A", tags="text")glow_colors = ["#FFB6C1", "#FFC0CB", "#FFD1DC", "#FFE4E1", "#FFF0F5"]for i, color in enumerate(glow_colors):glow_size = 36 + i * 2text_id = self.canvas.create_text(HEART_CENTER_X, HEART_CENTER_Y,text=TEXT, font=("SimHei", glow_size, "bold"),fill=color, tags="text", state=tk.HIDDEN)self.text_ids.append(text_id)if i >= len(glow_colors) - 2:self.canvas.itemconfig(text_id, state=tk.NORMAL)self.main_text_id = self.canvas.create_text(HEART_CENTER_X, HEART_CENTER_Y,text=TEXT, font=("SimHei", 36, "bold"),fill="#FFE4E1", tags="text")self.current_text_color = 0self.text_direction = 1self.text_alpha = 1.0def update_text_color(self):soft_colors = [("#FFE4E1", 1.0), ("#FFD1DC", 1.0), ("#FFC0CB", 1.0),("#FFB6C1", 0.9), ("#FFB3BA", 0.8), ("#FFB6C1", 0.9),("#FFC0CB", 1.0), ("#FFD1DC", 1.0)]self.current_text_color = (self.current_text_color + self.text_direction) % len(soft_colors)if self.current_text_color == 0 or self.current_text_color == len(soft_colors) - 1:self.text_direction *= -1color, alpha = soft_colors[self.current_text_color]self.canvas.itemconfig(self.main_text_id, fill=color)offset = 2 * sin(self.current_frame * 0.1)self.canvas.coords(self.main_text_id,HEART_CENTER_X + offset,HEART_CENTER_Y - abs(offset))def animate(self):self.canvas.delete("particles")for x, y, size, color in self.heart.all_points[self.current_frame]:self.canvas.create_rectangle(x, y, x + size, y + size,fill=color, width=0, tags="particles")if self.current_frame % 3 == 0:self.update_text_color()self.current_frame = (self.current_frame + 1) % self.heart.frame_countself.root.after(int(1000 / FPS), self.animate)if __name__ == "__main__":root = tk.Tk()app = HeartAnimation(root)root.mainloop()
3.代码分析
(1)模块导入
import tkinter as tk # tkinter: 用于创建GUI界面
import random # random: 生成随机数用于粒子分布
from math import sin, cos, pi, log # math: 提供数学函数(sin, cos, pi, log)
import numpy as np # 用于数值计算
(2)全局常量定义
# 画布尺寸和爱心位置参数
WIDTH, HEIGHT = 800, 600 # 可修改:窗口宽度和高度
HEART_CENTER_X, HEART_CENTER_Y = WIDTH // 2, HEIGHT // 2 # 爱心中心位置
HEART_SIDE = 11 # 可修改:爱心大小比例因子
FPS = 60 # 可修改:动画帧率
ANIMATION_SPEED = 1.5 # 可修改:动画速度系数# 颜色设置
HEART_COLORS = [ # 可修改:爱心粒子颜色列表"#FFB6C1", "#FFC0CB", "#FFD1DC", "#FFE4E1", "#FFF0F5","#FFB3BA", "#FFDFBA", "#FFFFBA", "#BAFFC9", "#BAE1FF","#FFB6C1", "#FF69B4", "#DB7093", "#FF1493", "#C71585"
]
BACKGROUND_COLOR = "#000000" # 可修改:背景颜色
TEXT = "我喜欢你" # 可修改:显示的文字内容
(3)爱心粒子系统
class Heart:def __init__(self, generate_frames=60): # generate_frames: 动画帧数self.points = set() # 爱心主要点集self.edge_points = set() # 边缘点集self.center_points = set() # 中心点集self.all_points = {} # 存储所有帧的点数据self.frame_count = generate_frames # 动画总帧数self.build(3000) # 生成3000个基础点self.setup_animation()def build(self, point_count): # point_count: 生成的基础点数量for _ in range(point_count):t = random.uniform(0, 2 * pi)x, y = self.heart_function(t)self.points.add((x, y))# 生成边缘扩散点for x, y in list(self.points):for _ in range(3): # 每个基础点生成3个边缘点nx, ny = self.scatter_inside(x, y, 0.05) # 0.05: 扩散系数self.edge_points.add((nx, ny))# 生成中心密集点point_list = list(self.points)for _ in range(5000): # 生成5000个中心点x, y = random.choice(point_list)nx, ny = self.scatter_inside(x, y, 0.2) # 0.2: 中心点扩散系数self.center_points.add((nx, ny))def heart_function(self, t, shrink_ratio=HEART_SIDE):"""爱心数学函数"""x = 16 * (sin(t) ** 3)y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))x *= shrink_ratio # 应用大小缩放y *= shrink_ratiox += HEART_CENTER_X # 平移到中心位置y += HEART_CENTER_Yreturn int(x), int(y)def scatter_inside(self, x, y, beta=0.15): # beta: 扩散强度"""在点周围随机扩散"""ratio_x = -beta * log(random.random())ratio_y = -beta * log(random.random())dx = ratio_x * (x - HEART_CENTER_X)dy = ratio_y * (y - HEART_CENTER_Y)return x - dx, y - dydef setup_animation(self):"""预计算所有帧的动画效果"""for frame in range(self.frame_count):self.calculate_frame(frame)def calculate_frame(self, frame):"""计算单帧的所有粒子位置和属性"""# 计算当前帧的动画比例 (0-1)ratio = 10 * self.animation_curve(frame / self.frame_count * 2 * pi)# 光晕效果参数halo_radius = int(4 + 6 * (1 + self.animation_curve(frame / self.frame_count * 2 * pi)))halo_count = int(4000 + 6000 * abs(self.animation_curve(frame / self.frame_count * 2 * pi) ** 2))halo_points = set()frame_points = []# 生成光晕粒子for _ in range(halo_count):t = random.uniform(0, 2 * pi)x, y = self.heart_function(t, shrink_ratio=HEART_SIDE * 0.9) # 稍小的爱心x, y = self.shrink(x, y, halo_radius) # 应用光晕半径if (x, y) not in halo_points:halo_points.add((x, y))x += random.randint(-10, 10) # 添加随机偏移y += random.randint(-10, 10)size = random.choice([1, 2, 3]) # 粒子大小color = random.choice(HEART_COLORS) # 粒子颜色frame_points.append((x, y, size, color))# 添加基础点、边缘点和中心点for points, size_range in [(self.points, (1, 3)), (self.edge_points, (1, 2)), (self.center_points, (1, 2))]:for x, y in points:nx, ny = self.calculate_position(x, y, ratio)size = random.randint(*size_range)color = self.get_color_gradient(x, y, edge=(points is self.edge_points), center=(points is self.center_points))frame_points.append((nx, ny, size, color))self.all_points[frame] = frame_pointsdef calculate_position(self, x, y, ratio):"""计算粒子在动画中的位置"""dx = ratio * self.force_function(x, y) * (x - HEART_CENTER_X) + random.randint(-1, 1)dy = ratio * self.force_function(x, y) * (y - HEART_CENTER_Y) + random.randint(-1, 1)return x - dx, y - dydef force_function(self, x, y):"""力函数,决定粒子如何移动"""return 1 / (((x - HEART_CENTER_X) ** 2 + (y - HEART_CENTER_Y) ** 2) ** 0.52)def animation_curve(self, p):"""动画曲线函数,使用正弦函数创造平滑的脉动效果"""return 0.5 * (1 + sin(p))def shrink(self, x, y, ratio):"""收缩函数,用于光晕效果"""force = -1 / (((x - HEART_CENTER_X) ** 2 + (y - HEART_CENTER_Y) ** 2) ** 0.6)dx = ratio * force * (x - HEART_CENTER_X)dy = ratio * force * (y - HEART_CENTER_Y)return x - dx, y - dydef get_color_gradient(self, x, y, edge=False, center=False):"""根据位置获取颜色渐变"""dist = ((x - HEART_CENTER_X) ** 2 + (y - HEART_CENTER_Y) ** 2) ** 0.5max_dist = ((WIDTH / 2) ** 2 + (HEIGHT / 2) ** 2) ** 0.5ratio = min(1.0, dist / max_dist)if center:return HEART_COLORS[int(ratio * len(HEART_COLORS) / 3)]elif edge:return HEART_COLORS[int(len(HEART_COLORS) / 2 + ratio * len(HEART_COLORS) / 3)]else:angle = (x - HEART_CENTER_X) / max_distreturn HEART_COLORS[int((angle + 1) / 2 * len(HEART_COLORS)) % len(HEART_COLORS)]
(4)主动画控制
""" 初始化方法 """
def __init__(self, root):self.root = root# 设置窗口属性self.canvas = tk.Canvas(...) # 创建画布self.heart = Heart() # 创建爱心粒子系统self.current_frame = 0 # 当前帧self.create_text_effect() # 创建文字效果self.animate() # 开始动画""" 动画循环 """
def animate(self):self.canvas.delete("particles") # 清除上一帧粒子# 绘制当前帧的所有粒子for x, y, size, color in self.heart.all_points[self.current_frame]:self.canvas.create_rectangle(x, y, x+size, y+size, fill=color, tags="particles")# 每3帧更新一次文字效果if self.current_frame % 3 == 0:self.update_text_color()# 计算下一帧self.current_frame = (self.current_frame + 1) % self.heart.frame_countself.root.after(int(1000/FPS), self.animate) # 设置下一帧
二:跳动爱心
1.效果展示
通过数学函数绘制一个爱心形状,并添加了众多动态效果
2.完整代码
import tkinter as tk
import random
from math import sin, cos, pi, log
from tkinter.constants import *width, height = 800, 600
heartx, hearty = width / 2, height / 2
side = 11
heartcolor = "#FF85A2"
bg_color = "#1A1A2E" class Heart:def __init__(self, generate_frame=20):self._points = set()self._edge_diffusion_points = set()self._center_diffusion_points = set()self.all_points = {}self.build(2000)self.generate_frame = generate_framefor frame in range(generate_frame):self.calc(frame)def build(self, number):for _ in range(number):t = random.uniform(0, 2 * pi)x, y = heart_function(t)self._points.add((x, y))for _x, _y in list(self._points):for _ in range(3):x, y = scatter_inside(_x, _y, 0.05)self._edge_diffusion_points.add((x, y))point_list = list(self._points)for _ in range(8000): x, y = random.choice(point_list)x, y = scatter_inside(x, y, 0.17)self._center_diffusion_points.add((x, y))@staticmethoddef calc_position(x, y, ratio):force = 1 / (((x - heartx) ** 2 + (y - hearty) ** 2) ** 0.520)dx = ratio * force * (x - heartx) + random.randint(-1, 1)dy = ratio * force * (y - hearty) + random.randint(-1, 1)return x - dx, y - dydef calc(self, generate_frame):ratio = 10 * curve(generate_frame / 10 * pi)halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))all_points = []heart_halo_point = set()for _ in range(halo_number):t = random.uniform(0, 2 * pi)x, y = heart_function(t, shrink_ratio=11.6)x, y = shrink(x, y, halo_radius)if (x, y) not in heart_halo_point:heart_halo_point.add((x, y))x += random.randint(-14, 14)y += random.randint(-14, 14)size = random.choice((1, 2, 2))all_points.append((x, y, size, random.choice(["#FFD700", "#FFA07A", "#FFB6C1", "#FFC0CB"])))for x, y in self._points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 3)all_points.append((x, y, size, heartcolor))warm_colors = ["#FFB6C1", "#FFC0CB", "#FFD700", "#FFA07A", "#FF8C66", "#FFA500"]for x, y in self._edge_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size, random.choice(warm_colors)))for x, y in self._center_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size, random.choice(warm_colors)))self.all_points[generate_frame] = all_pointsdef render(self, render_canvas, render_frame):for x, y, size, color in self.all_points[render_frame % self.generate_frame]:render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=color)def heart_function(t, shrink_ratio=side):x = 16 * (sin(t) ** 3)y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))x *= shrink_ratioy *= shrink_ratiox += heartxy += heartyreturn int(x), int(y)def scatter_inside(x, y, beta=0.15):ratio_x = - beta * log(random.random())ratio_y = - beta * log(random.random())dx = ratio_x * (x - heartx)dy = ratio_y * (y - hearty)return x - dx, y - dydef shrink(x, y, ratio):force = -1 / (((x - heartx) ** 2 + (y - hearty) ** 2) ** 0.6)dx = ratio * force * (x - heartx)dy = ratio * force * (y - hearty)return x - dx, y - dydef curve(p):return 2 * (2 * sin(4 * p)) / (2 * pi)def draw(main, render_canvas, render_heart, render_frame=0):render_canvas.delete('all')render_heart.render(render_canvas, render_frame)main.after(50, draw, main, render_canvas, render_heart, render_frame + 1) def main():root = tk.Tk()root.title("爱心代码")root.resizable(False, False)screenwidth = root.winfo_screenwidth()screenheight = root.winfo_screenheight()x = (screenwidth - width) // 2y = (screenheight - height) // 2 - 66root.geometry(f"{width}x{height}+{x}+{y}")canvas = tk.Canvas(root, bg=bg_color, width=width, height=height, highlightthickness=0)canvas.pack()heart = Heart()draw(root, canvas, heart)root.mainloop()if __name__ == '__main__':main()
3.代码分析
(1)导入模块和全局变量设置
import tkinter as tk
import random
from math import sin, cos, pi, log
from tkinter.constants import *# 画布尺寸和爱心位置
width, height = 800, 600
heartx, hearty = width / 2, height / 2
side = 11 # 爱心大小比例系数
heartcolor = "#FF85A2" # 主爱心颜色(粉红色)
bg_color = "#1A1A2E" # 背景颜色(深蓝色)
(2)Heart类 - 核心动画逻辑
class Heart:def __init__(self, generate_frame=20):""" 初始化爱心动画参数:generate_frame: 动画生成的帧数,影响动画长度"""self._points = set() # 爱心边缘点集合self._edge_diffusion_points = set() # 边缘扩散点集合self._center_diffusion_points = set() # 中心扩散点集合self.all_points = {} # 存储所有帧的点数据self.build(2000) # 初始构建2000个基础点self.generate_frame = generate_frame # 动画帧数# 预计算所有帧的数据for frame in range(generate_frame):self.calc(frame)def build(self, number):""" 构建爱心的基础点参数:number: 生成的基础点数量"""for _ in range(number):t = random.uniform(0, 2 * pi)x, y = heart_function(t)self._points.add((x, y))# 创建边缘扩散效果for _x, _y in list(self._points):for _ in range(3):x, y = scatter_inside(_x, _y, 0.05)self._edge_diffusion_points.add((x, y))# 创建中心扩散效果(更多点)point_list = list(self._points)for _ in range(8000): x, y = random.choice(point_list)x, y = scatter_inside(x, y, 0.17)self._center_diffusion_points.add((x, y))@staticmethoddef calc_position(x, y, ratio):""" 计算点的动态位置参数:x, y: 原始坐标ratio: 移动比例,控制动画幅度"""force = 1 / (((x - heartx) ** 2 + (y - hearty) ** 2) ** 0.520)dx = ratio * force * (x - heartx) + random.randint(-1, 1)dy = ratio * force * (y - hearty) + random.randint(-1, 1)return x - dx, y - dydef calc(self, generate_frame):""" 计算每一帧的点位置和属性参数:generate_frame: 当前帧序号"""# 计算当前帧的动画比例和光晕参数ratio = 10 * curve(generate_frame / 10 * pi)halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))all_points = []heart_halo_point = set()# 生成光晕效果点for _ in range(halo_number):t = random.uniform(0, 2 * pi)x, y = heart_function(t, shrink_ratio=11.6)x, y = shrink(x, y, halo_radius)if (x, y) not in heart_halo_point:heart_halo_point.add((x, y))x += random.randint(-14, 14) # 添加随机偏移y += random.randint(-14, 14)size = random.choice((1, 2, 2)) # 随机大小all_points.append((x, y, size, random.choice(["#FFD700", "#FFA07A", "#FFB6C1", "#FFC0CB"])))# 添加基础爱心点for x, y in self._points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 3)all_points.append((x, y, size, heartcolor))# 添加边缘扩散点(使用暖色调)warm_colors = ["#FFB6C1", "#FFC0CB", "#FFD700", "#FFA07A", "#FF8C66", "#FFA500"]for x, y in self._edge_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size, random.choice(warm_colors)))# 添加中心扩散点for x, y in self._center_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size, random.choice(warm_colors)))# 存储当前帧的所有点self.all_points[generate_frame] = all_pointsdef render(self, render_canvas, render_frame):""" 渲染当前帧到画布参数:render_canvas: Tkinter画布对象render_frame: 当前帧序号"""# 循环显示预计算的帧for x, y, size, color in self.all_points[render_frame % self.generate_frame]:render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=color)