WishesForYou / BDWConfettiandSound.py
middha's picture
Upload 3 files
057fbc4
import pygame
import random
# Initialize pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
LIGHT_GRAY = (211, 211, 211)
BLACK = (0, 0, 0)
# Screen setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("HAPPY BIRTHDAY SIVA Animation")
# Load sound
confetti_sound = pygame.mixer.Sound('B:\Coding\Temp\BalaKrishBDayWish2U.mp3')
sound_played = False
# Font settings
message = "HAPPY BIRTHDAY SIVA"
font = pygame.font.SysFont(None, 40)
target_positions = [(WIDTH // 2 - font.size(message)[0] // 2 + font.size(message[:i])[0], HEIGHT // 2) for i, char in enumerate(message)]
current_positions = [(random.randint(0, WIDTH), random.randint(0, HEIGHT)) for _ in message]
velocities = [(random.random() - 0.5, random.random() - 0.5) for _ in message]
def move_toward(pos, target, vel):
x, y = pos
tx, ty = target
vx, vy = vel
speed = 0.00095
vx += (tx - x) * speed
vy += (ty - y) * speed
vx *= 0.95
vy *= 0.95
x += vx
y += vy
threshold = 10
if abs(x - tx) < threshold and abs(y - ty) < threshold:
x, y = tx, ty
return (x, y), (vx, vy)
class Confetti:
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)]
def __init__(self):
self.x = random.randint(0, WIDTH)
self.y = random.randint(-20, -5) # Start slightly off-screen
self.color = random.choice(self.COLORS)
self.speed = random.uniform(1, 3)
def update(self):
self.y += self.speed
if self.y > HEIGHT:
self.y = random.randint(-20, -5)
self.x = random.randint(0, WIDTH)
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.x, self.y), 5)
confetti_particles = []
# Main loop
running = True
while running:
screen.fill(LIGHT_GRAY)
all_letters_in_place = True
for i, (x, y) in enumerate(current_positions):
(new_x, new_y), (new_vx, new_vy) = move_toward((x, y), target_positions[i], velocities[i])
current_positions[i] = (new_x, new_y)
velocities[i] = (new_vx, new_vy)
color = BLACK
char_surface = font.render(message[i], True, color)
screen.blit(char_surface, (new_x, new_y))
if new_x != target_positions[i][0] or new_y != target_positions[i][1]:
all_letters_in_place = False
if all_letters_in_place:
if not sound_played:
confetti_sound.play()
sound_played = True
# Initialize confetti particles
confetti_particles = [Confetti() for _ in range(100)]
# Draw and update confetti particles
for particle in confetti_particles:
particle.update()
particle.draw(screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.time.wait(10)
pygame.quit()