前言
俺又来啦!大家肯定是满世界找电视剧同款爱心代码,我看很多都是HTML,but!为了不是太过于跟风!so!整一个python同款也是可以的啦!
(记得!!!!一定要安装一个python环境,去下载一个pycharm,可以从b站看怎么下载和配置变量名,还要记得下载相应的库!)
正文
图片:
代码来啦!!!
(对了对了!复制完一定要看看下面的总结喔!!!)
import randomimport timefrom math import sin, cos, pi, log, tanfrom tkinter import *CANVAS_WIDTH = 640CANVAS_HEIGHT = 480CANVAS_CENTER_X = CANVAS_WIDTH / 2CANVAS_CENTER_Y = CANVAS_HEIGHT / 2IMAGE_ENLARGE = 11HEART_COLOR = "#e86184"WINDOWS_TITLE = 'Love'HEART_CENTER_TEXT = ' 'HEART_CENTER_TEXT_COLOR = '#FFD700'def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE): x = 14.6 * (sin(t) ** 3) y = -(14.5 * cos(t) - 4 * cos(2 * t) - 2 * cos(3 * t) - 0.5 * 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 - 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 heart_curve(p): return curve(p, (.4, .5, .2, .6)) # 爱心的贝塞尔曲线参数def heart_halo_curve(p): return curve(p, (.73, .55, .59, .92)) # 光环的贝塞尔曲线参数def curve(p, b): t = sin(p) p0 = b[0] p1 = b[1] p2 = b[2] p3 = b[3] t1 = (1 - t) t2 = t1 * t1 t3 = t2 * t1 r = 2 * (2 * sin(4 * p)) / (2 * pi) return rclass 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_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.24) 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.47) 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 * heart_curve(generate_frame / 10 * pi) halo_radius = int(4 + 6 * (1 + heart_halo_curve(generate_frame / 10 * pi))) halo_number = int(3000 + 4000 * abs(heart_halo_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=heart_halo_curve(generate_frame / 10 * pi) + 11) x, y = shrink(x, y, halo_radius) if (x, y) not in heart_halo_point: heart_halo_point.add((x, y)) random_int_range = int(27 + heart_halo_curve(generate_frame / 10 * pi) * 4) x += random.randint(-random_int_range, random_int_range) y += random.randint(-random_int_range, random_int_range) size = random.choice((1, 1, 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)) 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 frame_count(self): return self.generate_framedef draw(main: Tk, render_canvas_dict: dict, render_heart: Heart, render_frame=0): frame_index = render_frame % render_heart.frame_count() last_frame_index = (frame_index + render_heart.frame_count() - 1) % render_heart.frame_count() if last_frame_index in render_canvas_dict: render_canvas_dict[last_frame_index].pack_forget() if frame_index not in render_canvas_dict: canvas = Canvas( main, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH ) canvas.pack() render_heart.render(canvas, render_frame) canvas.create_text( CANVAS_CENTER_X, CANVAS_CENTER_Y, text=HEART_CENTER_TEXT, fill=HEART_CENTER_TEXT_COLOR, font=('楷体', 48, 'bold') # 字体 ) render_canvas_dict[frame_index] = canvas else: render_canvas_dict[frame_index].pack() main.after( 10, draw, main, render_canvas_dict, render_heart, render_frame + 1)if __name__ == '__main__': print('Waiting...') start_time = time.time() root = Tk() root.title(WINDOWS_TITLE) canvas_dict = {} heart = Heart(60) draw(root, canvas_dict, heart) end_time = time.time() print('造爱耗时 {:.2f} 秒 :'.format(end_time - start_time)) root.mainloop()
总结
注释:代码的第十二行' '里可以写她的名字啦!
大家一定要珍惜自己的女朋友啦,一个人和另一人相遇可能性是千万分之一,成为朋友是两亿分之一,一个人要爱上另一个人的概率是五亿分之一,而如果要成为伴侣,概率是十五亿分只一。如果要白头偕老的话,需要花费二十多年的时间等待,还得用六七十年的时间来完成。一个人对另一个人说一声“我爱你”,需要消耗两个苹果所提供的热量。这一切都显得那么不简单。
所以!!!!!给自己的女友写代码的朋友!一定得给我对她好!要不然!诅咒你这辈子再也碰不到比她好的女孩子啦!!!!!
来源地址:https://blog.csdn.net/joker_man1/article/details/127922440