Skip to main content
 首页 » 编程设计

python之鼠标悬停在物体上时的声音效果

2024年11月24日8mengfanrong

所以我正在尝试设置这个东西,只要玩家将鼠标“悬停”在标签上方,就会产生声音效果。 这是图片:https://gyazo.com/ca251495b348ab8cd27f7328c84518e8

我试过寻找解决方案,我以前找到过一个,但是在向它添加声音时它不起作用。

import math, random, sys 
import enum 
import pygame, time 
from pygame.locals import* 
from sys import exit 
from pygame import mixer 
 
#initialising python 
pygame.init() 
#pygame.mixer.init() 
pygame.mixer.pre_init(44100,16,2,4096) 
mixer.init() 
 
#define display 
W, H = 1600,900 
HW, HH = (W/2), (H/2) 
AREA = W * H 
 
 
#bsound effects 
buttonsound1 = pygame.mixer.Sound("ButtonSound1.wav")  
 
 
#initialising display 
CLOCK = pygame.time.Clock() 
DS = pygame.display.set_mode((W, H)) 
pygame.display.set_mode((0, 0), pygame.FULLSCREEN) 
FPS = 54 
progress = 0 
background = pygame.Surface(DS.get_size()) 
smallfont = pygame.font.SysFont("century gothic",25) 
 
 
#background image 
bg = pygame.image.load("Daytime.jpg").convert() 
loadingimg = pygame.image.load("LoadingScreen.png").convert() 
pause = pygame.image.load("Pause screen.png").convert() 
gameover = pygame.image.load("Game Over.png").convert() 
mainmenu = pygame.image.load("Main_Menu4.png").convert() 
#mainmenu = pygame.transform.smoothscale(mainmenu, (W,H)) 
loadingimg = pygame.transform.smoothscale(loadingimg, (W,H)) 
 
#define some colours 
BLACK = (0,0,0,255) 
WHITE = (255,255,255,255) 
green = (0,140,0) 
grey = (180,180,180) 
 
walkLeft = [pygame.image.load('Moving1.png'), pygame.image.load('Moving2.png'), pygame.image.load('Moving3.png'), pygame.image.load('Moving4.png'), pygame.image.load('Moving5.png'), pygame.image.load('Moving6.png'), pygame.image.load('Moving7.png'), pygame.image.load('Moving8.png'), pygame.image.load('Moving9.png')] 
walkRight = [] 
for i in walkLeft: 
    walkRight.append(pygame.transform.flip(i, True, False)) 
 
 
char = pygame.image.load('Moving1.png').convert_alpha() 
char2 = pygame.image.load('Moving1.png').convert_alpha() 
char2 = pygame.transform.flip(char2, True, False) 
 
x = 0 
y = 500 
height = 40 
width = 87 
vel = 5 
isJump = False 
jumpCount = 10 
left = False 
right = False 
walkCount = 0 
run = True 
 
 
# FUNCTIONS 
def event_handler(): 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            run = False 
            pygame.quit() 
            exit() 
 
# === CLASSES === (CamelCase names) 
 
class Button(): 
 
    def __init__(self, text, x=0, y=0, width=100, height=50, command=None): 
 
        self.text = text 
        self.command = command 
 
        self.image_normal = pygame.Surface((width, height)) 
        self.image_normal.fill(green) 
 
        self.image_hovered = pygame.Surface((width, height)) 
        #buttonsound1.play() 
 
        self.image = self.image_normal 
        self.rect = self.image.get_rect() 
 
        font = pygame.font.Font('freesansbold.ttf', 15) 
 
 
        text_image = font.render(text, True, WHITE) 
        text_rect = text_image.get_rect(center = self.rect.center) 
 
        self.image_normal.blit(text_image, text_rect) 
        self.image_hovered.blit(text_image, text_rect) 
 
        # you can't use it before `blit`  
        self.rect.topleft = (x, y) 
 
        self.hovered = False 
        #self.clicked = False 
 
    def update(self): 
 
        if self.hovered: 
            buttonsound1.play() 
        else: 
            self.image = self.image_normal 
 
    def draw(self, surface): 
 
        surface.blit(self.image, self.rect) 
 
    def handle_event(self, event): 
 
        if event.type == pygame.MOUSEMOTION: 
            self.hovered = self.rect.collidepoint(event.pos) 
            buttonsound1.play() 
        elif event.type == pygame.MOUSEBUTTONDOWN: 
            if self.hovered: 
                buttonsound1.play() 
                print('Clicked:', self.text) 
                if self.command: 
                    self.command() 
 
 
class GameState( enum.Enum ): 
    Loading = 0 
    Menu = 1 
    Settings = 2 
    Playing = 3 
    GameOver = 4 
 
#set the game state initially. 
game_state = GameState.Loading 
 
 
#LOADING 
def text_objects(text, color, size): 
    if size == "small": 
        textSurface = smallfont.render(text, True, color) 
 
    return textSurface, textSurface.get_rect() 
 
def loading(progress): 
    if progress < 100: 
        text = smallfont.render("Loading: " + str(int(progress)) + "%", True, WHITE) 
    else: 
        text = smallfont.render("Loading: " + str(100) + "%", True, WHITE) 
 
    DS.blit(text, [50, 660]) 
 
def message_to_screen(msh, color, y_displace = 0, size = "small"): 
    textSurf, textRect = text_objects(msg, color, size) 
    textRect.center = HW, HH + y_displace 
    DS.blit(textSurf, textRect) 
 
while (progress/4) < 100: 
    event_handler() 
    DS.blit(loadingimg, (0,0)) 
    time_count = (random.randint(1,1)) 
    increase = random.randint(1,20) 
    progress += increase 
    pygame.draw.rect(DS, green, [50, 700, 402, 29]) 
    pygame.draw.rect(DS, grey, [50, 701, 401, 27]) 
    if (progress/4) > 100: 
        pygame.draw.rect(DS, green, [50, 700, 401, 28]) 
    else: 
        pygame.draw.rect(DS, green, [50, 700, progress, 28]) 
    loading(progress/4) 
    pygame.display.flip() 
 
    time.sleep(time_count) 
 
#changing to menu 
game_state = GameState.Menu 
 
 
Menumusic = pygame.mixer.music.load("MainMenu.mp3") 
Menumusic = pygame.mixer.music.play(-1, 0.0) 
 
def main_menu(): 
    DS.blit(mainmenu, (0, 0)) 
    pygame.display.update() 
 
    btn1 = Button('Hello', 812.5, 250, 100, 50) 
    btn2 = Button('World', 825, 325, 100, 50) 
    btn3 = Button('Hello', 825, 450, 100, 50) 
    btn4 = Button('World', 825, 575, 100, 50) 
    btn5 = Button('World', 825, 675, 100, 50) 
    btn6 = Button('Hello', 825, 790, 100, 50) 
 
    while run: 
        event_handler() 
        btn1.update() 
        btn2.update() 
 
        # --- draws --- 
 
        btn1.draw(DS) 
        btn2.draw(DS) 
        btn3.draw(DS) 
        btn4.draw(DS) 
        btn5.draw(DS) 
        btn6.draw(DS) 
 
 
 
    pygame.display.update() 
 
main_menu() 

我期望的是每当鼠标经过标签时都会产生声音效果,但是,程序的输出是没有播放或发生任何事情。

请您参考如下方法:

我认为问题出在event_handler() 中的事件处理,它消耗了所有 事件。然后 Button 类 handle_event() 函数缺少事件(也永远不会调用此函数)。在代码中的单个位置处理事件并从那里调用是个好主意。

如果 OP 的代码确实有效,它看起来也在不断地重新播放声音。鼠标移动会生成很多 事件,因此播放声音的触发只需要在最初进入按钮矩形时播放。而鼠标移动处理程序在按钮的 rect 内的每个 事件上重新播放。

下面是一些示例代码,其中我采用了您的按钮,并修改了它们以以“边缘触发”的方式处理悬停。这意味着它仅在“悬停”状态首次变为真时触发,而不是当它为真时触发(这将是“水平触发”)

注意:它实际上并没有播放声音,因为我手边没有声音文件。它只是执行一个 print()。

import pygame 
 
#initialising python 
pygame.init() 
#pygame.mixer.init() 
pygame.mixer.pre_init(44100,16,2,4096) 
pygame.mixer.init() 
 
#define display 
W, H = 200, 200 
HW, HH = (W/2), (H/2) 
AREA = W * H 
 
#initialising display 
CLOCK = pygame.time.Clock() 
DS = pygame.display.set_mode((W, H)) 
#pygame.display.set_mode((0, 0), pygame.FULLSCREEN) 
pygame.display.set_mode((0, 0) ) 
FPS = 54 
 
#define some colours 
BLACK = (0,0,0,255) 
WHITE = (255,255,255,255) 
green = (0,140,0) 
grey = (180,180,180) 
 
 
class Button(): 
 
    def __init__(self, text, x=0, y=0, width=100, height=50, command=None): 
 
        self.text = text 
        self.command = command 
        self.image_normal = pygame.Surface((width, height)) 
        self.image_normal.fill(green) 
        self.image_hovered = pygame.Surface((width, height)) 
        self.image_hovered.fill(grey) 
        self.image  = self.image_normal 
        self.rect   = self.image.get_rect() 
        self.rect.x = x 
        self.rect.y = y 
 
        self.hovered= False  # is the mouse over this button? 
 
    def update(self): 
        pass 
 
    def handleMouseOver( self, mouse_position ): 
        """ If the given co-ordinate inside our rect, 
            Do all the mouse-hovering work             """ 
        # Check button position against mouse 
        # Change the state *once* on entry/exit 
        if ( self.mouseIsOver( mouse_position ) ): 
            if ( self.hovered == False ): 
                self.image = self.image_hovered 
                self.hovered = True   # edge-triggered, not level triggered 
                # Do we want to check pygame.mixer.get_busy() ? 
                if ( pygame.mixer.get_busy() == False ): 
                    print( self.text + " DO buttonsound1.play() ") 
        else: 
            if ( self.hovered == True ): 
                self.image = self.image_normal 
                self.hovered = False 
 
    def mouseIsOver( self, mouse_position ): 
        """ Is the given co-ordinate inside our rect """ 
        return self.rect.collidepoint( mouse_position ) 
 
    def draw(self, surface): 
 
        surface.blit(self.image, self.rect) 
 
 
def main_menu(): 
 
    run = True 
    btn1 = Button('Hello1',  50,  50, 40, 40) 
    btn2 = Button('World2', 100,  50, 40, 40) 
    btn3 = Button('Hello3',  50, 100, 40, 40) 
    btn4 = Button('World4', 100, 100, 40, 40) 
 
    # Put the buttons into a list so we can loop over them, simply 
    buttons = [ btn1, btn2, btn3, btn4 ] 
 
    while run: 
 
        # draw the buttons 
        for b in buttons: 
            b.draw( DS ) # --- draws --- 
 
        # Handle events 
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                run = False 
            if event.type == pygame.MOUSEMOTION: 
                mouse_position = event.pos 
                for b in buttons: 
                    b.handleMouseOver( mouse_position ) 
            elif event.type == pygame.MOUSEBUTTONDOWN: 
                mouse_position = event.pos 
                for b in buttons: 
                    if ( b.mouseIsOver( mouse_position ) ): 
                        print('Clicked:', b.text) 
                        #if b.command: 
                        #    b.command() 
 
 
        pygame.display.update() 
 
 
 
main_menu() 
pygame.quit()