简体   繁体   中英

Pygame - Mouse clicks not getting detected

I'm learning Pygame to make games w/ Python. However, I'm encountering a problem. I'm trying to detect when the player is currently clicking the screen, but my code isn't working. Is my code actually screwed, or is it just the online Pygame compiler that I'm using?

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))

while True:
  pygame.display.update()
  mouse = pygame.mouse.get_pressed()
  if mouse:
    print("You are clicking")
  else:
    print("You released")

When I ran this code, the output console spammed the text "You are clicking", thousands of times in a second. Even when I'm not clicking the screen, it still says this. Even when my mouse isn't over the screen. Just the same text. Over, and over. Is Pygame executing my program correctly?

To learn Pygame, I am using the official Docs from the developers. https://www.pygame.org/docs/ Is this an outdated way to learn, is this why my code continues to run errors?

The coordinates which are returned by pygame.mouse.get_pressed() are evaluated when the events are handled. You need to handle the events by either pygame.event.pump() or pygame.event.get() .

See pygame.event.get() :

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

pygame.mouse.get_pressed() returns a sequence of booleans representing the state of all the mouse buttons. Hense you have to evaluate if any button is pressed ( any(buttons) ) or if a special button is pressed by subscription (eg buttons[0] ).

For instance:

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))

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

    # if buttons[0]:  # for the left mouse button
    if any(buttons):  # for any mouse button
        print("You are clicking")
    else:
        print("You released")

    pygame.display.update()

If you just want to detect when the mouse button is pressed respectively released, then you have to implement the MOUSEBUTTONDOWN and MOUSEBUTTONUP (see pygame.event module):

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))

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

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

    pygame.display.update()

While pygame.mouse.get_pressed() returns the current state of the buttons, the MOUSEBUTTONDOWN and MOUSEBUTTONUP occurs only once a button is pressed.

The function pygame.mouse.get_pressed returns a list which contains true or false so for single click u should use-

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
  pygame.display.update()
  mouse = pygame.mouse.get_pressed()
  if mouse[0]:
    print("You are clicking")
  else:
    print("You released")

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