'str'对象在制作随机声音选择器时没有属性'play'

时间:2015-03-05 19:47:25

标签: python pygame

我再次,昨天我问了这个问题 - Choosing a random sound Pygame并在测试我的实施时遇到了问题。

错误消息是:

Traceback (most recent call last):
    File "F:/Source Code/Python/graphicalSlotMachine/net/Toby/GSM/Main.py", line    5, in <module>
    class Main:
  File "F:/Source Code/Python/graphicalSlotMachine/net/Toby/GSM/Main.py", line 6, in Main
    fruity = Machine.fruitMachine()
  File "F:\Source Code\Python\graphicalSlotMachine\net\Toby\GSM\Machine.py", line 46, in __init__
    self.splash()
  File "F:\Source Code\Python\graphicalSlotMachine\net\Toby\GSM\Machine.py", line 91, in splash
    self.fruitMachine()
  File "F:\Source Code\Python\graphicalSlotMachine\net\Toby\GSM\Machine.py", line 191, in fruitMachine
    ResourceLoader.SoundManager.playRandomLossSound()
  File "F:\Source Code\Python\graphicalSlotMachine\net\Toby\GSM\Util\ResourceLoader.py", line 41, in playRandomLossSound
    random.choice(ResourceLoader.SoundManager.lossSounds).play()
AttributeError: 'str' object has no attribute 'play'

列表是:

    lossSounds = ["Assets//2SAD4ME.ogg"] # list of sound objects
    winSounds = ["Assets//OBAT.ogg", "Assets//DSWYFD.ogg"] 

持有错误的方法是:

@staticmethod
    def playRandomLossSound():
        random.choice(ResourceLoader.SoundManager.lossSounds).play()

最后我对这个方法的实现是:

            if reel1.reelMove == reel2.reelMove == reel3.reelMove == 0 and self.end == 0:
            if self.fruitlist[0][2] == self.fruitlist[1][2] == self.fruitlist[2][2]:
                self.message = "Congratulations, you have won 0/"
                ResourceLoader.SoundManager.playRandomWinSound(1)
                if self.fruitlist[0][2] == 1:
                    self.credits += 1
                if self.fruitlist[0][2] == 2:
                    self.credits += 3
                if self.fruitlist[0][2] == 3:
                    self.credits += 5
                if self.fruitlist[0][2] == 4:
                    self.credits += 10
                self.end = 1
            else:
                self.message = "You did not win this time, try again?"
                ResourceLoader.SoundManager.playRandomLossSound()
                self.end = 1

谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

更改您的方法,因为您的列表包含字符串:

@staticmethod
def playRandomLossSound():
    sound = random.choice(ResourceLoader.SoundManager.lossSounds)
    pygame.mixer.Sound(sound).play()
相关问题