首页 > 解决方案 > 如何在pygame中只播放一次鼠标悬停音效?

问题描述

当我的鼠标悬停在绿色按钮上时,我只尝试播放一次声音,但它似乎不起作用——重复播放。

mouseisover = True
if greenbutton2.isOver(pos):
    window.blit(bls2,(0,0))
    if mouseisover:
        mouseover.play()
        mouseisover = False

我的整个游戏_intro代码在我的游戏介绍的主循环中首先我设置了一个mouseisover变量并将其设为True然后在我的主循环中我说如果那是True然后播放声音然后在它下面我说mouseisover = False但它仍然播放我的声音一次又一次不停

# start screen
def game_intro():

    carsound = pygame.mixer.Sound("carsound1.wav") # 
    

    mouseisover = True
    mouseover = pygame.mixer.Sound("lols (2).wav") # MOUSEOVERSOUND


    class button():
        def __init__(self, color, x,y,width,height, text=''):
            self.color = color
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.text = text

        def draw(self,window,outline=None):
            #Call this method to draw the button on the screen
            if outline:
                pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
                
            pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
            
            if self.text != '':
                font = pygame.font.SysFont('comicsans', 60)
                text = font.render(self.text, 1, (0,0,0))
                window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))

        def isOver(self, pos):
            #Pos is the mouse position or a tuple of (x,y) coordinates
            if pos[0] > self.x and pos[0] < self.x + self.width:
                if pos[1] > self.y and pos[1] < self.y + self.height:
                    return True
                
            return False
        
    white = (250,250,250)
    greenbutton = button((0,255,0),60,449,200,80, 'Click Me )')
    greenbutton2 = button((0,255,0),50,518,200,80, 'Click Me )')
    greenbutton3 = button((0,255,0),50,600,200,70, 'Click Me )')

    bls2 = pygame.image.load("about2.png")
    bls3 = pygame.image.load("credits25.png")
    bls4 = pygame.image.load("playgame1.png")


    def fade(width, height): 
        fade = pygame.Surface((width, height))
        fade.fill((0,0,0))
        for alpha in range(0, 100):
            fade.set_alpha(alpha)
            window.blit(fade, (0,0))
            pygame.display.update()
            pygame.time.delay(15)

            
    def redraw():

        bls = pygame.image.load("bgs.png")
        window.blit(bls,(0,0))





    # this makes it             
    snow_list=[]
    no_of_circles=100;
    clock = pygame.time.Clock()
    FPS = 60
    clock.tick(FPS)
    for i in range(no_of_circles):
        x = random.randrange(0, 800)
        y = random.randrange(0, 700)
        snow_list.append([x,y])




            
    red = (200,0,0)
    green = (255,250,250)
    bright_red = (255,250,0)
    bright_green = (0,255,0)
    clock = pygame.time.Clock()
    intro = True
    while intro:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                intro = False
                pygame.quit()

       
        redraw()


        pos = pygame.mouse.get_pos()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if greenbutton.isOver(pos):
                fade(800,800)
                main_loop()


        pos = pygame.mouse.get_pos()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if greenbutton2.isOver(pos):
                window.blit(bls2,(0,0))
                fade(800,800)
                menu()

        if greenbutton2.isOver(pos):
            window.blit(bls2,(0,0))
            if mouseisover:
                mouseover.play()
                mouseisover = False


        if greenbutton3.isOver(pos):
            mouseover.play()
            window.blit(bls3,(0,0))

        if greenbutton.isOver(pos):
            mouseover.play()
            window.blit(bls4,(0,0))

            
        pos = pygame.mouse.get_pos()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if greenbutton3.isOver(pos):
                fade(800,800)
                creditss()





    # GAME INTRO IMAGE




        for point in snow_list:
            point[1]+=1
            pygame.draw.circle(window, (255,255,255), point, 2)

            if(point[1] >= 600):
                point[0] = random.randrange(0, 600)
                point[1] = random.randrange(-10, -5)

        clock.tick(FPS)


        partics()

      

        pygame.display.update()
 

标签: pythonpygame

解决方案


没有更多上下文很难回答,但我最好的猜测是这段代码位于循环中。

这意味着每次循环运行时,变量 mouseisover 都被赋值为 True,丢弃之前的值。

假设这是您想要在循环之外初始化变量的情况。

你的代码现在是什么样子

    while (loop):
       mouseisover = True
       if greenbutton2.isOver(pos):
          window.blit(bls2,(0,0))
          if mouseisover:
             mouseover.play()
             mouseisover = False

你应该做什么

    mouseisover = True
    while (loop):
       if greenbutton2.isOver(pos):
          window.blit(bls2,(0,0))
          if mouseisover:
             mouseover.play()
             mouseisover = False

然而,这意味着声音只会播放一次,并且永远不会再播放。

更新:
您可以在按钮类中添加这样的方法:

    def playSoundIfMouseIsOver(self, pos, sound):
        if self.isOver(pos):
            if not self.over:
                sound.play()
                self.over = True
        else:
            self.over = False

然后让它们发出哔哔声,只需playSoundIfMouseIsOver使用鼠标的位置和您要播放的声音进行调用

        greenbutton2.playSoundIfMouseIsOver(pos, mouseover)
        greenbutton3.playSoundIfMouseIsOver(pos, mouseover)
        greenbutton.playSoundIfMouseIsOver(pos, mouseover)

这只是记住了 isOver 的最后一个值,如果它从 False 变为 True,它就会播放声音。

这些更改将要求您更改使用新方法处理播放声音的每一个

如果您有更多按钮,您可以将所有按钮放在一个列表中,然后在列表上迭代以对每个按钮执行相同的操作

创建列表(在创建不同按钮后放置):

myButtonList = [greenbutton, greenbutton2, greenbutton3]

在该列表上进行迭代(替换三个类似的行):

for myButton in myButtonList:
            myButton.playSoundIfMouseIsOver(pos, mouseover)

另外,我在您的代码中看到了一些奇怪的东西,您有 4 个完全相同的按钮类。您可以通过简单地将按钮类放在程序的最开始,在任何循环之外来定义一次。

如果您将类定义放在循环中,它们将只能在该循环中访问!


推荐阅读