📌  相关文章
📜  如何检查鼠标是否在pygame中的矩形上 - Python代码示例

📅  最后修改于: 2022-03-11 14:45:38.478000             🧑  作者: Mango

代码示例1
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')