首页 > 解决方案 > 在pygame中悬停时更改矩形的边框

问题描述

我有一个矩形:

PlayButton = pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(324, 380, 354, 35))

我想让矩形的边框在用户将鼠标悬停在矩形上时将颜色变为红色

# Checks if the rectangle is hovered over 
if PlayButton.collidepoint(pygame.mouse.get_pos()):
            print ('mouse is over newGameButton')

谢谢

标签: pythonpygame

解决方案


Set a state variable when the the muose hover:

hover = PlayButton.collidepoint(pygame.mouse.get_pos())

Draw an additional pygame.draw.rect() where the width parameter is set:

e.g. red border with a thickness of 3

if hover:
    pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(324, 380, 354, 35), 3)

推荐阅读