您现在的位置是:首页 > 技术教程 正文

【爱心上面可以加字】Python爱心代码,简单又好看

admin 阅读: 2024-03-23
后台-插件-广告管理-内容页头部广告(手机)

目录

前言

一、爱心代码展示

 二、完整代码



前言

最近都快要被动态爱心刷屏了,今天跟大家带来不一样的爱心代码,可以在爱心中央写下你想要的字!
可以用Python来实现它,会Python的朋友们赶快收藏起来。


一、爱心代码展示
 

代码运行之后就是这样的啦,简直是表白,道歉神器!

 二、完整代码

复杂粘贴格式容易出问题,大家记得要改正一下哦。
加字的代码在后面,大家替换成自己想要的文字就可以啦!

  1. import random
  2. from math import sin, cos, pi, log
  3. from tkinter import *
  4. CANVAS_WIDTH = 640
  5. CANVAS_HEIGHT = 480
  6. CANVAS_CENTER_X = CANVAS_WIDTH / 2
  7. CANVAS_CENTER_Y = CANVAS_HEIGHT / 2
  8. IMAGE_ENLARGE = 11
  9. HEART_COLOR = "#FF99CC"
  10. def center_window(root, width, height):
  11. screenwidth = root.winfo_screenwidth() # 获取显示屏宽度
  12. screenheight = root.winfo_screenheight() # 获取显示屏高度
  13. size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) /
  14. 2, (screenheight - height) / 2) # 设置窗口居中参数
  15. root.geometry(size) # 让窗口居中显示
  16. def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
  17. x = 16 * (sin(t) ** 3)
  18. y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
  19. # 放大
  20. x *= shrink_ratio
  21. y *= shrink_ratio
  22. # 移到画布中央
  23. x += CANVAS_CENTER_X
  24. y += CANVAS_CENTER_Y
  25. return int(x), int(y)
  26. def scatter_inside(x, y, beta=0.15):
  27. ratio_x = - beta * log(random.random())
  28. ratio_y = - beta * log(random.random())
  29. dx = ratio_x * (x - CANVAS_CENTER_X)
  30. dy = ratio_y * (y - CANVAS_CENTER_Y)
  31. return x - dx, y - dy
  32. def shrink(x, y, ratio):
  33. force = -1 / (((x - CANVAS_CENTER_X) ** 2 +
  34. (y - CANVAS_CENTER_Y) ** 2) ** 0.6)
  35. dx = ratio * force * (x - CANVAS_CENTER_X)
  36. dy = ratio * force * (y - CANVAS_CENTER_Y)
  37. return x - dx, y - dy
  38. def curve(p):
  39. return 2 * (2 * sin(4 * p)) / (2 * pi)
  40. class Heart:
  41. def __init__(self, generate_frame=20):
  42. self._points = set() # 原始爱心坐标集合
  43. self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合
  44. self._center_diffusion_points = set() # 中心扩散效果点坐标集合
  45. self.all_points = {} # 每帧动态点坐标
  46. self.build(2000)
  47. self.random_halo = 1000
  48. self.generate_frame = generate_frame
  49. for frame in range(generate_frame):
  50. self.calc(frame)
  51. def build(self, number):
  52. for _ in range(number):
  53. t = random.uniform(0, 2 * pi)
  54. x, y = heart_function(t)
  55. self._points.add((x, y))
  56. # 爱心内扩散
  57. for _x, _y in list(self._points):
  58. for _ in range(3):
  59. x, y = scatter_inside(_x, _y, 0.05)
  60. self._edge_diffusion_points.add((x, y))
  61. # 爱心内再次扩散
  62. point_list = list(self._points)
  63. for _ in range(4000):
  64. x, y = random.choice(point_list)
  65. x, y = scatter_inside(x, y, 0.17)
  66. self._center_diffusion_points.add((x, y))
  67. @staticmethod
  68. def calc_position(x, y, ratio):
  69. force = 1 / (((x - CANVAS_CENTER_X) ** 2 +
  70. (y - CANVAS_CENTER_Y) ** 2) ** 0.520)
  71. dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
  72. dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
  73. return x - dx, y - dy
  74. def calc(self, generate_frame):
  75. ratio = 10 * curve(generate_frame / 10 * pi)
  76. halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
  77. halo_number = int(
  78. 3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
  79. all_points = []
  80. # 光环
  81. heart_halo_point = set()
  82. for _ in range(halo_number):
  83. t = random.uniform(0, 2 * pi)
  84. x, y = heart_function(t, shrink_ratio=11.6)
  85. x, y = shrink(x, y, halo_radius)
  86. if (x, y) not in heart_halo_point:
  87. heart_halo_point.add((x, y))
  88. x += random.randint(-14, 14)
  89. y += random.randint(-14, 14)
  90. size = random.choice((1, 2, 2))
  91. all_points.append((x, y, size))
  92. # 轮廓
  93. for x, y in self._points:
  94. x, y = self.calc_position(x, y, ratio)
  95. size = random.randint(1, 3)
  96. all_points.append((x, y, size))
  97. # 内容
  98. for x, y in self._edge_diffusion_points:
  99. x, y = self.calc_position(x, y, ratio)
  100. size = random.randint(1, 2)
  101. all_points.append((x, y, size))
  102. self.all_points[generate_frame] = all_points
  103. for x, y in self._center_diffusion_points:
  104. x, y = self.calc_position(x, y, ratio)
  105. size = random.randint(1, 2)
  106. all_points.append((x, y, size))
  107. self.all_points[generate_frame] = all_points
  108. def render(self, render_canvas, render_frame):
  109. for x, y, size in self.all_points[render_frame % self.generate_frame]:
  110. render_canvas.create_rectangle(
  111. x, y, x + size, y + size, width=0, fill=HEART_COLOR)
  112. def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
  113. render_canvas.delete('all')
  114. render_heart.render(render_canvas, render_frame)
  115. main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
  116. if __name__ == '__main__':
  117. root = Tk()
  118. root.title("爱心")
  119. center_window(root, CANVAS_WIDTH, CANVAS_HEIGHT) # 窗口居中显示
  120. canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
  121. canvas.pack()
  122. heart = Heart()
  123. draw(root, canvas, heart)
  124. Label(root, text="你想要替换的文字", bg="black", fg="#FF99CC", font="Helvetic 20 bold").place(
  125. relx=.5, rely=.5, anchor=CENTER)
  126. root.mainloop()

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索