Pygame 简明教程

Pygame - Sound Objects

使用音乐和声音可以让任何计算机游戏更具吸引力。Pygame 库通过 pygame.mixer 模块支持此功能。该模块包含 Sound 类,用于加载 Sound 对象和控制播放。所有声音播放都在后台线程中混合。为了减少延迟声音,请使用较小的缓冲区大小。

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.

要从声音文件或文件对象获取 Sound 对象,请使用以下构造函数:

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

pygame.mixer.Sound(filename or file object)

Sound 类定义以下控制播放的方法:

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))

在游戏事件循环中,对于检测到的空格键,一个箭头对象被放置在 SHOOT 按钮的上方,并以递减的 y 坐标反复渲染。射击的声音同时也被播放。

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

下面的清单展示了 Sound 对象的使用。

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()