这是一个精灵类,它在屏幕上左右移动,当它碰到边界时,它会发出“砰砰”的声音并走向相反的方向,一切工作完全正常,除了没有嘣的声音,当它击中边如何在类中定义和播放声音效果
class MySprite(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("vegetables.gif")
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.rect.left = 0
self.rect.top = 167
self.__direction = 10
def update(self):
self.rect.left += self.__direction
sound = pygame.mixer.Sound("Bounce.mp3")
sound.set_volume(0.5)
if (self.rect.left < 0) or (self.rect.right > screen.get_width()):
sound.play()
self.__direction = -self.__direction
你说有一种沉闷的声音,然后你说没有?请更具体地了解正在发生的事情的顺序。声音是否发生过一次,但没有发生过?它是否始终发生在某些边缘而不是其他边缘? – bdesham
对于初学者来说,将声音加载到'__init__'中,而不是每一帧加载到'update'中。 – kevintodisco
@bdesham我的意思是说boing应该发生,但它不起作用,现在没有boing –