Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect if the user has double-clicked in pygame?

Tags:

python

pygame

I know I can check if there was a left click

event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT

but how can I check if they double clicked? Also is there any way to check if the user moved the scroll wheel forward or backwards?

like image 214
petabyte Avatar asked Mar 29 '12 03:03

petabyte


People also ask

How do I know if my mouse is clicking pygame?

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.

How do you know if a sprite is clicked pygame?

When you receive a mouse click event, you get the position by calling pygame. mouse. get_pos() . You can then check for a collision between a rect centered at the mouse position and your sprite's rect by calling pygame.

How do I know if a pygame button is pressed?

Detecting which key was pressed: To know which key was pressed, we have to check the event. key variable corresponds to which pygame keys. For example, the pygame key for the letter “A” is “K_a” then we will compare event. Key with K a and if it comes to be same that means the key “A” was pressed.

How does Pygame deal with mouse events?

When the display mode is set, the event queue will start receiving mouse events. The mouse buttons generate pygame. MOUSEBUTTONDOWN and pygame. MOUSEBUTTONUP events when they are pressed and released.


3 Answers

I'd just use the delta time value that clock.tick returns to increase a timer. In this example you have 0.5 seconds to double click otherwise the timer is reset.

import sys
import pygame as pg


pg.init()

screen = pg.display.set_mode((640, 480))
BLACK = pg.Color('black')
FONT = pg.font.Font(None, 32)


def game():
    clock = pg.time.Clock()
    timer = 0
    dt = 0
    running = True

    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if timer == 0:  # First mouse click.
                        timer = 0.001  # Start the timer.
                    # Click again before 0.5 seconds to double click.
                    elif timer < 0.5:
                        print('double click')
                        timer = 0

        # Increase timer after mouse was pressed the first time.
        if timer != 0:
            timer += dt
            # Reset after 0.5 seconds.
            if timer >= 0.5:
                print('too late')
                timer = 0

        screen.fill(BLACK)
        txt = FONT.render(str(round(timer, 2)), True, (180, 190, 40))
        screen.blit(txt, (40, 40))
        pg.display.flip()
        # dt == time in seconds since last tick.
        # / 1000 to convert milliseconds to seconds.
        dt = clock.tick(30) / 1000


if __name__ == '__main__':
    game()
    pg.quit()
    sys.exit()
like image 68
skrx Avatar answered Sep 23 '22 20:09

skrx


I've never used pygame - but:

  • Detecting double clicks: at a guess, instead of processing each click immediately, apply a 50ms delay and see if you get another click event in that time. The user probably won't notice the 50ms delay.

  • Distinguishing between scrollwheel up/down: see the comments on this documentation page. Apparently there are five buttons defined - left, middle, right, scrollwheel-up and scrollwheel-down. That is, you can capture scrollwheel events the same way you're capturing left clicks - you just need to look for SCROLL_UP or similar instead of LEFT.

    Look up the documentation to find out exactly what SCROLL_UP is called.

like image 36
Li-aung Yip Avatar answered Sep 20 '22 20:09

Li-aung Yip


A very simple solution is to define a pygame.time.Clock() to keep track of the time between two consecutive MOUSEBUTTONDOWN event.

Before the main loop define:

dbclock = pygame.time.Clock()

and in the event loop controller:

if event.type == pygame.MOUSEBUTTONDOWN:
    if dbclock.tick() < DOUBLECLICKTIME:
        print("double click detected!")

where DOUBLECLICKTIME is the maximum time allowed (in milliseconds) between two clicks for them being considered a double click. Define it before the mainloop. For example, to allow a maximum delay of half a second between the two clicks: DOUBLECLICKTIME = 500.

In pygame is possible to create as many pygame.time.Clock() objects are needed. dbclock must be used only for this purpose (I mean, no other calls to dbclock.tick() anywhere in the main loop) or it will mess with the tracking of the time between the two clicks.


For the sake of completeness, let me add also the answer about the scroll wheel, even if other answers already covered it.
The scroll wheel emits MOUSEBUTTONDOWN and MOUSEBUTTONUP events (it's considered a button). I can be identified by the event.button parameter, which is 4 when the wheel is rolled up, and 5 when the wheel is rolled down.

like image 44
Valentino Avatar answered Sep 23 '22 20:09

Valentino