2013-07-16 153 views
0

我想获得一定的循环鼠标以前被点击后运行后运行。 我发起mouse_clicked假称为变量,然后将其更改为True鼠标已被点击之后。然而,这似乎并没有让事情继续下去。 这是我的代码:pygame的:在获得环鼠标点击

import sys, pygame 

size = width, height = 320, 240 
screen = pygame.display.set_mode(size) 

running = True 
mouse_pressed = False 

while running: 
    while mouse_pressed: 
     rect = pygame.Rect(10, 20, 30, 30) 
     pygame.draw.rect(screen, (255,0,0), rect) 
     pygame.display.flip() 

     for event in pygame.event.get(): 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       mouse_pressed = True 
      if event.type == pygame.QUIT: 
       running = False  
sys.exit(0) 

谢谢! 奥马尔

回答

2

回答得太快

四处移动你的循环后可进行编辑

while running: 
    rect = pygame.Rect(10, 20, 30, 30) 
    pygame.draw.rect(screen, (255,0,0), rect) 
    pygame.display.flip() 

    for event in pygame.event.get(): 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      mouse_pressed = True 
     if event.type == pygame.QUIT: 
      running = False 

    while mouse_pressed: 
     # do your stuff 
     mouse_pressed = False 

在你的版本,整个循环永远不会发生,因为mouse_pressed被初始化为假。

1

它看起来像你的第二个循环是连首发:你正在发起MOUSE_PRESSED为假。因此,

while mouse_pressed 

必须在它开始之前停止循环。 希望这有助于!