2017-03-07 19 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のような状態変数のいくつかがそのクラスの属性になるようにしなければなりません。クラスのインスタンスはゲームプレイを目的としています。

この場合、 "start"変数も属性になり、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 
    ... 
関連する問題