Python Forum
[PyGame] Detect if mouse enters area
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Detect if mouse enters area
#1
Is there a better way of detecting if the mouse cursor enters a rect other than how I'm doing it here?

#! /usr/bin/env python3.10

# Do the imports
import pygame
import os

# Get current working directory
path = os.getcwd()

# Initialize pygame
pygame.init()

# Set screen size
screen = pygame.display.set_mode((800,600))

# Set and display the window icon
# game_icon = pygame.image.load(f'{path}/icons/ratt.ico')
#pygame.display.set_icon(game_icon)

# Set a hand cursor
hand = pygame.SYSTEM_CURSOR_HAND
arrow = pygame.SYSTEM_CURSOR_ARROW

# Set and display window title
pygame.display.set_caption('My Game')

# pygame clock for setting frame rate
clock = pygame.time.Clock()

font = pygame.font.SysFont(None, 120)
text = font.render('Test Text', True, 'ivory')
shadow = font.render('Test Text', True, 'steelblue')

btn_font = pygame.font.SysFont(None, 40)
button = pygame.Rect(350, 500, 120, 40)
btn_background = pygame.Rect(350, 500, 120, 40)
btn_shadow = pygame.Rect(352, 502, 120, 40)
btn_text = btn_font.render('Button', True, 'white')

# Draw rectangle
myrect = pygame.Rect(20, 10, 760, 80)



# Main program
def main():
    running = True

    while running:
        # Get mouse position
        mx, my = pygame.mouse.get_pos()

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

        screen.fill('ivory')

        pygame.draw.rect(screen, 'lightskyblue', myrect)
        pygame.draw.rect(screen, 'royalblue', myrect, 2)
        screen.blit(shadow, (220, 15))
        screen.blit(text,(218, 13))

        pygame.draw.rect(screen, (170, 170, 170), btn_shadow)
        pygame.draw.rect(screen, 'tomato', btn_background)
        pygame.draw.rect(screen, 'black', button, 1)

        # Check if cursor is in the button area
        if mx >= btn_background[0] and mx <= btn_background[0]+119 \
        and my >= btn_background[1] and my <= btn_background[1]+39:
            pygame.mouse.set_cursor(hand)
        else:
            pygame.mouse.set_cursor(arrow)

        screen.blit(btn_text, (363, 507))


        pygame.display.update()
        clock.tick(60)

if __name__ == '__main__':
    main()
Thanks in advance
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
Using the collidepoint () method may be a good alternative to checking the btn_background co-ordinates. See line 73.

#! /usr/bin/env python3.10
 
# Do the imports
import pygame
import os
 
# Get current working directory
path = os.getcwd()
 
# Initialize pygame
pygame.init()
 
# Set screen size
screen = pygame.display.set_mode((800,600))
 
# Set and display the window icon
# game_icon = pygame.image.load(f'{path}/icons/ratt.ico')
#pygame.display.set_icon(game_icon)
 
# Set a hand cursor
hand = pygame.SYSTEM_CURSOR_HAND
arrow = pygame.SYSTEM_CURSOR_ARROW
 
# Set and display window title
pygame.display.set_caption('My Game')
 
# pygame clock for setting frame rate
clock = pygame.time.Clock()
 
font = pygame.font.SysFont(None, 120)
text = font.render('Test Text', True, 'ivory')
shadow = font.render('Test Text', True, 'steelblue')
 
btn_font = pygame.font.SysFont(None, 40)
button = pygame.Rect(350, 500, 120, 40)
btn_background = pygame.Rect(350, 500, 120, 40)
btn_shadow = pygame.Rect(352, 502, 120, 40)
btn_text = btn_font.render('Button', True, 'white')
 
# Draw rectangle
myrect = pygame.Rect(20, 10, 760, 80)
 
 
 
# Main program
def main():
    running = True
 
    while running:
        # Get mouse position
        mx, my = pygame.mouse.get_pos()
 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
 
        screen.fill('ivory')
 
        pygame.draw.rect(screen, 'lightskyblue', myrect)
        pygame.draw.rect(screen, 'royalblue', myrect, 2)
        screen.blit(shadow, (220, 15))
        screen.blit(text,(218, 13))
 
        pygame.draw.rect(screen, (170, 170, 170), btn_shadow)
        pygame.draw.rect(screen, 'tomato', btn_background)
        pygame.draw.rect(screen, 'black', button, 1)
 
        # Check if cursor is in the button area
        '''
        if mx >= btn_background[0] and mx <= btn_background[0]+119 \
        and my >= btn_background[1] and my <= btn_background[1]+39:
        '''
        if button.collidepoint (mx, my) :
            pygame.mouse.set_cursor(hand)
        else:
            pygame.mouse.set_cursor(arrow)
 
        screen.blit(btn_text, (363, 507))
 
 
        pygame.display.update()
        clock.tick(60)
 
if __name__ == '__main__':
    main()
menator01 likes this post
Reply
#3
Thanks
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020