pygame鼠标按钮不起作用

时间:2014-05-23 23:08:44

标签: python pygame mouse

我正在制作这个多项选择程序,但是我需要鼠标事件才能在按下输入后才能工作。

这是我的代码:

    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

然后在下面我有这个:

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

当我按下鼠标左键时,Derp不会打印。 但是,当我把它缩进时:

    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会打印

2 个答案:

答案 0 :(得分:2)

你可以使用一个布尔表示是否按下了输入。

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")

我认为问题在于你正在迭代所有事件,并且在那个循环中你正在迭代所有事件(再次),这会导致一些问题

答案 1 :(得分:0)

很难说你说的是什么,但我有几个建议给你。首先,确保带有derp的for循环不在任何你不希望它出现的if语句中。其次,确保每个游戏循环调用pygame.event.get()一次,否则代码只会给你事件发生在两次通话之间,这不是全部。如果这些都不起作用,请尝试发布整个代码。