我已经坚持了很长时间了。我有一门纽扣课。您已经可以选择按钮,单击按钮,然后释放按钮(这会导致事件发生)。我想在你松开按钮时发出声音,但我就是做不到。我不明白你该怎么做,我什么都试过了。换句话说,我想确保鼠标在选择按钮时被按住,如果它在那个位置释放,一个名为 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
现在我们来澄清一下,假设我用鼠标按住click按钮,将它拖到按钮的位置,然后放开,release属性不应该打开,因为首先需要将鼠标放在按钮上。
暂无答案!
目前还没有任何答案,快来回答吧!