当前位置:   article > 正文

很火的一个动态爱心代码------名字满屏飘落

动态爱心代码

代码内容如下:

  1. import random # 导入随机库
  2. from math import sin, cos, pi, log
  3. import pygame # 导入pygame库
  4. from tkinter import * # 导入tkinter库
  5. CANVAS_WIDTH = 840 # 画布宽度
  6. CANVAS_HEIGHT = 800 # 画布高度
  7. CANVAS_CENTER_X = CANVAS_WIDTH / 2 # 画布中心坐标X
  8. CANVAS_CENTER_Y = CANVAS_HEIGHT / 2 # 画布中心坐标Y
  9. IMAGE_ENLARGE = 11 # 图片缩放比例
  10. HEART_COLOR = "red" # 爱心的颜色
  11. # 定义心形函数
  12. def heart_function(t, shrink_ratio=IMAGE_ENLARGE):
  13. x = 16 * (sin(t) ** 3)
  14. y = - (13 * cos(t) - 5 * cos(2*t) - 2 * cos(3 * t) - cos(4 * t))
  15. # 缩小
  16. x *= shrink_ratio
  17. y *= shrink_ratio
  18. # 移到画布中央
  19. x += CANVAS_CENTER_X
  20. y += CANVAS_CENTER_Y
  21. return int(x), int(y)
  22. # 定义内部扩散函数
  23. def scatter_inside(x, y, beta=0.15):
  24. ratio_x = - beta * log(random.random())
  25. ratio_y = - beta * log(random.random())
  26. dx = ratio_x * (x - CANVAS_CENTER_X)
  27. dy = ratio_y * (y - CANVAS_CENTER_Y)
  28. return x - dx, y - dy
  29. # 定义缩小函数
  30. def shrink(x, y, ratio):
  31. force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 1.6)
  32. dx = ratio * force * (x - CANVAS_CENTER_X)
  33. dy = ratio * force * (y - CANVAS_CENTER_Y)
  34. return x - dx, y - dy
  35. # 定义曲线函数
  36. def curve(p):
  37. return 2 * (2 * sin(4 * p)) / (2 * pi)
  38. # 定义Heart类
  39. class Heart:
  40. def __init__(self, generate_frame=20):
  41. self._points = set() # 原始爱心坐标集合
  42. self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合
  43. self._center_diffusion_points = set() # 中心扩散效果点坐标集合
  44. self.all_points = {} # 每帧动态点坐标
  45. self.build(2000)
  46. self.random_halo = 1000
  47. self.generate_frame = generate_frame
  48. for frame in range(generate_frame):
  49. self.calc(frame)
  50. # 生成爱心坐标集合
  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. # 计算每一帧的动态点坐标
  68. @staticmethod
  69. def calc_position(x, y, ratio):
  70. force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.5)
  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(3000 + 4800 * abs(curve(generate_frame / 10 * pi) ** 2))
  78. all_points = []
  79. # 光环
  80. heart_halo_point = set()
  81. for _ in range(halo_number):
  82. t = random.uniform(0, 2 * pi)
  83. x, y = heart_function(t, shrink_ratio=11.6)
  84. x, y = shrink(x, y, halo_radius)
  85. if (x, y) not in heart_halo_point:
  86. heart_halo_point.add((x, y))
  87. x += random.randint(-14, 14)
  88. y += random.randint(-14, 14)
  89. size = random.choice((1, 2, 2))
  90. all_points.append((x, y, size))
  91. # 轮廓
  92. for x, y in self._points:
  93. x, y = self.calc_position(x, y, ratio)
  94. size = random.randint(1, 3)
  95. all_points.append((x, y, size))
  96. # 内容
  97. for x, y in self._edge_diffusion_points:
  98. x, y = self.calc_position(x, y, ratio)
  99. size = random.randint(1, 2)
  100. all_points.append((x, y, size))
  101. for x, y in self._center_diffusion_points:
  102. x, y = self.calc_position(x, y, ratio)
  103. size = random.randint(1, 2)
  104. all_points.append((x, y, size))
  105. self.all_points[generate_frame] = all_points
  106. # 绘制爱心
  107. def render(self, render_canvas, render_frame):
  108. for x, y, size in self.all_points[render_frame % self.generate_frame]:
  109. render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
  110. # 定义绘制函数
  111. def draw(main, render_canvas, render_heart, render_frame=0):
  112. render_canvas.delete('all')
  113. render_heart.render(render_canvas, render_frame)
  114. words = []
  115. name = "小仙女" # 想要替换的名字
  116. count = 40 # 修改数量
  117. myfont='楷体' # 改成你电脑上有的字体
  118. nameFlowColor = '#cc0000' # 飘动名字的颜色,#cc0000为红色
  119. for _ in range(count):
  120. words.append(name)
  121. for i in range(len(words)):
  122. x = random.randint(0, CANVAS_WIDTH)
  123. y = random.randint(0, CANVAS_HEIGHT)
  124. color = nameFlowColor
  125. render_canvas.create_text(x, y, text=words[i], font=(myfont, 20), fill=color)
  126. main.after(500, draw, main, render_canvas, render_heart, render_frame + 1)
  127. if __name__ == '__main__':
  128. root = Tk()
  129. canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
  130. canvas.pack()
  131. heart = Heart()
  132. draw(root, canvas, heart)
  133. root.mainloop()
  134. pygame.mixer.init() # 初始化音乐播放
  135. pygame.mixer.music.load('替换音乐文件路径')
  136. pygame.mixer.music.play(-1) # 循环播放

运行效果如下:

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

闽ICP备14008679号