简体   繁体   English

Pygame-如何在矩形区域制作pygame.mouse.get_pos

[英]Pygame - How to make pygame.mouse.get_pos in a rectangular area

Basically, the question is asking how to get the mouse location for a rectangular area. 基本上,问题是询问如何获取矩形区域的鼠标位置。 So, instead of just finding if the mouse is on, for example, the coordinates 100, 100, it would find if the mouse is inside a rectangular area with the corners being at 100, 100, or where ever the rectangle is. 因此,它不仅可以找到鼠标是否位于例如坐标100、100上,还可以找到鼠标是否位于矩形区域内,并且角点位于100、100或矩形所在的位置。

Sorry if this is a really simple question to answer, I just couldn't find it anywhere. 抱歉,如果这是一个非常简单的问题,我只是在任何地方都找不到。 Thanks 谢谢

import pygame, sys

pygame.init(); clock = pygame.time.Clock()
scr = pygame.display.set_mode((640, 480))

image = pygame.image.load('image.png')
rect = image.get_rect()
rect.center = (320, 240)

while True:
    pygame.display.flip()
    clock.tick(60)
    scr.fill((0, 0, 0))
    scr.blit(image, rect)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit()

    if pygame.mouse.get_pressed()[0]:
        if rect.collidepoint(pygame.mouse.get_pos()):
            print 'The mouse was click inside the image.'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM