2
votes

I am making this multiple choice program, but I need the mouse event to only work after enter is pressed.

Here is my code:

    for event in pygame.event.get(): # If user did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True
        elif event.type == pygame.KEYDOWN: # If user pressed a key
            if event.key == pygame.K_RETURN: # If user pressed enter
                # Makes the start screen go away
                enter_pressed = True
                # Increments question_number
                question_number += 1

                # Where I Draw the question screen

Then down below I have this:

                for event in pygame.event.get(): # If user did something
                    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        print("Derp") 

Derp won't print when I press left mouse button. However, when I have it indented like this:

    for event in pygame.event.get(): # If user did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True
        elif event.type == pygame.KEYDOWN: # If user pressed a key
            if event.key == pygame.K_RETURN: # If user pressed enter
                # Makes the start screen go away
                enter_pressed = True
                # Increments question_number
                question_number += 1

                # Where I Draw the question screen           

        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        print("Derp") 

Derp does print when I press left mouse button

2

2 Answers

3
votes

You could use a boolean indicating wheter or not enter was pressed.

for event in pygame.event.get(): # If user did something
    enter_pressed = False
    if event.type == pygame.QUIT: # If user clicked close
        done = True
    elif event.type == pygame.KEYDOWN: # If user pressed a key
        if event.key == pygame.K_RETURN: # If user pressed enter
            # Makes the start screen go away
            enter_pressed = True
            # Increments question_number
            question_number += 1

            # Where I Draw the question screen           

    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and enter_pressed:
                    print("Derp")

I think that the problem is that you are iterating over all events, and inside that loop you are iterating over all events (again), and that causes some problems

0
votes

It is hard to tell from what you said, but I have a couple of recommendations for you. First, make sure that the for loop with derp isn't in any if statements you don't want it to be in. Second, make sure you call pygame.event.get() once per game loop or the code will only give you events that happened between the two calls, which will not be all of them. If neither of these things work, try posting the whole code.