简体   繁体   English

pygame鼠标按下不起作用

[英]pygame mouse button down not working

I am making this multiple choice program, but I need the mouse event to only work after enter is pressed. 我正在制作此多选程序,但是我需要鼠标事件仅在按下Enter键后才能起作用。

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. 当我按鼠标左键时,不会打印Derp。 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 当我按下鼠标左键时,Derp会打印

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 我认为问题在于您要遍历所有事件,并且在该循环内您要遍历所有事件(再次),这会导致一些问题

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. 首先,请确保您不希望包含derp的for循环不在任何if语句中。其次,请确保每个游戏循环调用一次pygame.event.get() ,否则代码只会给出您在这两个调用之间发生的事件将不会全部发生。 If neither of these things work, try posting the whole code. 如果这些都不起作用,请尝试发布整个代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM