2016-07-28 48 views
2

我在另一个循环中有一个while true循环(代码如下)。我想检查你是否点击了一个按钮,如果是这样,将光标改变成我之前导入的图像。我试图通过隐藏光标并让图像跟着它来做到这一点。但是当我运行它时,它隐藏了光标,它将图像绘制在原来的位置,但不随光标移动。而真正的循环不能在另一个python中工作

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() 

有谁能告诉我我做错了吗?

+5

你的第二个循环将永远不会结束,你需要,如果你想在第一循环中再次重复,以'break'它。 –

回答

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()