2016-07-26 149 views
-2

我得到了這麼多,它有點混亂,我不能找出代碼的一個簡單的問題。遊戲不會停止事件循環

while not gameExit: 
    while gameOver == True: 
     pygame.display.flip() 
     gameDisplay.fill(white) 
     display_message("OVER. C to continue, Q to quit", red) 

     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_q: 
        gameOver = False 
        gameExit = True 
       if event.key == pygame.K_c: 
        gameLoop() 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      gameExit = True 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_LEFT: 
       lead_x_change = -10 
       lead_y_change = 0 
      elif event.key == pygame.K_RIGHT: 
       lead_x_change = 10 
       lead_y_change = 0 
      elif event.key == pygame.K_UP: 
       lead_y_change = -10 
       lead_x_change = 0 
      elif event.key == pygame.K_DOWN: 
       lead_y_change = 10 
       lead_x_change = 0 
     if (lead_x < 0) or (lead_x >= 600) or (lead_y < 0) or (lead_y >= 800): 
      gameOver = True 

所以,當我運行這段代碼,當蛇去邊界上,該下繼續和Q表示退出的事不來UNTIL我按下鍵盤上的按鈕或移動我的鼠標。這是爲什麼?

+0

你已經解決了這個問題嗎? – 2016-08-03 14:08:17

回答

0

移動:

if (lead_x < 0) or (lead_x >= 600) or (lead_y < 0) or (lead_y >= 800): 
     gameOver = True 

pygame.event.get -loop的,因爲如果事件已經發生它將只運行(鼠標移動,按下按鍵)。否則,如果沒有您的操作,代碼將無法到達支票。