简体   繁体   中英

Python pygame pygame.mouse.get_pressed()[0] responses when i move mouse, not when the mouse is in the same position

Im making a 2d game where you can shoot, so the logic is when pygame.mouse.get_pressed()[0] (mouse is pressed) bullets are created. Problem is, when im holding my finger on mouse, and key is pressed, my code creates one bullet when i start clicking, when im still clicking nothings happening, when i move my mouse, the bullets start to create. Pygame dont create bullets when the mouse is in a single position, i have to move mouse. Whats wrong. Here is the important fragment of code. (its in a def run())

its in a loop ( https://i.stack.imgur.com/0Y86f.png )

pygame.mouse.get_pressed() is not an event, but returns the current state of the button. You have to use the MOUSEBUTTONDOWN event. See also Pygame mouse clicking detection .
pygame.mouse.get_pressed() returns a list of Boolean values that represent the state ( True or False ) of all mouse buttons. The state of a button is True as long as a button is held down. If you just want to detect when the mouse button is pressed or released, then you have to implement the MOUSEBUTTONDOWN and MOUSEBUTTONUP (see pygame.event module):

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            print("clicked", event.button)
        if event.type == pygame.MOUSEBUTTONUP:
            print("released", event.button)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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