当前位置:   article > 正文

用python编写会跳动的爱心(中间可写字)_python爱心代码跳动

python爱心代码跳动

今天小恋在这给大家分享一个跳动爱心的编码,大家可以用此编码给心爱的人表白噢,祝有情人终成眷属❤

下面就发放代码:

共有148行

  1. import random # 导入random模板
  2. from math import sin, cos, pi, log
  3. from tkinter import *
  4. CANVAS_WIDTH = 640 # 画布宽640
  5. CANVAS_HEIGHT = 480 # 画布高480
  6. CANVAS_CENTER_X = CANVAS_WIDTH / 2 # X的中心为1/2的宽
  7. CANVAS_CENTER_Y = CANVAS_HEIGHT / 2 # Y的中心为1/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): # 收缩X和Y的比率
  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="Hyy", bg="black", fg="#FF99CC", font="Helvetic 20 bold").place(
  125. relx=.5, rely=.5, anchor=CENTER) # 爱心中心写字
  126. Label(root, text="By 小恋 ᏊˊꈊˋᏊ", font=("Helvetica", 18), fg="#c12bec", bg="black").place(
  127. relx=.8, rely=.8, anchor=CENTER) # 在右下角署名
  128. root.mainloop() # 开始画画

爱心视频

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/192275
推荐阅读
相关标签
  

闽ICP备14008679号