External Exam Download Resources Web Applications Games Recycle Bin

Pygame Music

sound.py

import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
done = False

pygame.mixer.music.load('happy.mp3')
pygame.mixer.music.play(-1) # times repeat:
                            # -1 = infinite,
                            # 0  = play song once
#pygame.mixer.music.stop()
coin_sound = pygame.mixer.Sound('coin.wav') # load sound

while not done:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      done = True
    elif event.type == pygame.KEYDOWN:
      if event.key == pygame.K_ESCAPE:
        done = True
      elif event.key == pygame.K_SPACE:
        coin_sound.play() # plays sound effect once
        
  pygame.display.flip()
pygame.quit()

save coin.wav to project folder:

coin.wav

save happy.mp3 to project folder:

happy.mp3

put all files together into project folder: