当前位置:   article > 正文

新年烟花python(附源码)_def __init__(self, x, y, firework, colour)

def __init__(self, x, y, firework, colour)
  1. import pygame
  2. from random import randint, uniform, choice
  3. import math
  4. vector = pygame.math.Vector2
  5. gravity = vector(0, 0.3)
  6. DISPLAY_WIDTH = DISPLAY_HEIGHT = 800
  7. trail_colours = [(45, 45, 45), (60, 60, 60), (75, 75, 75),
  8. (125, 125, 125), (150, 150, 150)]
  9. dynamic_offset = 1
  10. static_offset = 5
  11. class Firework:
  12. def __init__(self):
  13. self.colour = (randint(0, 255), randint(0, 255), randint(0, 255))
  14. self.colours = (
  15. (randint(0, 255), randint(0, 255), randint(0, 255)
  16. ), (randint(0, 255), randint(0, 255), randint(0, 255)),
  17. (randint(0, 255), randint(0, 255), randint(0, 255)))
  18. self.firework = Particle(randint(0, DISPLAY_WIDTH), DISPLAY_HEIGHT, True,
  19. self.colour) # Creates the firework particle
  20. self.exploded = False
  21. self.particles = []
  22. self.min_max_particles = vector(100, 225)
  23. def update(self, win): # called every frame
  24. if not self.exploded:
  25. self.firework.apply_force(gravity)
  26. self.firework.move()
  27. for tf in self.firework.trails:
  28. tf.show(win)
  29. self.show(win)
  30. if self.firework.vel.y >= 0:
  31. self.exploded = True
  32. self.explode()
  33. else:
  34. for particle in self.particles:
  35. particle.apply_force(
  36. vector(gravity.x + uniform(-1, 1) / 20, gravity.y / 2 + (randint(1, 8) / 100)))
  37. particle.move()
  38. for t in particle.trails:
  39. t.show(win)
  40. particle.show(win)
  41. def explode(self):
  42. amount = randint(self.min_max_particles.x, self.min_max_particles.y)
  43. for i in range(amount):
  44. self.particles.append(
  45. Particle(self.firework.pos.x, self.firework.pos.y, False, self.colours))
  46. def show(self, win):
  47. pygame.draw.circle(win, self.colour, (int(self.firework.pos.x), int(
  48. self.firework.pos.y)), self.firework.size)
  49. def remove(self):
  50. if self.exploded:
  51. for p in self.particles:
  52. if p.remove is True:
  53. self.particles.remove(p)
  54. if len(self.particles) == 0:
  55. return True
  56. else:
  57. return False
  58. class Particle:
  59. def __init__(self, x, y, firework, colour):
  60. self.firework = firework
  61. self.pos = vector(x, y)
  62. self.origin = vector(x, y)
  63. self.radius = 20
  64. self.remove = False
  65. self.explosion_radius = randint(5, 18)
  66. self.life = 0
  67. self.acc = vector(0, 0)
  68. # trail variables
  69. self.trails = [] # stores the particles trail objects
  70. self.prev_posx = [-10] * 10 # stores the 10 last positions
  71. self.prev_posy = [-10] * 10 # stores the 10 last positions
  72. if self.firework:
  73. self.vel = vector(0, -randint(17, 20))
  74. self.size = 5
  75. self.colour = colour
  76. for i in range(5):
  77. self.trails.append(Trail(i, self.size, True))
  78. else:
  79. self.vel = vector(uniform(-1, 1), uniform(-1, 1))
  80. self.vel.x *= randint(7, self.explosion_radius + 2)
  81. self.vel.y *= randint(7, self.explosion_radius + 2)
  82. self.size = randint(2, 4)
  83. self.colour = choice(colour)
  84. for i in range(5):
  85. self.trails.append(Trail(i, self.size, False))
  86. def apply_force(self, force):
  87. self.acc += force
  88. def move(self):
  89. if not self.firework:
  90. self.vel.x *= 0.8
  91. self.vel.y *= 0.8
  92. self.vel += self.acc
  93. self.pos += self.vel
  94. self.acc *= 0
  95. if self.life == 0 and not self.firework: # check if particle is outside explosion radius
  96. distance = math.sqrt((self.pos.x - self.origin.x)
  97. ** 2 + (self.pos.y - self.origin.y) ** 2)
  98. if distance > self.explosion_radius:
  99. self.remove = True
  100. self.decay()
  101. self.trail_update()
  102. self.life += 1
  103. def show(self, win):
  104. pygame.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)),
  105. self.size)
  106. def decay(self): # random decay of the particles
  107. if 50 > self.life > 10: # early stage their is a small chance of decay
  108. ran = randint(0, 30)
  109. if ran == 0:
  110. self.remove = True
  111. elif self.life > 50:
  112. ran = randint(0, 5)
  113. if ran == 0:
  114. self.remove = True
  115. def trail_update(self):
  116. self.prev_posx.pop()
  117. self.prev_posx.insert(0, int(self.pos.x))
  118. self.prev_posy.pop()
  119. self.prev_posy.insert(0, int(self.pos.y))
  120. for n, t in enumerate(self.trails):
  121. if t.dynamic:
  122. t.get_pos(self.prev_posx[n + dynamic_offset],
  123. self.prev_posy[n + dynamic_offset])
  124. else:
  125. t.get_pos(self.prev_posx[n + static_offset],
  126. self.prev_posy[n + static_offset])
  127. class Trail:
  128. def __init__(self, n, size, dynamic):
  129. self.pos_in_line = n
  130. self.pos = vector(-10, -10)
  131. self.dynamic = dynamic
  132. if self.dynamic:
  133. self.colour = trail_colours[n]
  134. self.size = int(size - n / 2)
  135. else:
  136. self.colour = (255, 255, 200)
  137. self.size = size - 2
  138. if self.size < 0:
  139. self.size = 0
  140. def get_pos(self, x, y):
  141. self.pos = vector(x, y)
  142. def show(self, win):
  143. pygame.draw.circle(win, self.colour, (int(
  144. self.pos.x), int(self.pos.y)), self.size)
  145. def update(win, fireworks):
  146. for fw in fireworks:
  147. fw.update(win)
  148. if fw.remove():
  149. fireworks.remove(fw)
  150. pygame.display.update()
  151. def main():
  152. pygame.init()
  153. pygame.display.set_caption("Fireworks in Pygame")
  154. win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
  155. clock = pygame.time.Clock()
  156. fireworks = [Firework() for i in range(2)] # create the first fireworks
  157. running = True
  158. while running:
  159. clock.tick(60)
  160. for event in pygame.event.get():
  161. if event.type == pygame.QUIT:
  162. running = False
  163. if event.type == pygame.KEYDOWN: # Change game speed with number keys
  164. if event.key == pygame.K_1:
  165. fireworks.append(Firework())
  166. if event.key == pygame.K_2:
  167. for i in range(10):
  168. fireworks.append(Firework())
  169. win.fill((20, 20, 30)) # draw background
  170. if randint(0, 20) == 1: # create new firework
  171. fireworks.append(Firework())
  172. update(win, fireworks)
  173. # stats for fun
  174. # total_particles = 0
  175. # for f in fireworks:
  176. # total_particles += len(f.particles)
  177. # print(f"Fireworks: {len(fireworks)}\nParticles: {total_particles}\n\n")
  178. pygame.quit()
  179. quit()
  180. main()

查看效果: 

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

闽ICP备14008679号