I've been stuck with this for a very long time now. I have a button class. You can already select the button, click the button, and release the button (which causes a event to happen). I want to put sound for when you release the button, but I just can't. I don't understand how you're supposed to do that, I've tried everything. In other words, I want to make sure that the mouse is held down when it's already selecting the button, then if it releases in that position, a attribute called release will toggle to True for one frame. Then I can check if release is True, then I'd play the sound. My question: What condition do I need to check to have the release attribute toggle to True for one frame? This is my code structure:
# "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
Now just to clarify, let's assume I hold down the click button with my mouse, drag it to the button's position, then let go, the release attribute shouldn't toggle on since the mouse needs to be placed on the button to begin with.