lnNoam
lnNoam

Reputation: 1055

Mouse click only after keyboard press sequence complete (pygame)

I want the code tied to a mouse click (pygame.MOUSEBUTTONUP) to execute only AFTER the code tied to a spacebar press (pygame.KEYDOWN and pygame.K_SPACE) is 100% finished. Any mouse clicks prior to that should be ignored.

I know the second if statement won't work because of its relation, or lack thereof, to for event in pygame.event.get():. I just don't know how to write that correctly...

import pygame

screen = pygame.display.set_mode((600, 400))

def task():

taskExit = False

while not taskExit:

     for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                print "Drawing..."
                screen.fill(gray)  
                    #<<<code to create pretty pictures>>>

                if event.type == pygame.MOUSEBUTTONUP:
                    print "mouse at (%d, %d)" % event.pos  # where they clicked.
                        #<<<more code to interact with said pretty pictures>>>

task()
pygame.quit()
quit()

Upvotes: 0

Views: 530

Answers (2)

lnNoam
lnNoam

Reputation: 1055

So, for posterity, there is a very simple way to do this in pygame (oh, the value in actually reading the documentation...)

Namely, with pygame.event.set_blocked(pygame.MOUSEBUTTONUP) and pygame.event.set_allowed(pygame.MOUSEBUTTONUP) enables one to simply block the left mouse click at will. Very handy.

e.g.,

import pygame

screen = pygame.display.set_mode((600, 400))

def task():

    space_bar_pressed = 0

    taskExit = False

    while not taskExit:

         for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

                elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    pygame.event.set_blocked(pygame.MOUSEBUTTONUP)  #blocking
                    print "Drawing..."
                    screen.fill(gray)
                    #<<<code to create pretty pictures>>>
                    pygame.display.update()
                    pygame.event.set_allowed(pygame.MOUSEBUTTONUP)  #allowing
                    space_bar_pressed = 1

                elif event.type == pygame.MOUSEBUTTONUP and space_bar_pressed == 1:
                    print "mouse at (%d, %d)" % event.pos  # where they clicked.
                    #<<<more code to interact with said pretty pictures>>>
                    pygame.display.update()

task()
pygame.quit()
quit()

Upvotes: 1

mstuebner
mstuebner

Reputation: 434

With a little re-organisation this will work as you expect. Export the code you want to execute after these events into two functions:

    def space_bar():
            print "Drawing..."
            screen.fill(gray)  
                #<<<code to create pretty pictures>>>

    def mouse_event():
            print "mouse at (%d, %d)" % event.pos  # where they clicked.
                #<<<more code to interact with said pretty pictures>>>

which allows you to call them both after the mouse event and give you control about the sequence the code is executed.

    elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
        space_bar()

    elif event.type == pygame.MOUSEBUTTONUP:
        space_bar()
        mouse_event()

Upvotes: 2

Related Questions