Pygame Mouse Clicking Detection

Mouse click event pygame

You have to use the MOUSEBUTTONDOWN event instead of pygame.mouse.get_pressed():

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

if event.type == MOUSEBUTTONDOWN:
if event.button == 1: # 1 == left button
print("click!")

pygame.display.flip()
clock.tick(30)

pygame.quit()
sys.exit()

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.

The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released.

The pygame.event.Event() object 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. Each mouse button is associated a value. For instance the value of the attributes is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up respectively mouse wheel down. When multiple keys are pressed, multiple mouse button events occur. Further explanations can be found in the documentation of the module pygame.event.

Mouse click detection in pygame

You have to update your screen each time when you draw or do something in screen. So, put this line under your first while loop.

pygame.display.flip()

In your condition you was checking x and y and which are not mouse position.

if click[0] == 1 and x in range(30) and y in range (30):

Check your mouse position in range(90) because of you have three rectangular and the are 30x30.

if click[0] == 1 and mouse[0] in range(90) and mouse[1] in range (90):

Then, set your rectangular start position to fill the mouse point.

rect_x = 30*(mouse[0]//30) # set start x position of rectangular  
rect_y = 30*(mouse[1]//30) # set start y position of rectangular

You can edit your code with this.

while running:

pygame.display.flip()
event = pygame.event.poll()

#found in what position my mouse is
if event.type == pygame.QUIT:
running = 0
elif event.type == pygame.MOUSEMOTION:
print("mouse at (%d, %d)" % event.pos)


mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

#mouse click
if click[0] == 1 and mouse[0] in range(90) and mouse[1] in range (90):

'''
rect_x = 30*(0//30) = 0
rect_y = 30*(70//30) = 60
'''

rect_x = 30*(mouse[0]//30) # set start x position of rectangular
rect_y = 30*(mouse[1]//30) # set start y position of rectangular

pygame.draw.rect(screen, red, (rect_x, rect_y , 30 , 30)) # rectangular (height, width), (30, 30)

Sample Image

Pygame - Mouse clicks not getting detected

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 (e.g. 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.

Python3 - How to detect mouse clicked on the image or not in pygame

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. The position of the rectangle can be specified by a keyword argument. For example, the center of the rectangle can be specified with the keyword argument center. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments).

You have already set the image rectangle to imagePosition. Use imagePosition rather than image.get_rect():

while running:
# [...]

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = 0
elif event.type == pygame.MOUSEMOTION:
x, y = event.pos
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
s = s + 1 # number of shots fired
shot.play()
if imagePosition.collidepoint(event.pos):
print('clicked on image')

Detect mouse click on circle that is on different surface

You have to compute the mouse position relative to the Surface.

You have 2 possibilities. Either subtract the (top left) position of the Surface on the screen from the mouse position:

x = event.pos[0] - s_rect.left
y = event.pos[1] - s_rect.top

Or add the (top left) position of the Surface on the screen to the center point of the circle:

sqx = (x - (s_rect.left + 20))**2
sqy = (y - (s_rect.top + 20))**2

Complete example

Sample Image

from pygame.locals import *
import pygame, math

pygame.init()
screen = pygame.display.set_mode((640, 480))

s = pygame.Surface((100, 100))
s.fill((255, 0, 0))
s_rect = s.get_rect(center = (300, 300))
clicked = False

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

x = event.pos[0]
y = event.pos[1]

sqx = (x - (s_rect.left + 20))**2
sqy = (y - (s_rect.top + 20))**2

if math.sqrt(sqx + sqy) < 20:
clicked = not clicked
print ('inside')

screen.fill((200, 200, 255))
color = (255, 255, 255) if clicked else (0, 0, 0)
pygame.draw.circle(s, color, (20, 20), 20)
screen.blit(s, s_rect)
pygame.display.update()

pygame.quit()

How do I make pygame recognize a single mouse click as a single mouse click?

You have to use the MOUSEDOWN event instead of pygame.mouse.get_pressed(). While pygame.mouse.get_pressed() returns the current state of the buttons, the MOUSEBUTTONDOWN and MOUSEBUTTONUP occurs only once a button is pressed.



Related Topics



Leave a reply



Submit