【问题标题】:How do I play MouseOver sound effect only once in pygame?如何在pygame中只播放一次鼠标悬停音效?
【发布时间】:2020-07-26 00:31:46
【问题描述】:

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

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()
 

【问题讨论】:

  • mouseisover = True 在 while 循环内吗?
  • 这个 sn-p 不会重现问题。它调用play 并且只通过代码。
  • 不在我的主循环之外
  • 你能发布更多代码吗?
  • 是的,我确实刷新了页面

标签: python pygame


【解决方案1】:

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

这意味着每次循环运行时,变量 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 个完全相同的按钮类。只需将按钮类放在程序的最开头,任何循环之外,您就可以一次定义按钮类。

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

【讨论】:

  • mouseisover = True 在循环之外。 stackoverflow.com/questions/63082689/…
  • 我也试过了,我仍然有同样的问题,我把 mouseisover = True 放在循环之外,它仍然比那些播放我的声音更多
  • 我下载了你的例子,button2只播放一次声音。另一方面,按钮 1 和 3 会无限期地播放声音。如果您希望所有绿色按钮都出现这种行为,请告诉我,我会更新我的答案
  • 是的,请更新它,谢谢,因为我试图让它在我的鼠标悬停在按钮上时播放一次声音,然后当我的鼠标离开按钮并返回按钮时再次播放歌曲
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2015-09-16
  • 2011-11-10
  • 2018-07-31
相关资源
最近更新 更多