Python之pygame学习(播放声⾳和⾳效)⽂章⽬录
⼀、相关⽅法
1. pygame.mixer.Sound():播放⾳效
⾳频可以可以是 OGG ⾳频⽂件或者 WAV ⾳频⽂件
王千源事件
2. pygame.mixer.music:播放背景⾳乐
⼆、⽰例1:Music
import pygame
import sys
from pygame.locals import*
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load("bg_music.wav")# 载⼊⾳乐
pygame.mixer.music.set_volume(0.2)# 设置⾳量为 0.2
pygame.mixer.music.play()# 播放⾳乐
dog_sound = pygame.mixer.Sound("dog.wav")
dog_sound.set_volume(0.2)
乌龟组合bird_sound = pygame.mixer.Sound("bird.wav")
bird_sound.set_volume(0.2)
bg_size = width,height =300,200
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("Music Demo")
pause =False
pause_imgae = pygame.image.load("pause.png").convert_alpha() unpause_imgae = pygame.image.load("keep.png").convert_alpha()
pause_rect = _rect()
兽兽不雅照片 = width //2,height //2
clock = pygame.time.Clock()
while True:
for event in ():
pe== QUIT:
pe== MOUSEBUTTONDOWN:
# button(1:左键;2:中间键;3:右键)
if event.button ==1:#表⽰点击⿏标左键
dog_sound.play()
if event.button ==3:# 表⽰点击⿏标右键
bird_sound.play()
pe== KEYDOWN:
if event.key == K_SPACE:
pause =not pause
screen.fill((255,255,255))
if pause:
screen.blit(pause_imgae,pause_rect)
else:
screen.blit(unpause_imgae,pause_rect)
pygame.display.flip()
clock.tick(30)
三、⽰例2:给摩擦⼩球游戏添加背景⾳乐和⾳效
import pygame
import sys
from pygame.locals import*
en dag tilbage
from random import*一路顺风吉他谱
# pygame.sprite.Sprite 代表游戏对象的简单基类
class Ball(pygame.sprite.Sprite):
def__init__(self,image,position,speed,bg_size):
# pygame.sprite.Sprite.__init__(self)
# pygame.sprite.Sprite.__init__(self)
super().__init__()# 注意 super 的格式
self.image = pygame.image.load(image).convert_alpha()
< = _rect()
self.speed = speed
self.width,self.height = bg_size[0],bg_size[1]
# 由于采⽤ collide_circle()⽅法检测圆的碰撞,所以需要设置 radius 属性
self.radius = width /2
def move(self):
< = ve(self.speed)
# 这⾥要的效果是⼩球整个超出屏幕左侧的应对⽅案
ight <0:
left > self.width:
bottom <0:
op > self.height:
def main():
pygame.init()
ball_image ="gray_ball.png"
bg_image ="background.png"
running =True
# 添加背景⾳乐
pygame.mixer.music.load("bg_music.wav")
pygame.mixer.music.play()
# 添加⾳效
win_sound = pygame.mixer.Sound("dog.wav")
loser_sound = pygame.mixer.Sound("loser.wav")
laugh_sound = pygame.mixer.Sound("laugh.wav")
hole_sound = pygame.mixer.Sound("hole.wav")
# ⾳乐播放完毕,游戏结束
Gameover = USEREVENT # ⾃定义⼀个⽤户事件
pygame.mixer.music.set_endevent(Gameover)
#根据背景图⽚设置游戏界⾯的尺⼨
bg_size = width,height =1024,681
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("Play the ball")
background = pygame.image.load(bg_image).convert_alpha()
# ⽤来存放⼩球对象的列表
balls =[]
group = pygame.sprite.Group()
# 创建五个⼩球,位置随机,速度随机
for i in range(5):
绿动
# 球的⼤⼩是 100 x 100 防⽌越出边界,所以要宽和⾼减 100 的范围以内
position = randint(0,width-100),randint(0,height-100)
speed =[randint(-10,10),randint(-10,10)]
ball = Ball(ball_image,position,speed,bg_size)
# 创建⼩球时需要检测,防⽌球与球之间发⽣重叠
# while pygame.sprite.spritecollide(ball,group,False):
while pygame.sprite.spritecollide(ball,group,False,llide_circle): # 若发⽣碰撞即有重叠,就重新分配位置
balls.append(ball)
# 新建⼀个球后,将其加⼊检测组中
group.add(ball)
clock = pygame.time.Clock()# 设置帧率
while running:
for event in ():
pe== QUIT:
pe== Gameover:
loser_sound.play()
pygame.time.delay(2000)
laugh_sound.play()
running =False
screen.blit(background,(0,0))
for each in balls:
screen.blit(each.)
for each in group:
# 检测组中每⼀个⼩球运动过程中是否发⽣碰撞
# 先将其弹出,检测完后再加⼊
# 若发⽣碰撞,就运动⽅向改为相反⽅向
if pygame.sprite.spritecollide(each,group,False,llide_circle):                each.speed[0]=-each.speed[0]
each.speed[1]=-each.speed[1]
group.add(each)
pygame.display.flip()
clock.tick(30)#最⾼每秒不超过 30 次
if __name__ =='__main__':
main()