'How to make MOUSEBUTTONDOWN to only detect the left clicks?

MOUSEBUTTONDOWN detects Left, Right and MouseWheel events. I want to make it to only detect the left clicks.

Source Code:

import pygame, sys, time
from pygame.locals import *
from millify import millify,prettify

pygame.mixer.init()
pygame.init()
pygame.mixer.music.load("soundtrack.wav")
WHITE = 255,255,255
font = pygame.font.SysFont(None, 44)
cpsecond = open("clickpersecond.txt", "r+")
cps = int(cpsecond.read())
baltotal = open("totalbal.txt", "r+")
totalbal = int(baltotal.read())
totalbalM = prettify(totalbal, '.')
clock = pygame.time.Clock()
w = 800
h = 600
screen = pygame.display.set_mode((w,h))
pygame.display.set_caption('Tap Simulator')
Loop = True
background = pygame.image.load("Background.jpg")
egg = pygame.image.load("egg.png")
resized_egg = pygame.transform.scale(egg, (282, 352))
text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
pygame.mixer.music.play(-1,0.0)
while Loop: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            Loop = False


        if event.type == MOUSEBUTTONDOWN: #I want it to only detect left click
            egg_rect = resized_egg.get_rect(topleft = (260,150))
            if egg_rect.collidepoint(event.pos):
                totalbal += cps
                totalbalM = prettify(totalbal, '.')
                text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
                print("Your total clicks are", totalbalM, end="\r")

    #print(pygame.mouse.get_pos()) #to get mouse pos
    screen.blit(background, (0,0))
    screen.blit(text, (235,557))
    screen.blit(resized_egg, (260,150))
    pygame.display.flip()
    pygame.display.update()
    clock.tick(30)

with open("totalbal.txt", "w") as baltotal:
    baltotal.write(str(totalbal))
baltotal.close

pygame.quit()
sys.exit()


Solution 1:[1]

See Pygame mouse clicking detection. The pygame.event.Event() object that is generated for the MOUSEBUTTONDOWN has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked. The value of the button attribute is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up respectively mouse wheel down.

Hence, if you want to detect a left click you need to check if event.button == 1:

while Loop: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            Loop = False


        if event.type == MOUSEBUTTONDOWN: #I want it to only detect left click
   
            if event.button == 1: # 1 == left 

                egg_rect = resized_egg.get_rect(topleft = (260,150))
                if egg_rect.collidepoint(event.pos):
                    totalbal += cps
                    totalbalM = prettify(totalbal, '.')
                    text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
                    print("Your total clicks are", totalbalM, end="\r")

    # [...]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1