简体   繁体   English

如何使用pygame事件循环让pygame鼠标继续返回事件

[英]How to make the pygame mouse continue to return events using the pygame event loop

I would like to know how to make it so that when you hold down the pygame mouse, it continues getting events in the event loop.我想知道如何做到这一点,以便当您按住 pygame 鼠标时,它会继续在事件循环中获取事件。
I do not want to use pygame.mouse.get_pressed() because I want to also have a hold-down time to where it doesn't do anything.我不想使用 pygame.mouse.get_pressed() 因为我还想有一个不做任何事情的按住时间。
Kind of like if you hold down a key in a text box in your browser: it types one key, then waits for about half a second, then starts typing a lot of keys really fast.有点像如果您在浏览器的文本框中按住一个键:它键入一个键,然后等待大约半秒钟,然后开始非常快速地键入很多键。
Also, I think it involves something before the main loop when the window is created or something, but I'm not sure.另外,我认为它在创建窗口时的主循环之前涉及一些东西,但我不确定。
I know there's a way to do this in pygame because I saw a Stack Overflow answer to it before, but after a while of searching, I cannot find it.我知道在 pygame 中有一种方法可以做到这一点,因为我之前看到过 Stack Overflow 的答案,但是经过一段时间的搜索,我找不到它。 If you have seen it or can find it, that is also appreciated too.如果您已经看过或可以找到它,那也将不胜感激。 This is my first time asking a question, so if I did anything wrong, please tell me.这是我第一次问问题,所以如果我做错了什么,请告诉我。
Here is the code:这是代码:

import pygame

WIDTH = 1000
HEIGHT = 800
WIN = pygame.display.set_mode()

def main():
    run = True 
    clock = pygame.time.Clock()
    
    while run:
        clock.tick(60)
      
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                print("the mouse was pressed")
        
        WIN.fill((0, 0, 0))

        pygame.display.update()

I would like to know how to make it so that when you hold down the pygame mouse, it continues getting events in the event loop.我想知道如何做到这一点,以便当您按住 pygame 鼠标时,它会继续在事件循环中获取事件。

Yo can't.哟不能。 However you can use pygame.mouse.get_pressed() :但是,您可以使用pygame.mouse.get_pressed()

import pygame

WIDTH = 1000
HEIGHT = 800
WIN = pygame.display.set_mode()

def main():
    run = True 
    clock = pygame.time.Clock()
    
    while run:
        clock.tick(60)
      
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
  
        buttons = pygame.mouse.get_pressed() 
        if any(buttons):
            print("the mouse was pressed")

        WIN.fill((0, 0, 0))
        pygame.display.update()

The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. MOUSEBUTTONDOWN事件在您单击鼠标按钮时发生一次,而MOUSEBUTTONUP事件在释放鼠标按钮时发生一次。
pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the current state ( True or False ) of all mouse buttons. pygame.mouse.get_pressed()返回一个布尔值列表,代表所有鼠标按钮的当前状态( TrueFalse )。 The state of a button is True as long as a button is held down.只要按下按钮,按钮的状态就是True

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

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