[Pygame] checking if mouse is over a mask

[Pygame] checking if mouse is over a mask

Postby metulburr » Sun Jul 07, 2013 1:21 am

I know of checking if mouse is in a rect:
Code: Select all
if obj.image_rect.collidepoint(pygame.mouse.get_pos()):


i know of checking if a mask is overlapping another mask:
Code: Select all
if obj.mask.overlap(second_mask, (offset_x, offset_y)):


but how do you check if the mouse is within the mask? To be honest i would of thought this is long drawn out, but would of at least worked, but yet nope.

Code: Select all
import pygame
pygame.init()
screensize = (400,400)
screen = pygame.display.set_mode(screensize)

image = pygame.Surface([50,50])
image.fill((255,255,255))
image_rect = image.get_rect()
mask = pygame.mask.from_surface(image)


mouse = pygame.Surface([1,1])
mouse_rect = mouse.get_rect()
mouse_mask = pygame.mask.from_surface(mouse)


run = True
while run:
    screen.fill((0,0,0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    #if image_rect.collidepoint(pygame.mouse.get_pos()):
    #    print('mouse over box')
       
    mouse_rect = pygame.mouse.get_pos()
    x = mouse_rect[0] - image_rect.x
    y = mouse_rect[1] - image_rect.y
    if mask.overlap(mouse_mask, (x,y)):
        print('mouse over box')
       
    screen.blit(image, image_rect)
    pygame.display.flip()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] checking if mouse is over a mask

Postby DrakeMagi » Sun Jul 07, 2013 2:27 am

if you just checking a point like (5,5)
just use if mask.get_at(mouse_position()) it return 0 or nonzero
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 112
Joined: Sun May 12, 2013 8:36 pm

Re: [Pygame] checking if mouse is over a mask

Postby metulburr » Sun Jul 07, 2013 3:16 am

why would it get indexerror if it returns zero or nonzero?
Code: Select all
Traceback (most recent call last):
  File "test.py", line 24, in <module>
    if mask.get_at(pygame.mouse.get_pos()):
IndexError: 393, 74 is out of bounds
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] checking if mouse is over a mask

Postby Mekire » Sun Jul 07, 2013 3:27 am

You have to do an initial check to see if the cursor is in the rectangle containing the mask; then use get_at.

Like:
Code: Select all
pos = pg.mouse.get_pos()
if Rect.collidepoint(pos):
    offset = pos[0]-Rect.x,pos[1]-Rect.y
    if Mask.get_at(offset):
        print("Mask collission.")

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] checking if mouse is over a mask

Postby metulburr » Sun Jul 07, 2013 4:30 am

oh ok, i had that in the original but didnt put that part in the test script for this.

I am going to put in my test script as i know i will one day be searching for this same exact thing:
Code: Select all
import pygame
pygame.init()
screensize = (400,400)
screen = pygame.display.set_mode(screensize)

image = pygame.Surface([50,50])
image.fill((255,255,255))
image_rect = image.get_rect(center=(200,200))
mask = pygame.mask.from_surface(image)


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

    mouse = pygame.mouse.get_pos()
    if image_rect.collidepoint(mouse):
        offset = mouse[0] - image_rect.x, mouse[1] - image_rect.y
       
        if not mask.get_at(offset):
            if pygame.mouse.get_pressed()[0]:
                print('pressed box')
            else:
                print('not pressed box')
   
    screen.blit(image, image_rect)
    pygame.display.flip()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] checking if mouse is over a mask

Postby Mekire » Sun Jul 07, 2013 11:55 am

You should note that in your sample above--because you neither set a color key, nor use convert_alpha--your mask is using the color of the top left pixel in your image as the color key, effectively creating an empty mask. A very easy gotcha to get hit by. Basically you should never not convert a surface, and if you use standard convert, you better set a color key for that surface (unless you are fully expecting the topleft corner to be picked as the empty color).

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests