昨天坤子哥发我一个抖音,爱心代码,太过简单

我直接打包成exe

import tkinter as tk
import random
import math

WIN_W = 200
WIN_H = 60

def heart_x(t, size=10):
    return size * 16 * (math.sin(t) ** 3)

def heart_y(t, size=10):
    return -size * (
        13 * math.cos(t)
        - 5 * math.cos(2 * t)
        - 2 * math.cos(3 * t)
        - math.cos(4 * t)
    )

def clamp(value, low, high):
    return max(low, min(value, high))

def create_tip_window(root, x, y, text, bg):
    win = tk.Toplevel(root)
    win.title("温馨提示")
    win.geometry(f"{WIN_W}x{WIN_H}+{int(x)}+{int(y)}")
    win.attributes("-topmost", True)
    win.resizable(False, False)

    label = tk.Label(
        win,
        text=text,
        bg=bg,
        font=("微软雅黑", 12),
        width=20,
        height=2
    )
    label.pack(fill="both", expand=True)

    return win

def move_window_smooth(win, start_x, start_y, end_x, end_y, steps=18, interval=8, on_done=None):
    def step(i):
        if not win.winfo_exists():
            return

        nx = start_x + (end_x - start_x) * i / steps
        ny = start_y + (end_y - start_y) * i / steps
        win.geometry(f"{WIN_W}x{WIN_H}+{int(nx)}+{int(ny)}")

        if i < steps:
            win.after(interval, lambda: step(i + 1))
        else:
            if on_done:
                on_done()

    step(0)

def close_window_fade(win, steps=10, interval=25):
    def fade(i):
        if not win.winfo_exists():
            return

        alpha = max(0.0, 1 - i / steps)
        try:
            win.attributes("-alpha", alpha)
        except:
            pass

        if i < steps:
            win.after(interval, lambda: fade(i + 1))
        else:
            if win.winfo_exists():
                win.destroy()

    fade(0)

def random_fly_target(start_x, start_y, screen_width, screen_height):
    margin = 10

    # 随机方向
    angle = random.uniform(0, 2 * math.pi)

    # 飞得更远一点,像满天飞
    min_dist = int(min(screen_width, screen_height) * 0.35)
    max_dist = int(min(screen_width, screen_height) * 0.80)
    fly_dist = random.randint(min_dist, max_dist)

    target_x = start_x + math.cos(angle) * fly_dist
    target_y = start_y + math.sin(angle) * fly_dist

    # 再加随机扰动
    target_x += random.randint(-120, 120)
    target_y += random.randint(-120, 120)

    # 限制在屏幕范围内
    target_x = clamp(target_x, margin, screen_width - WIN_W - margin)
    target_y = clamp(target_y, margin, screen_height - WIN_H - margin)

    return int(target_x), int(target_y)

if __name__ == "__main__":
    root = tk.Tk()
    root.withdraw()

    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    center_x = screen_width // 2
    center_y = screen_height // 2

    tips = [
        "顺顺利利", "早点休息", "愿所有烦恼都消失",
        "别熬夜", "今天过得开心嘛", "天冷了,多穿衣服",
        "要开心呀", "今天也要加油", "照顾好自己"
    ]

    bg_colors = [
        "lightpink", "skyblue", "lightgreen", "lavender",
        "lightyellow", "plum", "coral", "bisque", "aquamarine",
        "mistyrose", "honeydew", "lavenderblush", "oldlace"
    ]

    windows_data = []

    heart_points = []

    # 外层
    angle = 0
    while angle < 2 * math.pi:
        x = heart_x(angle, 24)
        y = heart_y(angle, 24)
        heart_points.append((x, y))
        angle += 0.06

    # 中层
    angle = 0
    while angle < 2 * math.pi:
        x = heart_x(angle, 20)
        y = heart_y(angle, 20)
        heart_points.append((x, y))
        angle += 0.08

    
    def spawn_one(index):
        if index >= len(heart_points):
            # 爱心生成完,停一下再满天飞
            root.after(700, fly_all_random)
            return

        x, y = heart_points[index]
        screen_x = int(center_x + x)
        screen_y = int(center_y + y)

        tip = random.choice(tips)
        bg = random.choice(bg_colors)

        win = create_tip_window(root, screen_x, screen_y, tip, bg)
        windows_data.append({
            "win": win,
            "x": screen_x,
            "y": screen_y
        })

        root.after(20, lambda: spawn_one(index + 1))

    def fly_all_random():
        finished = {"count": 0}
        total = len(windows_data)

        def one_done():
            finished["count"] += 1
            if finished["count"] == total:
                root.after(1200, close_one_by_one)

        for item in windows_data:
            start_x = item["x"]
            start_y = item["y"]

            target_x, target_y = random_fly_target(
                start_x, start_y,
                screen_width, screen_height
            )

            item["target_x"] = target_x
            item["target_y"] = target_y

            move_window_smooth(
                item["win"],
                start_x, start_y,
                target_x, target_y,
                steps=16,
                interval=7,
                on_done=one_done
            )

            item["x"] = target_x
            item["y"] = target_y

    def close_one_by_one(index=0):
        if index >= len(windows_data):
            root.after(500, root.destroy)
            return

        item = windows_data[index]
        win = item["win"]

        if win.winfo_exists():
            close_window_fade(win)

        root.after(40, lambda: close_one_by_one(index + 1))

    spawn_one(0)
    root.mainloop()

Categories:

Tags:

No responses yet

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注