0

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)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – J_H Jan 09 '23 at 20:46
  • 1
    `pygame.mouse.get_pressed()` is not an event, but returns the current state of the button. You have to use the `MOSUEDOWN` event. See [Pygame mouse clicking detection](https://stackoverflow.com/questions/10990137/pygame-mouse-clicking-detection) – Rabbid76 Jan 09 '23 at 20:47

1 Answers1

1

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)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174