2017-03-07 59 views
0

我已經創建了此遊戲,該遊戲爲您提供了儘可能多地殺死目標的時間限制。以下是暫停和取消暫停遊戲的代碼部分。我遇到的問題是,當我暫停遊戲時,設置時間限制的計時器仍在計數。我該如何解決這個問題?當我將遊戲設置爲暫停狀態時,我該如何停止計時器

paused = False 

def button(msg, msg_two, x, y, w, h, i, a, fontsize, action=None): 
    global paused 
    mouse = pygame.mouse.get_pos() 
    click = pygame.mouse.get_pressed() 

    if (x < mouse[0] < (x+450)) and (y < mouse[1]<(y+100)): 
     pygame.draw.rect(gameDisplay, a, [x, y, w, h]) 
     largeText = pygame.font.Font('freesansbold.ttf',fontsize) 
     TextSurf, TextRect = text_objects(msg, largeText, green) 
     TextRect.center = ((x+(w/2)),(y+(h/2))) 
     gameDisplay.blit(TextSurf, TextRect) 
     if click[0] == 1 and action != None: 
      if action == "play": 
       gameloop() 
      elif action == "quit": 
       game_quit() 
      elif action == "pause": 
       paused = True 
       pause() 
      elif action == "unpause": 
       unpause() 

    else: 
     pygame.draw.rect(gameDisplay, i, [x, y, w, h]) 
     largeText = pygame.font.Font('freesansbold.ttf',fontsize) 
     TextSurf, TextRect = text_objects(msg_two, largeText, green) 
     TextRect.center = ((x+(w/2)),(y+(h/2))) 
     gameDisplay.blit(TextSurf, TextRect) 

def game_quit(): 
    pygame.quit() 
    quit() 

def unpause(): 
    global paused 
    paused = False 

def pause(): 

    pygame.mouse.set_visible(1) 

    button_x = (display_width/4)-150 
    button_y = display_height/1.5 
    button_width = 450 
    button_height = 100 


    while paused: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 


     gameDisplay.fill(white) 
     largeText = pygame.font.Font('freesansbold.ttf',72) 
     TextSurf, TextRect = text_objects('paused', largeText, red) 
     TextRect.center = ((display_width/2),(display_height/3)) 
     gameDisplay.blit(TextSurf, TextRect) 

     mouse = pygame.mouse.get_pos() 

     button("let's go", "carry on", button_x, button_y, button_width, button_height, blue, light_blue, 70, "unpause") 
     button("Bye then :(", "Leaving?", button_x+600, button_y, button_width, button_height, red, light_red, 70, "quit") 

     pygame.display.update() 
     clock.tick(15) 

def gameloop(): 

    gameExit = False 

    start = time.time() 

    elapsed = 0 

    while not gameExit and elapsed < 30: 
     button("Stop", "Stop", 1550, 0, 50, 50, red, light_red, 15, "pause") 
     elapsed = time.time() - start - (enemy_kills/2) 
    gameloop() 

def game_intro(): 
    pygame.mouse.set_visible(1) 

    button_x = (display_width/4)-150 
    button_y = display_height/1.5 
    button_width = 450 
    button_height = 100 

    intro = True 

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


     gameDisplay.fill(white) 
     largeText = pygame.font.Font('freesansbold.ttf',72) 
     TextSurf, TextRect = text_objects('Shoot Hitler and not trump', largeText, red) 
     TextRect.center = ((display_width/2),(display_height/3)) 
     gameDisplay.blit(TextSurf, TextRect) 

     mouse = pygame.mouse.get_pos() 

     button("let's go", "wanna play?", button_x, button_y, button_width, button_height, blue, light_blue, 70, "play") 
     button("Bye then :(", "wanna quit?", button_x+600, button_y, button_width, button_height, red, light_red, 70, "quit") 

     pygame.display.update() 
     clock.tick(15) 

game_intro() 

我道歉,如果我已經錯過了代碼的重要組成部分。告訴我,如果我有

回答

0

這個程序還有其他奇怪的事情,使它很難理解它足以給你一個正確的答案。

例如,您在gameloop函數末尾調用返回gameloop()的事實 - 它會創建一個遞歸調用,根本不應該遞歸調用。

無論如何,會發生什麼事情是你應該將這些函數的大部分放在類中 - 這樣一些狀態變量如paused可以是該類的一個屬性。這個班的實例是爲了玩遊戲。

在這種情況下,您的「開始」變量也將成爲一個屬性,以便可以在unpause方法中對其進行修改。

然後您可以計算暫停時間的數量,並將該數量添加到「開始」滴答計數。

或者你可以讓start成爲一個全局變量,並繼續使用你當前的模式。由於我沒有重新組織所有的代碼來回答問題,因此保持事情或多或少都會要求您這樣做:

def gameloop(): 
    global start 
    ... 

def pause(): 
    global pause_start 
    pause_start = time.time() 
    ... 

def unpause(): 
    global start, pause_start 
    pause_duration = time.time() - pause_start 
    start += pause_duration 
    ... 
相關問題