检查python中范围内的坐标

时间:2014-01-30 11:46:53

标签: python python-3.x pygame

randomNumber = random.randint(1,625)
randomNumber2 = random.randint(1,420)
pygame.draw.rect(screen,RED,(randomNumber,randomNumber2,50,50),5)
if event.type == pygame.MOUSEBUTTONDOWN:
   y = pygame.mouse.get_pos()

我想知道如何检查鼠标在点击时是否已经点击了已绘制的方块。

1 个答案:

答案 0 :(得分:3)

randomNumber = random.randint(1,625)
randomNumber2 = random.randint(1,420)
my_rect = pygame.draw.rect(screen,RED,(randomNumber,randomNumber2,50,50),5)

if event.type == pygame.MOUSEBUTTONDOWN:
   x, y = pygame.mouse.get_pos()

   is_inside = my_rect.collidepoint(x, y) 

   if is_inside:
       #DO WHATEVER YOU WANT TO DO

试试这个。