2016-06-28 359 views
0

出於某種原因,我不能退出這個循環,當我改變爲intro = false,任何人都可以幫助我如何退出這個if語句。這是我的菜單屏幕,一旦我點擊「新遊戲」,我希望它退出game_intro功能。退出如果語句循環python

這是我定義game_intro:

def game_intro(): 
    intro = True 

    if intro == True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

     window.fill(superlightgrey) 
     fontvertical = pygame.font.SysFont("comicsansms", 100)  
     text = fontvertical.render("Connect 4", True, skyblue) 
     word = (10, 1) 
     window.blit(text,word) 
     ButtonIntro("New Game", 340, 140, 200, 50, superlightgrey, superlightgrey, "Play") 

這是我創建的按鈕功能:

def ButtonIntro(msg, x, y, w, h, ic, ac, action=None): 
    mouse = pygame.mouse.get_pos() 
    click = pygame.mouse.get_pressed() 
    if x+w > mouse[0] > x and y+h > mouse[1] > y: 
     pygame.draw.rect(window, ac, (x, y, w, h)) 
     if click[0] == 1 and action != None: 
      pygame.draw.rect(window, lightgrey, (x, y, w, h)) 
      if action == "Play":    
       intro = False 
       ##WHAT DO I NEED HERE TO EXIT LOOP 

而這正是我呼籲函數:

while intro == True: 
    game_intro() 
print("loopexited") 
+0

你可以簡單地做到這一點'回報'聲明。 –

回答

0

intro變量在函數內部,當你在函數內部創建一個變量時,沒有連接到功能外的任何東西。

intro = True 
def myfunction(): 
    intro = False 
myfunction() 
print(intro) 

此代碼打印:

True 

myfunction的內部的intro變量作爲來自外部的一個完全獨立的變量來創建。

看起來你可能在game_intro函數中也有另一個單獨的intro變量。

可以解決這個得到使用global關鍵字,但你可能會更好試圖找到一種不同的方式來組織你的代碼(global被認爲是不好的做法。)