Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
941 views
in Technique[技术] by (71.8m points)

pygame - MOUSEBUTTONUP vs mouse.get_pressed()

I see that there are 2 ways of detecting clicks: using MOUSEBUTTONUP and mouse.get_pressed()

Why are there 2 ways of doing this? Are they different in any way? When would a programmer use one over the other?

question from:https://stackoverflow.com/questions/65914897/mousebuttonup-vs-mouse-get-pressed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Events occur only once and are used to receive notification when a status has changed. mouse.get_pressed() is used to get the current status of the mouse buttons at any time.

The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. The pygame.event.Event() object has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked. Each mouse button is associated a value. For instance the value of the attributes is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up respectively mouse wheel down. When multiple keys are pressed, multiple mouse button events occur. Further explanations can be found in the documentation of the module pygame.event.

The current position of the mouse can be determined via pygame.mouse.get_pos(). The return value is a tuple that represents the x and y coordinates of the mouse cursor. pygame.mouse.get_pressed() returns a list of Boolean values ??that represent the state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down. When multiple buttons are pressed, multiple items in the list are True. The 1st, 2nd and 3rd elements in the list represent the left, middle and right mouse buttons. If a specific button is pressed, this can be evaluated by subscription:

buttons = pygame.mouse.get_pressed()
if buttons[0]:
    print("left button pressed")

If any button is pressed, this can be evaluated with the any function:

buttons = pygame.mouse.get_pressed()
if any(buttons):
    print("button pressed")

Further explanations can be found in the documentation of the module pygame.mouse.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...