Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pygame中仅播放一次鼠标悬停音效?_Python_Pygame - Fatal编程技术网

Python 如何在pygame中仅播放一次鼠标悬停音效?

Python 如何在pygame中仅播放一次鼠标悬停音效?,python,pygame,Python,Pygame,当我的鼠标放在绿色按钮上时,我尝试只播放一次声音,但它似乎不起作用-反复播放 mouseisover = True if greenbutton2.isOver(pos): window.blit(bls2,(0,0)) if mouseisover: mouseover.play() mouseisover = False 我的整个游戏_intro代码在游戏介绍的主循环中首先我设置了一个mouseisover变量并使其为真,然后在我的主循环中我

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

mouseisover = True
if greenbutton2.isOver(pos):
    window.blit(bls2,(0,0))
    if mouseisover:
        mouseover.play()
        mouseisover = False
我的整个游戏_intro代码在游戏介绍的主循环中首先我设置了一个mouseisover变量并使其为真,然后在我的主循环中我说如果这是真的,然后播放声音,然后在它下面我说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()
 

#开始屏幕
def game_intro():
carsound=pygame.mixer.Sound(“carsound1.wav”)#
mouseishover=True
mouseover=pygame.mixer.Sound(“lols(2.wav”)#MOUSEOVERSOUND
类按钮():
定义初始值(自身、颜色、x、y、宽度、高度、文本=“”):
self.color=颜色
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.text=文本
def绘图(自身、窗口、轮廓=无):
#调用此方法在屏幕上绘制按钮
如果概述:
pygame.draw.rect(窗口,轮廓,(self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(窗口,自我颜色,(self.x,self.y,self.width,self.height),0)
如果self.text!='':
font=pygame.font.SysFont('comicsans',60)
text=font.render(self.text,1,(0,0,0))
blit(text,(self.x+(self.width/2-text.get\u width()/2),self.y+(self.height/2-text.get\u height()/2)))
def isOver(自身,位置):
#Pos是鼠标位置或(x,y)坐标的元组
如果位置[0]>self.x和位置[0]self.y和位置[1]=600):
点[0]=random.randrange(0600)
点[1]=random.randrange(-10,-5)
时钟滴答声(FPS)
partics()
pygame.display.update()

如果没有更多的上下文,很难回答,但我的最佳猜测是该代码位于循环中

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

假设这是您的情况,您希望在循环之外初始化变量

您的代码现在是什么样子的

while(循环):
mouseishover=True
如果绿色按钮2.isOver(位置):
窗板(bls2,(0,0))
如果鼠标悬停:
mouseover.play()
mouseishover=False
你该怎么办

mouseishover=True
while(循环):
如果绿色按钮2.isOver(位置):
窗板(bls2,(0,0))
如果鼠标悬停:
mouseover.play()
mouseishover=False
然而,这意味着声音只能播放一次,永远不会再播放

更新:
您可以向button类添加如下方法:

def playSoundIfMouseIsOver(自身、位置、声音):
如果自我保护(位置):
如果不是自动结束:
播放
self.over=True
其他:
self.over=False
然后,要让它们发出嘟嘟声,只需使用鼠标的位置和要播放的声音调用
playSoundIfMouseIsOver