如何解决释放按钮时发出声音 类Button:脚本
我已经被这个问题困扰了很长时间了。我有一个按钮类。您已经可以选择按钮,单击按钮,然后释放按钮(这会导致事件发生)。我想在你松开按钮时发出声音,但我不能。我不明白你应该怎么做,我已经尝试了一切。换句话说,我想确保鼠标在已经选择按钮时被按住,然后如果它在那个位置释放,一个名为 release
的属性将在一帧内切换为 True。然后我可以检查 release 是否为 True,然后我会播放声音。我的问题:我需要检查什么条件才能将 release
属性切换为 True 一帧?这是我的代码结构:
# "mouse_down" is a global for when the mouse is being held down
# "clicked" is a global variable for the first frame at which the mouse is held down
# I also have a "release" global variable for the frame that the mouse stops clicking,this variable isn't currently being used.
class Button:
def __init__(self,pos,size,font,text,colors):
"""
colors[n]
0: fill unselected
1: border unselected
2: text unselected
3: fill selected
4: border selected
5: text selected
6: fill clicked
7: border clicked
8: text
"""
self.pos = pos
self.size = size
self.font = font
self.text = text
self.colors = colors
self.selected = False
self.clicked = False
self.release= False
def update(self):
# If the mouse is hovering on the button
if (pygame.mouse.get_pos()[0] > button.pos[0] and pygame.mouse.get_pos()[0] < button.pos[0] + button.size[0]) and (pygame.mouse.get_pos()[1] > button.pos[1] and pygame.mouse.get_pos()[1] < button.pos[1] + button.size[1]):
self.selected = True
if clicked:
self.clicked = True
if not mouse_down:
self.clicked = False
else:
self.selected = False
self.clicked = False
现在只是为了澄清,让我们假设我用鼠标按住单击按钮,将其拖到按钮的位置,然后松开,释放属性不应打开,因为鼠标需要放在按钮上才能开始。
解决方法
如果鼠标与按钮发生碰撞,最好使用 pygame.rect.collidepoint
而不是
if (pygame.mouse.get_pos()[0] > button.pos[0] and pygame.mouse.get_pos()[0] < button.pos[0] + button.size[0]) and (pygame.mouse.get_pos()[1] > button.pos[1] and pygame.mouse.get_pos()[1] < button.pos[1] + button.size[1]):
工作原理:
- 主循环:获取
MOUSEBUTTONUP
事件(释放鼠标按钮时发生) - 将此事件发送到按钮
- 如果实际鼠标点击 (
MOUSEBUTTONDOWN
) 发生在按钮中,也发送 - 如果两个事件都为 True,则播放声音
在按钮类中:
-
__init__
:生成碰撞区域+文本Surface
-
update
:绘制矩形,然后将文本居中,然后检查是否碰撞 + 事件 + 按下按钮
类Button
:
class Button:
def __init__(self,pos,size,font,text,color):
self.rect = Rect(pos,size) #
self.text = font.render(text,color) # text surface
self.button_down_on = False
def update(self,release_event):
# draw the button
pygame.draw.rect(screen,(100,100,100),self.rect)
width,height = self.text.get_size()
screen.blit(self.text,(int(self.rect.x + self.rect.width / 2 - width / 2),int(self.rect.y + self.rect.height / 2 - height / 2)))
# check collisions
if release_event and self.rect.collidepoint(pygame.mouse.get_pos()):
if self.button_down_on: # if the mouse began to press on this button
print('play sound')
脚本
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,480))
button = Button((10,10),(200,40),pygame.font.SysFont('consolas',20),'Test',(127,255,255))
running = True
while running:
release_event = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONDOWN:
# check if the click begin on a button
button.button_down_on = button.rect.collidepoint(event.pos)
elif event.type == MOUSEBUTTONUP:
release_event = True # send this event to Button.update
screen.fill((255,255))
button.update(release_event)
pygame.display.flip()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。