Чтобы проверить, находится ли мышь в прямоугольнике в Pygame, вы можете использовать следующие методы:
Метод 1: использование функции colliderect()
import pygame
# Create a rectangle
rect = pygame.Rect(x, y, width, height)
# Check if the mouse is on the rectangle
if rect.collidepoint(pygame.mouse.get_pos()):
# Mouse is on the rectangle
print("Mouse is on the rectangle")
Метод 2. Использование свойств прямоугольника
import pygame
# Create a rectangle
rect = pygame.Rect(x, y, width, height)
# Check if the mouse is on the rectangle
if rect.left <= pygame.mouse.get_pos()[0] <= rect.right and \
rect.top <= pygame.mouse.get_pos()[1] <= rect.bottom:
# Mouse is on the rectangle
print("Mouse is on the rectangle")
Метод 3. Использование функции colliderect()с собственным прямоугольником мыши
import pygame
# Create a rectangle
rect = pygame.Rect(x, y, width, height)
# Create a rectangle for the mouse
mouse_rect = pygame.Rect(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1], 1, 1)
# Check if the mouse rectangle collides with the rectangle
if rect.colliderect(mouse_rect):
# Mouse is on the rectangle
print("Mouse is on the rectangle")