Pygame - Sound Objects



Use of music and sounds make any computer game more engaging. Pygame library supports this feature through pygame.mixer module. This module contains Sound class for loading Sound objects and controlling playback. All sound playback is mixed in background threads. For less laggy sound use a smaller buffer size.

To obtain Sound object from a sound file or file object, use following constructor −

pygame.mixer.Sound(filename or file object)

The Sound class defines following methods for controlling playback −

play(loops=0, maxtime=0, fade_ms=0) Begin playback of the Sound (i.e., on the computer's speakers) on an available Channel. Loops parameter is for repeated play.
stop() This will stop the playback of this Sound on any active Channels.
fadeout(time) This will stop playback of the sound after fading it out over the time argument in milliseconds.
set_volume(value) This will set the playback volume for this Sound,immediately affecting the Sound if it is playing and any future playback of this Sound. volume in the range of 0.0 to 1.0
get_length() Return the length of this Sound in seconds.

In following example, a text button is rendered at the bottom of display window. A space key fires an arrow upwards accompanied by a sound playing.

font = pygame.font.SysFont("Arial", 14)
text1=font.render(" SHOOT ", True, bg)
rect1 = text1.get_rect(midbottom=(200,300))
img=pygame.image.load("arrow.png")
rect2=img.get_rect(midtop=(200, 270))

Inside the game event loop, for a space key detected, an arrow object is place above the SHOOT button and repeatedly rendered with decrementing y coordinate. The shooting sound is also played at the same time.

sound=pygame.mixer.Sound("sound.wav")img=pygame.image.load("arrow.png")
rect2=img.get_rect(midtop=(200, 270))

if event.type == pygame.KEYDOWN:
   if event.key == pygame.K_SPACE: 18. Pygame — Sound objects
      print ("space")
      if kup==0:
         screen.blit(img, (190,y))
         kup=1
   if kup==1:
      y=y-1
      screen.blit(img, (190,y))
      sound.play()
      if y<=0:
         kup=0
         y=265

Example

Following listing demonstrates use of Sound object.

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
white = (255,255,255)
bg = (127,127,127)
sound=pygame.mixer.Sound("sound.wav")
font = pygame.font.SysFont("Arial", 14)
text1=font.render(" SHOOT ", True, bg)
rect1 = text1.get_rect(midbottom=(200,300))
img=pygame.image.load("arrow.png")
rect2=img.get_rect(midtop=(200, 270))
kup=0
psmode=True
screen = pygame.display.set_mode((400,300))
screen.fill(white)
y=265
while not done:

   for event in pygame.event.get():
      screen.blit(text1, rect1)
      pygame.draw.rect(screen, (255,0,0),rect1,2)
      if event.type == pygame.QUIT:
         sound.stop()
         done = True
      if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            print ("space")
            if kup==0:
               screen.blit(img, (190,y))
               kup=1
   if kup==1:
      y=y-1
      screen.blit(img, (190,y))
      sound.play()
      if y<=0:
         kup=0
         y=265
   pygame.display.update()
Advertisements