【问题标题】:How do i stop a timer when i set a game to paused当我将游戏设置为暂停时如何停止计时器
【发布时间】:2017-07-28 03:45:27
【问题描述】:

我创建了这个游戏,它给你一个时间限制来杀死尽可能多的目标。下面是暂停和取消暂停游戏的代码部分。我遇到的问题是,当我暂停游戏时,设置时间限制的计时器仍在计数。我怎样才能让它停止?

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()

如果我遗漏了代码的重要部分,我深表歉意。如果我有,请告诉我

【问题讨论】:

    标签: python-3.x time pygame


    【解决方案1】:

    这个程序中还有其他一些奇怪的东西,让你很难理解它来给你一个正确的答案。

    例如,您在 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
       ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多