Advertisement
Guest User

create fireworks animation

a guest
Dec 23rd, 2024
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import pygame
  2. import random
  3. import math
  4.  
  5. # constants
  6. WIDTH, HEIGHT = 800, 600
  7. FPS = 60
  8. BLACK, WHITE = (0, 0, 0), (255, 255, 255)
  9.  
  10. # Initialize Pygame
  11. pygame.init()
  12. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  13. pygame.display.set_caption("Fireworks Animation")
  14. clock = pygame.time.Clock()
  15.  
  16.  
  17. class Firework:
  18.     #constructor
  19.     def __init__(self, whistle_sound, crack_sound):
  20.         self.x = random.randint(100, WIDTH - 100)
  21.         self.y = HEIGHT
  22.         self.target_y = random.randint(100, HEIGHT // 2)
  23.         self.color = [random.randint(50, 255) for _ in range(3)]
  24.         self.exploded = False
  25.         self.particles = []
  26.         self.whistle_playing = True
  27.  
  28.         # Import sounds
  29.         self.whistle_sound = pygame.mixer.Sound("firewhistle.mp3")
  30.         self.crack_sound = pygame.mixer.Sound("fireblast.mp3")
  31.  
  32.         # Play firewhistle sound
  33.         self.whistle_sound.play()
  34.  
  35.     def update(self):
  36.         if not self.exploded:
  37.             self.y -= 5
  38.             if self.y <= self.target_y:
  39.                 self.explode()
  40.         else:
  41.             for particle in self.particles:
  42.                 particle.update()
  43.  
  44.     def draw(self):
  45.         if not self.exploded:
  46.             pygame.draw.circle(screen, self.color, (self.x, self.y), 3)
  47.         else:
  48.             for particle in self.particles:
  49.                 particle.draw()
  50.  
  51.     def explode(self):
  52.         self.exploded = True
  53.         self.whistle_sound.stop()  # Stop whistle sound when exploding
  54.         self.crack_sound.play()  # Play firecrack sound
  55.  
  56.         for _ in range(100):
  57.             angle = random.uniform(0, 2 * math.pi)
  58.             speed = random.uniform(2, 6)
  59.             dx = math.cos(angle) * speed
  60.             dy = math.sin(angle) * speed
  61.             self.particles.append(Particle(self.x, self.y, dx, dy, self.color))
  62.  
  63.        
  64.  
  65.  
  66. class Particle:
  67.     def __init__(self, x, y, dx, dy, color): #constructor
  68.         self.x = x
  69.         self.y = y
  70.         self.dx = dx
  71.         self.dy = dy
  72.         self.lifetime = random.randint(40, 100)
  73.         self.color = color
  74.         self.size = random.randint(2, 4)
  75.  
  76.     def update(self):
  77.         self.x += self.dx
  78.         self.y += self.dy
  79.         self.dy += 0.1  #gravityeffect
  80.         self.lifetime -= 1
  81.  
  82.     def draw(self):
  83.         if self.lifetime > 0:
  84.             alpha = max(0, int((self.lifetime / 100) * 255))
  85.             color_with_alpha = (self.color[0], self.color[1], self.color[2], alpha)
  86.             pygame.draw.circle(screen, color_with_alpha, (int(self.x), int(self.y)), self.size)
  87.  
  88. running = True
  89. fireworks = []
  90. while running:
  91.     screen.fill(BLACK)
  92.  
  93.     for event in pygame.event.get():
  94.         if event.type == pygame.QUIT:
  95.             running = False
  96.  
  97.     # Add new fireworks randomly (1/20 chance)
  98.     if random.randint(1, 20) == 1:
  99.         fireworks.append(Firework(firewhistle_sound, firecrack_sound))
  100.  
  101.     #draw fireworks
  102.     for firework in fireworks:
  103.         firework.update()
  104.         firework.draw()
  105.         if firework.exploded and all(p.lifetime <= 0 for p in firework.particles):
  106.             fireworks.remove(firework)
  107.  
  108.     pygame.display.flip()
  109.     clock.tick(FPS)
  110.  
  111. pygame.quit()
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement