Playing sound in pygame does not work

Question:
My program runs fine with everything in it in Wing IDE, but it does not play the sound in Repl. below.


Repl link:
https://replit.com/team/ICS3U1Asad/Bouncing-Beach-Ball

import pygame
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode([640,480])
screen.fill([255, 255, 255])

my_ball = pygame.image.load('beach_ball.png')   #load the picture of a beach ball
pygame.mixer.music.load("Bouncing_Ball.ogg")    #sound file

x = 50 #starting coordinates
y = 50
x_speed = 1 #moving increments
y_speed = 1

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.time.delay(5)
    pygame.draw.rect(screen, [255,255,255], [x, y, 100, 100], 0)
    x = x + x_speed
    y = y + y_speed
    
    #100 is the ball's diameter. You want to make sure the ball bounces back
    #when reaches the edges. x<0 and y<0 make sure the ball does not go beyond  
    #the left and the top edges of the screen
    if x > screen.get_width() - 100 or x < 0:   
        x_speed = - x_speed                      
        pygame.mixer.music.play()               
    if y > screen.get_height() - 100 or y < 0:  
        y_speed = -y_speed                      
        pygame.mixer.music.play()
    screen.blit(my_ball, [x, y])
    pygame.display.flip()
pygame.quit()


Check out Playing audio on Replit | Replit Docs

1 Like

This might be helpful :