绘制一颗会跳动的心❤ 嘿嘿
可以说是程序员的专属浪漫了吧,就像点燃一颗LED灯一样?(我瞎说的啊,大家别当真,我很菜的!!!!)
程序就在下面啦,然后我加了一些动态,会有点神秘感,有点缓冲
👇👇👇👇👇👇👇👇👇
import random
from math import sin, cos, pi, log
from tkinter import *Canvas_WIDTH = 640
Canvas_HEIGHT = 480
Canvas_CENTER_X = Canvas_WIDTH / 2
Canvas_CENTER_Y = Canvas_HEIGHT / 2
IMAGE_ENLARGE = 11
HEART_COLOR = "#FF99CC"class Heart:def __init__(self, generate_frame=40):self._points = []self._edge_diffusion_points = set()self._center_diffusion_points = set()self.all_points = []self.generate_frame = generate_frameself.build(2000)self.calc_all()def build(self, number):for _ in range(number):t = random.uniform(0, 2 * pi)x, y = heart_function(t)self._points.append((x, y))self._edge_diffusion_points.add((x, y))for _x, _y in list(self._points):for _ in range(3):x, y = scatter_inside(_x, _y, 0.3)self._edge_diffusion_points.add((x, y))point_list = list(self._points)for _ in range(4000):x, y = random.choice(point_list)x, y = scatter_inside(x, y, 0.17)self._center_diffusion_points.add((x, y))def calc_all(self):for frame in range(self.generate_frame):self.calc(frame)@staticmethoddef calc_position(x, y, ratio):force = 1 / (((x - Canvas_CENTER_X) ** 2 + (y - Canvas_CENTER_Y) ** 2) ** 0.520)dx = ratio * force * (x - Canvas_CENTER_X) + random.randint(-1, 1)dy = ratio * force * (y - Canvas_CENTER_Y) + 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)))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))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))for x, y in self._edge_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 3)all_points.append((x, y, size))self.all_points.append(all_points)for x, y in self._center_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 3)all_points.append((x, y, size))self.all_points.append(all_points)def render(self, render_canvas, render_frame, display_points, final_look=False):points_to_display = self.all_points[render_frame % self.generate_frame][:display_points]for x, y, size in points_to_display:if final_look:render_canvas.create_oval(x, y, x + size, y + size, width=0, fill=HEART_COLOR, outline='')else:render_canvas.create_oval(x, y, x + size, y + size, width=0, fill=HEART_COLOR, outline='')def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):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 += Canvas_CENTER_Xy += Canvas_CENTER_Yreturn 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 - Canvas_CENTER_X)dy = ratio_y * (y - Canvas_CENTER_Y)return x - dx, y - dydef shrink(x, y, ratio):force = -1 / (((x - Canvas_CENTER_X) ** 2 + (y - Canvas_CENTER_Y) ** 2) ** 0.6)dx = ratio * force * (x - Canvas_CENTER_X)dy = ratio * force * (y - Canvas_CENTER_Y)return x - dx, y - dydef curve(p):return 2 * (2 * sin(4 * p)) / (2 * pi)def draw_text(canvas, text, font_size, color, x, y):canvas.create_text(x, y, text=text, font=("Helvetica", font_size), fill=color)def show_message(canvas, message, next_step, delay=2000):canvas.delete("all")draw_text(canvas, message, 20, "white", Canvas_CENTER_X, Canvas_CENTER_Y)canvas.after(delay, next_step)def countdown(canvas, count):if count > 0:canvas.delete("all")draw_text(canvas, f"{count}", 40, "white", Canvas_CENTER_X, Canvas_CENTER_Y)canvas.after(1000, countdown, canvas, count - 1)else:canvas.delete("all")canvas.after(100, start_animation, canvas)def start_animation(canvas):heart = Heart()animate_heart(canvas, heart)def animate_heart(canvas, render_heart, render_frame=0, display_points=0, final_look=False):canvas.delete("all")render_heart.render(canvas, render_frame, display_points, final_look)# draw_text(canvas, "520快乐", 20, "white", Canvas_CENTER_X, Canvas_CENTER_Y + 200)if not final_look:if display_points < len(render_heart.all_points[render_frame % render_heart.generate_frame]):display_points += 10 # Increase the number of displayed pointselse:final_look = True # Switch to the final lookcanvas.after(20, animate_heart, canvas, render_heart, render_frame + 1, display_points, final_look)def prepare_start(canvas):show_message(canvas, "小吕同学专属浪漫❤", lambda: show_message(canvas, "准备好了吗?", lambda: show_message(canvas, "要开始了!", lambda: countdown(canvas, 3))))if __name__ == '__main__':root = Tk()root.title("心动")canvas = Canvas(root, bg='black', height=Canvas_HEIGHT, width=Canvas_WIDTH)canvas.pack()prepare_start(canvas)root.mainloop()