Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to check if mouse is over a rect in pygame

rectangle = pygame.Rect(5, 5, 20, 20) # x, y, width, height

def is_over(rect, pos):
    # function takes a tuple of (x, y) coords and a pygame.Rect object
    # returns True if the given rect overlaps the given coords
    # else it returns False
    return True if rect.collidepoint(pos[0], pos[1]) else False

pos = pygame.mouse.get_pos() # gets the current mouse coords
if is_over(rectangle, pos): # pass in the pygame.Rect and the mouse coords
    print('The mouse is over the rectangle')
else:
    print('The mouse is not over the rectangle')
Comment

python pygame how to check if the mouse is on a rect

rect = pygame.Rect(x,y,width,height)
mouse = pygame.mouse.get_pos()

mouse_on_rect = rect.collidepoint(mouse)
# this will tell you if the mouse(a 1x1 rect) is on the rect variable)
Comment

PREVIOUS NEXT
Code Example
Python :: requests session in python 
Python :: all subarrays of an array python 
Python :: use of // in python 
Python :: python utf8 
Python :: how to find the cube of a number in python 
Python :: language detection python 
Python :: random list python 
Python :: add rectangle matplotlib 
Python :: python read from txt file 
Python :: python remove duplicates from 2d list 
Python :: python read column data from text file 
Python :: python get lan ip 
Python :: add headers tp requests python 
Python :: binary search tree iterator python 
Python :: remove spaces from input python 
Python :: max of matrix numpy 
Python :: python enumerate() function 
Python :: python 3.9.5 installed update default version 
Python :: except as exception: 
Python :: discord.py cog 
Python :: python - removeempy space in a cell 
Python :: get os information python 
Python :: pyqt5 line edit password input 
Python :: how to access variable from another function in same class in python 
Python :: bot ping discord.py 
Python :: how to separate a string or int with comma in python 
Python :: install hydra python 
Python :: discord.py how to use permissions 
Python :: slack send message python 
Python :: append method linked list python 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =