为什么我的pygame声音文件没有播放?

时间:2013-08-20 17:33:27

标签: python audio pygame

我的pygame游戏没有播放声音,我试图以几种不同的方式播放它。我可以得到任何帮助吗?

bif="bg.bmp"
mif="Male Hero.png"

import os, sys
import pygame, sys
from pygame.locals import *

pygame.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)

walk = pygame.mixer.Sound(os.path.join("Walk.wav"))

screen=pygame.display.set_mode((400,400),pygame.FULLSCREEN,32) 
background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()

x,y=0,0
movex, movey=0,0


while True:
     for event in pygame.event.get():
        if event.type == QUIT:                                                    
            pygame.quit()
            sys.exit
        if event.type == KEYDOWN:
            if event.key == K_a:
               movex=-0.04
               walk.play
            elif event.key==K_d:
                 movex=+0.04
                 walk.play
            elif event.key==K_w:
                 movey=-0.04
                 walks.play
            elif event.key==K_s:
                 movey=+0.04
                 walk.play
            elif event.key==K_ESCAPE:
                 pygame.quit()
                 sys.exit
        if event.type==KEYUP:
             if event.key == K_a:
                movex=0
            elif event.key==K_d:
                 movex=0
            elif event.key==K_w:
                 movey=0
            elif event.key==K_s:
                 movey=0

    x+=movex
    y+=movey

    screen.blit(background,(0,0))
    screen.blit(mouse_c,(x,y))
    pygame.display.update()

这是我的整套代码,这是应该播放声音的部分。

while True:
for event in pygame.event.get():
    if event.type == QUIT:                                                    
        pygame.quit()
        sys.exit
    if event.type == KEYDOWN:
        if event.key == K_a:
           movex=-0.04
           walk.play
        elif event.key==K_d:
             movex=+0.04
             walk.play
        elif event.key==K_w:
             movey=-0.04
             walks.play
        elif event.key==K_s:
             movey=+0.04
             walk.play

任何帮助都会很好。

1 个答案:

答案 0 :(得分:3)

我认为您需要将walk.play更改为walk.play(),然后在walks代替walk的情况下解决错误:)