而真正的循环在python

时间:2016-07-28 09:01:47

标签: python while-loop

我在另一个循环中有一个while true循环(下面的代码)。我想检查您是否单击了按钮,如果是,请将光标更改为我之前导入的图像。我试图通过隐藏光标并让图像跟随它来做到这一点。但是当我运行它时,它会隐藏光标将图像绘制到原来的位置,但不会随光标一起移动。

while True:
    for event in pygame.event.get():
        if event.type == MOUSEBUTTONUP:
            mousex, mousey = pygame.mouse.get_pos()
            if mousex > 100 and mousex < 200 and mousey > 50 and mousey < 100:  # a button on my screen 
                pygame.mouse.set_visible(False)
                while True:
                    mousex, mousey = pygame.mouse.get_pos()
                    DISPLAYSURF.blit(cursorImg, (mousex,mousey))
                    pygame.display.update()

有人可以告诉我,我做错了吗?

1 个答案:

答案 0 :(得分:1)

将您的代码更改为:

while True:
    for event in pygame.event.get():
        if event.type == MOUSEBUTTONUP:
            mousex, mousey = pygame.mouse.get_pos()
            if mousex > 100 and mousex < 200 and mousey > 50 and mousey < 100:  # a button on my screen 
                pygame.mouse.set_visible(False)
                mousex, mousey = pygame.mouse.get_pos()
                DISPLAYSURF.blit(cursorImg, (mousex,mousey))
    pygame.display.update()