当我运行程序并单击my_rect时,没有任何反应,最终它会停止工作

时间:2015-01-23 15:52:45

标签: python-2.7 pygame

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            is_inside = my_rect.collidepoint(pygame.mouse.get_pos())
            print is_inside
            if is_inside == 1:
                myfont = pygame.font.SysFont("monospace", 15)

                label = myfont.render("The Penrose Triangle by Roman Formicola", 5, (black))
                gameDisplay.blit(label, (300,150))

我有它的目标是,一旦你点击my_rect,消息将无限期地留在那里

1 个答案:

答案 0 :(得分:0)

只需创建一个包含Rect的变量(实际使用Rect类):

my_rect = pygame.Rect(400, 300, 100, 100)

然后,您可以使用Rect的{​​{3}}方法检查鼠标位置是否在my_rect内:

...
    elif event.type == pygame.MOUSEBUTTONDOWN:
        is_inside = my_rect.collidepoint(pygame.mouse.get_pos())
pygame.draw.rect(gameDisplay, cyan, my_rect, 0)
...