python 爱心代码(超详细教程)赶紧给你女朋友做起来!快收藏
效果图:
代码:
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" def center_window(root, width, height): screenwidth = root.winfo_screenwidth() # 获取显示屏宽度 screenheight = root.winfo_screenheight() # 获取显示屏高度 size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2) # 设置窗口居中参数 root.geometry(size) # 让窗口居中显示 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_ratio y *= shrink_ratio # 移到画布中央 x += CANVAS_CENTER_X y += CANVAS_CENTER_Y return 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 - dy def 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 - dy def curve(p): return 2 * (2 * sin(4 * p)) / (2 * pi) 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.random_halo = 1000 self.generate_frame = generate_frame for 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(4000): x, y = random.choice(point_list) x, y = scatter_inside(x, y, 0.17) self._center_diffusion_points.add((x, y)) @staticmethod def 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 - dy def 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)) # 轮廓 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, 2) all_points.append((x, y, size)) self.all_points[generate_frame] = all_points 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)) self.all_points[generate_frame] = all_points def render(self, render_canvas, render_frame): for x, y, size in self.all_points[render_frame % self.generate_frame]: render_canvas.create_rectangle( x, y, x + size, y + size, width=0, fill=HEART_COLOR) def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0): render_canvas.delete('all') render_heart.render(render_canvas, render_frame) main.after(160, draw, main, render_canvas, render_heart, render_frame + 1) if __name__ == '__main__': root = Tk() root.title("爱心") center_window(root, CANVAS_WIDTH, CANVAS_HEIGHT) # 窗口居中显示 canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH) canvas.pack() heart = Heart() draw(root, canvas, heart) Label(root, text="I love you", bg="black", fg="#FF99CC", font="Helvetic 20 bold").place( relx=.5, rely=.5, anchor=CENTER) root.mainloop()
如果你觉得文章还不错,请大家 点赞、分享、留言 下,因为这将是我持续输出更多优质文章的最强动力!
如果想要系统学习Python、Python问题咨询,或者考虑做一些工作以外的副业,都可以扫描二维码添加微信,围观朋友圈一起交流学习。
我们还为大家准备了Python资料和副业项目合集,感兴趣的小伙伴快来找我领取一起交流学习哦!
关于Python学习指南
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!
👉Python所有方向的学习路线👈
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。(全套教程文末领取)
👉Python学习视频600合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
温馨提示:篇幅有限,已打包文件夹,获取方式在:文末
👉Python70个实战练手案例&源码👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
👉Python大厂面试资料👈
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
👉Python副业兼职路线&方法👈
学好 Python 不论是就业还是做副业赚钱都不错,但要学会兼职接单还是要有一个学习规划。
👉 这份完整版的Python全套学习资料已经上传,朋友们如果需要可以扫描下方CSDN官方认证二维码或者点击链接免费领取【保证100%免费】