当前位置:   article > 正文

跨年迎 2024 用Python实现例子烟花,可自选音乐_pycharm2024春节代码

pycharm2024春节代码
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import pygame, math, time, random, os
  4. from sys import exit
  5. #win(窗口的w=宽,h=高)
  6. WIN_W = 900
  7. WIN_H = 600
  8. # 定义时间、显示、频率
  9. t1 = 0.10 #时间流速
  10. show_n = 0
  11. show_frequency = 0.0105 #烟花绽放频率,数值越大频率越高
  12. color_list = [
  13. [255, 50, 50],
  14. [50, 255, 50],
  15. [50, 50, 255],
  16. [255, 255, 50],
  17. [255, 50, 255],
  18. [50, 255, 255],
  19. [255, 255, 255]
  20. ]
  21. pygame.init()
  22. pygame.mixer.init()
  23. screen = pygame.display.set_mode((WIN_W, WIN_H),pygame.RESIZABLE, 32)
  24. pygame.display.set_caption("五彩烟花大放送")
  25. # 改成你自己
  26. sound_wav = pygame.mixer.music.load("D:\桌面\有趣的Python\烟花源码\周懂\青花瓷.mp3")
  27. pygame.mixer.music.play()
  28. class Fireworks():
  29. is_show = False
  30. x, y = 0, 0
  31. vy = 0
  32. p_list = []
  33. color = [0, 0, 0]
  34. v = 0
  35. def __init__(self, x, y, vy, n=300, color=[0, 255, 0], v=10):
  36. self.x = x
  37. self.y = y
  38. self.vy = vy
  39. self.color = color
  40. self.v = v
  41. for i in range(n):
  42. self.p_list.append([random.random() * 2 * math.pi, 0, v * math.pow(random.random(), 1 / 3)])
  43. def again(self):
  44. self.is_show = True
  45. self.x = random.randint(WIN_W // 2 - 350, WIN_W // 2 + 350)
  46. self.y = random.randint(int(WIN_H / 2), int(WIN_H * 3 / 5))
  47. self.vy = -40 * (random.random() * 0.4 + 0.8) - self.vy * 0.2
  48. self.color = color_list[random.randint(0, len(color_list) - 1)].copy()
  49. n = len(self.p_list)
  50. self.p_list = []
  51. for i in range(n):
  52. self.p_list.append([random.random() * 2 * math.pi, 0, self.v * math.pow(random.random(), 1 / 3)])
  53. def run(self):
  54. global show_n
  55. for p in self.p_list:
  56. p[1] = p[1] + (random.random() * 0.6 + 0.7) * p[2]
  57. p[2] = p[2] * 0.97
  58. if p[2] < 1.2:
  59. self.color[0] *= 0.9999
  60. self.color[1] *= 0.9999
  61. self.color[2] *= 0.9999
  62. if max(self.color) < 10 or self.y>WIN_H+p[1]:
  63. show_n -= 1
  64. self.is_show = False
  65. break
  66. self.vy += 10 * t1
  67. self.y += self.vy * t1
  68. fk_list = []
  69. fk_list.append(Fireworks(300, 300, -20, n=100, color=[0, 255, 0], v=10))
  70. fk_list.append(Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=11))
  71. fk_list.append(Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=12))
  72. fk_list.append(Fireworks(300, 300, -20, n=500, color=[0, 0, 255], v=12))
  73. fk_list.append(Fireworks(300, 300, -20, n=600, color=[0, 0, 255], v=13))
  74. fk_list.append(Fireworks(300, 300, -20, n=700, color=[255, 0, 0], v=15))
  75. fk_list.append(Fireworks(300, 300, -20, n=800, color=[255, 255, 0], v=18))
  76. clock = pygame.time.Clock()
  77. # 游戏主循环
  78. while True:
  79. if not pygame.mixer.music.get_busy():
  80. pygame.mixer.music.play()
  81. for event in pygame.event.get():
  82. #pygame这个不能少,否则退出还在循环
  83. if event.type == pygame.QUIT:
  84. exit()
  85. screen.fill((0, 0, 0))
  86. # 放烟花
  87. for i, fk in enumerate(fk_list):
  88. if not fk.is_show:
  89. fk.is_show = False
  90. if random.random() < show_frequency * (len(fk_list) - show_n):
  91. show_n += 1
  92. fk.again()
  93. continue
  94. fk.run()
  95. for p in fk.p_list:
  96. x, y = fk.x + p[1] * math.cos(p[0]), fk.y + p[1] * math.sin(p[0])
  97. if random.random() < 0.055:
  98. screen.set_at((int(x), int(y)),(255,255,255))
  99. else:
  100. screen.set_at((int(x), int(y)), (int(fk.color[0]), int(fk.color[1]), int(fk.color[2])))
  101. pygame.display.update()
  102. time_passed = clock.tick(50)

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

闽ICP备14008679号