Python - 小玩意 - 圣诞树&背景音乐&弹窗

本是古典 何须时尚 2023-10-16 13:59 30阅读 0赞
  1. import turtle as t
  2. import tkinter as tk
  3. import pygame
  4. import random as r
  5. import threading
  6. import time
  7. # 初始化背景音乐
  8. def initialize_music():
  9. file = r"./music/周杰伦-蜗牛.mp3"
  10. pygame.mixer.init()
  11. pygame.mixer.music.load(file)
  12. pygame.mixer.music.play()
  13. # 画圣诞树
  14. def draw_christmas_tree():
  15. n = 100.0
  16. t.pensize(10)
  17. t.speed("fastest")
  18. t.screensize(888, 700, "black")
  19. t.left(90)
  20. t.forward(3 * n)
  21. t.color("orange", "yellow")
  22. t.begin_fill()
  23. t.left(126)
  24. for i in range(5):
  25. t.forward(n / 5)
  26. t.right(144)
  27. t.forward(n / 5)
  28. t.left(72)
  29. t.end_fill()
  30. t.right(126)
  31. t.color("dark green")
  32. t.backward(n * 4.8)
  33. def draw_light():
  34. if r.randint(0, 30) == 0:
  35. t.color('tomato')
  36. t.circle(6)
  37. elif r.randint(0, 30) == 1:
  38. t.color('orange')
  39. t.circle(3)
  40. else:
  41. t.width(5)
  42. t.color('dark green')
  43. def draw_tree(d, s):
  44. if d <= 0:
  45. return
  46. t.forward(s)
  47. draw_tree(d - 1, s * .8)
  48. t.right(120)
  49. draw_tree(d - 3, s * .5)
  50. draw_light()
  51. t.right(120)
  52. draw_tree(d - 3, s * .5)
  53. t.right(120)
  54. t.backward(s)
  55. draw_tree(15, n)
  56. t.backward(n / 2)
  57. # 圣诞树下的装饰
  58. def draw_decorations():
  59. def zs():
  60. for i in range(200):
  61. a = 200 - 400 * r.random()
  62. b = 10 - 20 * r.random()
  63. t.up()
  64. t.forward(b)
  65. t.left(90)
  66. t.forward(a)
  67. t.down()
  68. if r.randint(0, 1) == 0:
  69. t.color('tomato')
  70. else:
  71. t.color('wheat')
  72. t.circle(2)
  73. t.up()
  74. t.backward(a)
  75. t.right(90)
  76. t.backward(b)
  77. zs()
  78. # 画雪花
  79. def draw_snowflakes():
  80. t.hideturtle()
  81. t.pensize(2)
  82. for i in range(200):
  83. t.pencolor("white")
  84. t.penup()
  85. t.setx(r.randint(-350, 350))
  86. t.sety(r.randint(-100, 350))
  87. t.pendown()
  88. dens = 6
  89. snowsize = r.randint(1, 10)
  90. for j in range(dens):
  91. t.forward(int(snowsize))
  92. t.backward(int(snowsize))
  93. t.right(int(360 / dens))
  94. # 创建弹窗
  95. def create_popup():
  96. window = tk.Tk()
  97. width = window.winfo_screenwidth()
  98. height = window.winfo_screenheight()
  99. a = r.randrange(0, width)
  100. b = r.randrange(0, height)
  101. window.title('圣诞节快乐')
  102. window.geometry("200x50" + "+" + str(a) + "+" + str(b))
  103. tk.Label(window,
  104. text='圣诞节快乐!',
  105. bg='pink',
  106. font=('Comic Sans MS', 17),
  107. width=18, height=2).pack()
  108. window.mainloop()
  109. # 主函数
  110. def main():
  111. initialize_music()
  112. draw_christmas_tree()
  113. draw_decorations()
  114. draw_snowflakes()
  115. # 创建线程池,管理弹窗线程
  116. pool = []
  117. for i in range(100):
  118. thread = threading.Thread(target=create_popup)
  119. pool.append(thread)
  120. thread.start()
  121. time.sleep(0.01)
  122. for thread in pool:
  123. thread.join()
  124. if __name__ == "__main__":
  125. main()

发表评论

表情:
评论列表 (有 0 条评论,30人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python实现背景音乐

    用Python实现背景音乐 伴随着音乐的节奏,我们的生活不再单调。如果你也想给你的程序添加音乐,这篇文章将会为你提供帮助。本文将介绍如何使用Python来实现背景音乐功能。