因此,我正在尝试设置一个只要播放器将鼠标“悬停”在标签上方就可以产生声音效果的东西。 这是图像: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()
我希望每当鼠标悬停在标签上时都会产生声音效果,但是程序的输出是什么也不会播放或发生。
答案 0 :(得分:1)
我认为问题是event_handler()
中事件的处理,这消耗了所有事件。然后Button
类handle_event()
函数会饿死事件(也永远不会调用此函数)。最好在代码中的单个位置处理事件,然后从那里调出。
如果OP的代码确实起作用了,那么它似乎也在不断重放声音。鼠标移动会产生很多事件,因此触发声音的触发只需要在按钮矩形的初始输入处播放。而鼠标移动处理程序将在按钮rect
内的每一个事件中重新播放。
下面是一些示例代码,其中我带了您的按钮,并对其进行了修改,以“触发边缘”的方式处理悬停。这意味着它仅在“悬停”状态首次变为true时才触发,而不是在 while 变为true时触发(即“级别触发”)
注意:它实际上并不播放声音,因为我没有方便的声音文件。它只是做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()